Search
Search
50
30 55
25 35 53 60
10
31 62
37
20 Search path for 37
10 20
2 12 18 25
11 14 23 28
23
18 30
10 20 27 32
6 15 50
23
18 30
10 20 27 32
6 19 50
50
30 55
25 53 60
35
10
31 62
37
20
Prints out keys in sorted order: 10, 20, 25, 30, 31, 35, 37, 50, 53, 55, 60, 62
search(12)
15
10 20
2 12 18 25
11 14 23 28
t
search(23)
15
10 20
2 12 18 25
11 14 23 28
t
Binary Search Tree
Search
TreeNode * BinaryTree::search(int key)
{
TreeNode *t = root;
while (t) {
if (t->data == key) break;
else if (t->data > key) t = t->left;
else t = t->right;
}
return t;
}
search(13)
15
10 20
2 12 18 25
11 14 23 28
t
Tree-Minimum
TreeNode *Tree_Minimum()
{ TreeNode * x=root;
While (x->left != null)
x= x->left;
return x
}
Tree Maximum
TreeNode *Tree_Maximum()
{ TreeNode* x=root;
While (x->right != null)
x= x->right;
return x
}
Successor and Predecessor
10, 20, 25, 30, 31, 35, 37, 50, 53, 55, 60, 62
x 50
30 55
25 35 53 60
10 The successor of x
31 62
37 is the leftmost node
in its right subtree
20
The predecessor of x is
the rightmost node in
its left subtree:
BST Operations Preview
25 25
Insert 8
10 37 10 37
15 30 65 8 15 30 65
The successor of 25
30
Delete 25
10 37
15 65
insert(19)
insert(13)
insert(4)
15
10 20
2 12 18 25
11 14 23 28
Binary Search Tree – Insert
void BinaryTree::insert(int data)
{
TreeNode *t1, *t2, *t3;
t1 = new TreeNode;
t1->left = NULL; t1->right = NULL;
t2 = root;
if (!root) root = t1;
else {
while (t2) {
t3 = t2;
if (t2->data > key) t2 = t2->left;
else t2 = t2->right;
}
if (t2->data > key) t3->left = t1;
else t3->right = t1;
}
}
insert(13)
15
10 20
2 12 18 25
11 14 23 28
1
3
Insertion
Example: insert z = 32 into the BST below
x 25
Compare 32 and 25
traverse the right subtree
20 35
12 40 insert 32 as
Compare 32 left child
and 35, y 25
traverse the 25
left subtree y
x 20 35
20 35
12 40 12 32 40
z
delete(12)
delete(14)
delete(2)
15
10 20
2 12 18 25
11 14 23 28
13
Delete a node from a BST
1. Locate the desired node by search; call it t
2. If t is a leaf, disconnect it from its parent
and set the pointer in the parent node equal
to NULL
3. If it has only one child then remove t from
the tree by making t’s parent point to its
child.
4. Otherwise, find the largest/smallest among
t’s LST/RST; call it p. Copy p’s information
into t. Delete p.
Deletion (A)
Case A: node x (to be deleted) is a leaf node
Operation: update the parent node to have an empty subtree.
25 25
15 40
15 40
20 30 45 45
20 30
17
x
Deletion (B)
Case B: node x has a left child but no right child
Operation: attach the left subtree of x to the parent
25 25
15 40
15 40
20 30 45 45
17 30
x
17
Deletion (C)
Case C: node x has a right child but no left child
Operation: attach the right subtree of x to the parent
25 25
x 15 40
20 40
20 30 45 45
17 30
17
Deletion (D)
Case D: node x has two children
Operation: 1. Select as the replacement (of x) node y, the
successor of x. y has the smallest value
greater than that of x.
2. Unlink y from the tree.
3. Connect y’s right subtree to its parent.
4. Finally, connect y at the deleted node.
r r
y
x
A A
B z B z
y
D C D
C
Deletion (D) - an Example
10, 25, 28, 30, 33, 34, 35, 40, 50, 65 10, 25, 28, 33, 34, 35, 40, 50, 65
40 40
30 33
65
65
35
25 35
25
50
50
10 28 28 34
33 10
replacement
of 30 34
Time Complexity
15
10 20
2 12 18 25
11 14 23 28
Search
Insert
Delete
insert(15) 15
insert(18) 18
insert(20)
insert(26) 20
insert(27)
26
insert(34)
27
34
Time Complexity
O(k) where k is the height
Height Balanced Trees
k = log (n)
Running Time
All basic operations on a binary search tree of height h run
in O(h) time.