2. Definition
Binary search tree is a binary tree that
satisfies the following constraint.
•The key value of left child is smaller than its
parent key
•the key value of right child is greater than its
parent.
5. Do by yourself
Insert the values 13, 3, 4,12, 14,
10, 5, 1, 8, 2, 7, 9,11, 6, 18 in
that order, starting from an
empty tree.
6. Deletion Operation
Case 1: Node to be deleted has no children
•simply delete the node.
Case 2: Node to be deleted has either left or right
empty subtree
•append nonempty subtree to its grand parent node
Case 3: Node to be deleted has both left and right
subtree
•Find the inorder successor node of the node to be
deleted
•Attach the right subtree of inorder successor node to its
grand parent
•Replace the node to be deleted by its inorder successor
7. 9
7
5
64 8 10
9
7
5
6 8 10
Case 1: removing a node with 2 EMPTY SUBTREES
parent
cursor
Removal in BST: Example
Removing 4
replace the link in the parent with
null
8. Case 2: removing a node with 2 SUBTREES
9
7
5
6 8 10
9
6
5
8 10
cursor
cursor
- replace the node's value with the max value in the left subtree
- delete the max node in the left subtree
44
Removing 7
Removal in BST: Example
What other element
can be used as
replacement?
9. 9
7
5
6 8 10
9
7
5
6 8 10
cursor
cursor
parent
parent
the node has no left child:
link the parent of the node to the right (non-empty) subtree
Case 3: removing a node with 1 EMPTY SUBTREE
Removal in BST: Example
10. 9
7
5
8 10
9
7
5
8 10
cursor
cursor
parent
parent
the node has no right child:
link the parent of the node to the left (non-empty) subtree
Case 4: removing a node with 1 EMPTY SUBTREE
Removing 5
4 4
Removal in BST: Example
12. Search Operation
1. Start at the root node
2. Branch either left or right repeatedly until the
element is found
3. if the search ends with empty subtree, the
element is not present in the tree.
30. ASSESSMENT
1.Which traversal technique traverses the
elements of binary search tree in ascending
order.
A. pre-order traversal
B. post-order traversal
C. in-order traversal
D. converse post-order traversal.
31. Contd..
2. The height of the binary tree in the best and
worst case is
A. n and log n respectively
B. log n and n respectively
C. log n and n/2 respectively
D. n/2 and log n respectively
32. Contd..
3. Create a binary search tree using the
following operations: insert 3, insert 7, insert
8, insert 1, insert 5, insert 0, insert 4, insert 6,
insert 9. Then, the left and right child of the
inorder successor of node 5 is
o0 and 6 respectively
o4 and null respectively
o3 and 9 respectively
o4 and 6 respectively
34. Contd..
5. The successor used in deletion operation of
binary search tree is
A. inorder successor
B. preorder successor
C. postorder successor
D. converse preorder successor