DATA Structures :Binary Search Trees
Bnary Search Trees
Binary tree is a tree that each node in it has maximum of two children. Binary
search tree (BST) is a binary tree which its elements positioned in special
order. In each BST all values(i.e key) in left sub tree are less than values
in right sub tree.
This is a simple implementation of Binary Search Tree Insertion using
Python.
class Node:
def __init__(self, val):
self.l_child = None
self.r_child = None
self.data = val
def insert(root, node):
if root is None:
root =
node
else:
if root.data > node.data:
if root.l_child is None:
root.l_child = node
else:
insert(root.l_child, node)
else:
if root.r_child is None:
root.r_child = node
else:
insert(root.r_child, node)
Traversing to all nodes in BST trees
we have different traversal techniques as listed below:
1) In order:
In this traversal technique, we traverse the left subtree first till there
are no more nodes in the left subtree. After this, we visit the root node
and then proceed to traverse the right subtree until there are no more nodes
to traverse in the right subtree. Thus the order of inOrder traversal is
left->root->right.
def in_order_print(root):
if not root:
return
in_order_print(root.l_child)
print root.data
in_order_print(root.r_child)
2) Pre-order:
For preorder traversal technique, we process the root node first, then we
traverse the entire left subtree and finally, we traverse the right subtree.
Hence the order of preorder traversal is root->left->right.
def pre_order_print(root):
if not root:
return
print root.data
pre_order_print(root.l_child)
pre_order_print(root.r_child)
3) Post-order: In the post-order traversal technique, we traverse the
left subtree, then the right subtree and finally the root node. The order of
traversal for the postorder technique is left->right->root.
def post_order_print(root):
if not root:
return
post_order_print(root.l_child)
post_order_print(root.r_child)
print root.data
Binary Search Tree - Deletion
Before starting with deletion I just want to put some lights on what is a
Binary search tree(BST), Each node in a BST can have maximum of two nodes(left
and right child).The left sub-tree of a node has a key less than or equal to
its parent node's key. The right sub-tree of a node has a key greater than to
its parent node's key. Deleting a node in a tree while maintaining its Binary
search tree property.
There are three cases to be considered while deleting a node.
Case 1: Node to be deleted is the leaf node.
Case 2: Node to be deleted has one child.
Case 3: Node to be deleted has both children.
Implementation in c++
struct node
{
int
data;
node
*left, *right;
};
node* delete_node(node *root, int data)
{
if(root == nullptr) return root;
else if(data < root->data)
root->left = delete_node(root->left, data);
else if(data > root->data)
root->right = delete_node(root->right, data);
else
{
if(root->left == nullptr &&
root->right == nullptr) // Case 1
{
free(root);
root =
nullptr;
}
else
if(root->left == nullptr) // Case 2
{
node* temp
= root;
root=
root->right;
free(temp);
}
else if(root->right == nullptr) // Case 2
{
node* temp
= root;
root = root->left;
free(temp);
}
else // Case 3
{
node*
temp = root->right;
while(temp->left != nullptr) temp =
temp->left;
root->data = temp->data;
root->right =
delete_node(root->right, temp->data);
}
}
return root;
}
Time complexity of above code is O(h), where h is the height of the tree.
If Any one need our help regarding any subject are technology we can
help you. Please Subscribe and Comment
Contact Persons
Rajesh G
Software Developer At GGK
gundupallirajesh@gmail.com
Haswanth M
Front End Developer at TCS
haswanthmuktha@gmail.com
Poorna K
FullStack Dev at Wipro
poorna.k26@gmail.com
Awesome
ReplyDelete