Data Structures: Lecture-6
Data Structures: Lecture-6
LECTURE-6
TREE
➢ The left subtree of a node contains only nodes with keys lesser than the node’s key.
➢ The right subtree of a node contains only nodes with keys greater than the node’s key.
➢ The left and right subtree each must also be a binary search tree.
➢ There must be no duplicate nodes.
Construct a Binary Search Tree (BST) for the following sequence of numbers-
A binary search tree is traversed in exactly the same way a binary tree is traversed
Preorder Traversal
Inorder Traversal-
10, 20 , 30 , 100 , 150 , 200 , 300
Postorder Traversal-
10, 30 , 20 , 150 , 300 , 200 , 100
Construct a Binary Search Tree from given postorder
➢ The last element of postorder traversal is always root. We first construct the root.
➢ Then we find the index of last element which is smaller than root. Let the index be ‘i’.
➢ The values between 0 and ‘i’ are part of left subtree, and the values between ‘i+1’ and ‘n-2’ are
part of right subtree.
➢ Divide given postorder at index “i” and recur for left and right sub-trees.
Example:
10 is the last element, so we make it root. Now we look for the last element smaller than 10, we find 5.
10
/ \
/ \
{1, 7, 5} {50, 40}
We recursively follow above steps for subarrays {1, 7, 5} and {40, 50}, and get the complete tree.
10
/ \
5 40
/ \ \
1 7 50
Construct BST from given preorder traversal
➢ The first element of preorder traversal is Example: Preorder- {10, 5, 1, 7, 40, 50}
always the root.
10 is the first element, so we make it root. Now we
➢ We first construct the root. Then we find the look for the first element greater than 10, we find 40.
index of the first element which is greater So we know the structure of BST is as follows.:
than the root. 10
/ \
➢ Let the index be ‘i’. The values between root {5, 1, 7} {40, 50}
and ‘i’ will be part of the left subtree, and the
We recursively follow the above steps for subarrays
values between ‘i’ and ‘n-1’ will be part of the {5, 1, 7} and {40, 50}, and get the complete tree
right subtree. 10
➢ Divide the given pre[] at index “i” and recur / \
5 40
for left and right sub-trees. / \ \
1 7 50
Construct BST from given inorder traversal
3
Example: inorder traversal is 1,2,3,4,5,6
/ \
2 5
/ / \
1 4 6
Insertion in Binary Search Tree
A new key is always inserted at the leaf by maintaining the property of the binary search tree. We start
searching for a key from the root until we hit a leaf node. Once a leaf node is found, the new node is added
as a child of the leaf node. The below steps are followed while we try to insert a node into a binary search
tree:
➢ Check the value to be inserted (say X) with the value of the current node (say val) we are in:
▪ If X is less than val move to the left subtree.
▪ Otherwise, move to the right subtree.
➢ Once the leaf node is reached, insert X to its right or left based on the relation between X and the leaf
node’s value.
Example:
Deletion in Binary Search Tree
Just remove / disconnect the leaf node that is to deleted from the tree.
Example-
Consider the following example where node with value = 20 is deleted from the BST-
Case-02: Deletion Of A Node Having Only One Child-
Make the child of the deleting node, the child of its grandparent.
Example-
Consider the following example where node with value = 30 is deleted from the BST-
Case-03: Deletion Of A Node Having Two Children-
Method-01:
Consider the following example where node with value = 15 is deleted from the BST-
Method-02:
Consider the following example where node with value = 15 is deleted from the BST-
Search Operation
Search Operation is performed to search a particular element in the Binary Search Tree.
In worst case,the binary search tree is a skewed In best case,the binary search tree is a balanced
binary search tree. binary search tree.
Height of the binary search tree becomes n. Height of the binary search tree becomes log(n).
So, Time complexity of BST Operations = O(n). So, Time complexity of BST Operations = O(logn).