Download presentation
Presentation is loading. Please wait.
1
Binary Search Trees
2
Binary search trees A binary search tree is a binary tree where all elements in the left subtree are less than elements in the right subtree 2
3
Binary search trees A binary search tree is a binary tree where all elements in the left subtree are less than elements in the right subtree As we saw earlier, inorder traversal is a b c d e f g h i j which is in sorted order So this is a binary search tree 3
4
Binary search trees A binary search tree is a binary tree whose inorder traversal is in sorted order As we saw earlier, inorder traversal is a b c d e f g h i j which is in sorted order So this is a binary search tree There are many more (binary) search trees that have inorder traversal a b c d e f g h i j 4
5
Binary search trees A binary search tree is a binary tree whose inorder traversal is in sorted order As we saw earlier, inorder traversal is a b c d e f g h i j which is in sorted order So this is a binary search tree All entries are unique! Typical use … representing a set 5
7
demo
8
Implementing search in a binary search tree
Can implement binary search in O(log n) time on average Takes longer if the tree is badly balanced For every node X, value in all nodes in left subtree of X are less than value in X, and value of all nodes in right subtree are greater than value in X Algorithm is simple: if x < node then search left subtree if x > node then search right subtree When the tree is balanced the path length to the leaves is log(n) 8
9
Find 17
10
Find 17
11
Find 17
12
Find 17
13
Find 17
14
Find 17 Fails to find 17
15
Insert New Node Inserting a new node This method works whether we are using the tree to implement a set, sequence etc. as long as it is an injective binary search tree. Inserting a new node in general we must: Add to bottom of tree at all times (add a leaf) Keep the search tree property (left less than right) 15
17
Insert 40
18
Insert 40
19
Insert 40
20
Insert 40
21
Insert 40
22
Another illustration of insertion …
23
Adding a node with value x = 5. (Example)
Insert New Node Adding a node with value x = 5. (Example) 6 3 2 4 23
24
Adding a node with value x = 5. (Example)
Insert New Node Adding a node with value x = 5. (Example) 6 3 2 4 Compare 5 with node: 5 > 3 so go right 24
25
Adding a node with value x = 5. (Example)
Insert New Node Adding a node with value x = 5. (Example) 6 3 2 4 Compare 5 with node: 5 < 6 so go left 25
26
Adding a node with value x = 5. (Example)
Insert New Node Adding a node with value x = 5. (Example) 6 3 2 4 Compare 5 with node: 5 > 4 so go right 26
27
Adding a node with value x = 5. (Example)
Insert New Node Adding a node with value x = 5. (Example) 3 2 6 4 Can’t go right as that is null Insert node right of here, as a leaf 27
28
Adding a node with value x = 5. (Example)
Insert New Node Adding a node with value x = 5. (Example) 3 2 6 4 5 Can’t go right as that is null Insert node right of here, as a leaf 28
29
Why bother?
31
n=3, log(n+1)-1 = 1 ≤ height ≤ n-1
32
n=7, log(n+1)-1 = 2 ≤ height ≤ n-1
33
n=15, log(n+1)-1 = 3 ≤ height ≤ n-1
34
n=31, log(n+1)-1 = 4 ≤ height ≤ n-1
35
n=63, log(n+1)-1 = 5 ≤ height ≤ n-1
36
n=127, log(n+1)-1 = 6 ≤ height ≤ n-1
37
n=255, log(n+1)-1 = 7 ≤ height ≤ n-1
38
And this is crucial height (h) log2(n+1)-1 ≤ h ≤ n-1 If we get it right we can access data in logarithmic time!
39
Log to the base 2 of … 2 = 1 4 = 2 8 = 3 16 = 4 … 1024 = 10
40
Log to the base 2 of … 2 = 1 4 = 2 8 = 3 16 = 4 … 1024 = 10 Brilliant!
41
java implementation
42
Node
43
Node
44
Node
45
Node Default constructor
46
Node parameterised constructor note use of this.
47
Node getters
48
Node setters
49
Node predicates
50
Node For printing
51
The BSTree class BinarySearchTree (BSTree)
52
Your mission, should you choose to accept it …
53
Implement insert, find and delete …
54
BSTree
55
BSTree A binary search tree has a root where the root is a node It also has a size, where size is the number of nodes in the tree
56
BSTree Default constructor, an empty tree with an empty root
57
BSTree Get the root Test if tree is empty Get the size of the tree
58
BSTree Insert string s into the tree
59
BSTree Insert string s into the tree
60
BSTree Insert string s into the subtree rooted on the current node
61
BSTree If s is in the left subtree …
62
BSTree If s is in the left subtree and we need to create a new node
63
BSTree If s is in the right subtree …
64
BSTree If s is in the right subtree and we need to create a new node
65
finding a node
66
finding a node BSTree
67
finding a node BSTree The string s is present in the tree if the tree isn’t empty and we can find a node that contains s
68
finding a node BSTree find string s in the subtree rooted on the current node
69
finding a node BSTree If s is equal to the data in the current node then return the current node as a result.
70
finding a node BSTree If s is less than the data in the current node and the current node has a left child then try and find s in the subtree rooted on the left child
71
finding a node BSTree If s is greater than the data in the current node and the current node has a right child then try and find s in the subtree rooted on the right child
72
finding a node BSTree s is less than the current node and it has no left child or s is greater than the current node and it has no right child … deliver null!
73
Deletion of a node
74
Deletion of a node 16 11 30 7 13 20 40 18 25
75
Delete 7 Deletion of a node 16 11 30 7 13 20 40 18 25
76
Delete 7 Deletion of a node 16 11 30 7 13 20 40 18 25
77
Delete 7 Deletion of a node 16 11 30 13 20 40 18 25
78
Delete 11 Deletion of a node 16 11 30 13 20 40 18 25
79
Delete 11 Deletion of a node 16 11 30 13 20 40 18 25
80
Delete 11 Deletion of a node 16 11 30 13 20 40 Copy contents 18 25
81
Delete 11 Deletion of a node 16 13 30 13 20 40 Copy contents 18 25
82
Delete 11 Deletion of a node 16 13 30 13 20 40 Copy contents 18 25
83
Delete 11 Deletion of a node 16 13 30 13 20 40 Delete leaf 18 25
84
Delete 11 Deletion of a node 16 13 30 20 40 18 25
85
Delete 16 Deletion of a node 16 13 30 20 40 18 25
86
Delete 16 Deletion of a node 16 13 30 20 40 18 25
87
Deletion of a node Delete 16 16 13 30 20 40 18 25
Find the smallest node in the right subtree
88
Deletion of a node Delete 16 16 13 30 20 40 18 25
Find the smallest node in the right subtree
89
Deletion of a node Delete 16 16 13 30 20 40 18 25
Find the smallest node in the right subtree
90
Delete 16 Deletion of a node 16 13 30 20 40 18 25 Copy contents
91
Delete 16 Deletion of a node 18 13 30 20 40 18 25 Copy contents
92
Delete 16 Deletion of a node 18 13 30 20 40 18 25 Delete leaf
93
Delete 16 Deletion of a node 18 13 30 20 40 25 Delete leaf
94
Deletion of a node Delete 16 18 13 30 20 40 25
This process maintains the inorder property
95
There is a symmetric equivalent: find largest node in left branch …
Delete 16 Deletion of a node 18 13 30 20 40 25 This process maintains the inorder property There is a symmetric equivalent: find largest node in left branch …
96
Deletion of a node Delete 16 16 13 30 20 40 18 25
Find the largest node in the left subtree
97
Deletion of a node Delete 16 16 13 30 20 40 18 25
Find the largest node in the left subtree
98
Delete 16 Deletion of a node 13 13 30 20 40 18 25 Copy contents
99
Delete 16 Deletion of a node 13 13 30 20 40 18 25 Delete leaf
100
Delete 16 Deletion of a node 13 30 20 40 18 25 Delete leaf
101
Deletion of a node Delete 16 40 13 20 30 25 18
Therefore we have 2 options resulting in two different trees, both valid 40 18 20 30 13 25
102
Well baby, is there an algorithm for this?
Deletion of a node Well baby, is there an algorithm for this?
103
Deletion of a node
104
Deletion of a non-internal node (a node with less than 2 children)
Non-internal – less than 2 children 6 cases to consider
105
Deletion of a non-internal node
Non-internal – less than 2 children Actually only 4 cases to consider
106
Deletion of a non-internal node
Assuming we have already found the node to delete
107
Deletion of a non-internal node
subsumed Assuming we have already found the node to delete
108
Deletion of a non-internal node
109
Deletion of a non-internal node
110
Deletion of a non-internal node
111
Deletion of a non-internal node
112
Deletion of a non-internal node
113
Deletion of a non-internal node
114
Deletion of a non-internal node
115
Deletion of a non-internal node
117
Deletion of an internal node
Only 2 cases to consider
118
Deletion of an internal node
Internal – 2 children Actually only 1 case to consider
119
Deletion of an internal node
get the node, call it w, to the right of this node
120
Deletion of an internal node
get the node, call it w, to the right of this node w
121
Deletion of an internal node
get the node, call it w, to the right of this node get the “smallest” node, call it y, in the subtree rooted at w w
122
Deletion of an internal node
get the node, call it w, to the right of this node get the “smallest” node, call it y, in the subtree rooted at w w y
123
Deletion of an internal node
get the node, call it w, to the right of this node get the “smallest” node, call it y, in the subtree rooted at w w y
124
Deletion of an internal node
get the node, call it w, to the right of this node get the “smallest” node, call it y, in the subtree rooted at w replace what’s in node with what’s in y w y
125
Deletion of an internal node
get the node, call it w, to the right of this node get the “smallest” node, call it y, in the subtree rooted at w replace what’s in node with what’s in y y w y
126
Deletion of an internal node
get the node, call it w, to the right of this node get the “smallest” node, call it y, in the subtree rooted at w replace what’s in node with what’s in y node y is not-internal, delete it (as before) y w y
127
Deletion of an internal node
get the node, call it w, to the right of this node get the “smallest” node, call it y, in the subtree rooted at w replace what’s in node with what’s in y node y is not-internal, delete it (as before) y w y
128
Deletion of an internal node
get the node, call it w, to the right of this node get the “smallest” node, call it y, in the subtree rooted at w replace what’s in node with what’s in y node y is not-internal, delete it (as before)
129
Groovey. But what if the node is the root?
Deletion of a node Groovey. But what if the node is the root?
130
Deletion of the root Only 4 cases to consider
131
Deletion of the root Only 4 cases to consider … or is there?
132
Deletion of the root
133
Deletion of the root
134
Deletion of the root
135
Deletion of the root
136
Deletion of the root
137
Finding the node to delete
Deletion of a node As before …
138
All of the code for deletion
139
Deletion of a node All of the code
141
Is there a demo?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.