Lesson 5 - Binary Search Tree (BST)

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 26

Algorithms

Binary Search Trees


(BST)
Binary Search Trees
Binary Search Tree (BST)
 a binary tree where:
all keys in the left subtree of a node are less
than or equal to the key of that node;
all keys in the right subtree of a node are
greater than or equal to the key of that node;
and
both the left and right subtrees are also
binary search trees.

01/19/22 2
Binary Search Trees
BST Property
Let x be a node in a binary search tree.
 If y is a node in the left subtree of x, then
key of y ≤ key of x.
 If y is a node in the right subtree of x,
then key of x ≤ key of y.

01/19/22 3
Binary Search Trees
Ex.
22

16 55

13 20 30 90

9 21 25 40

01/19/22 4
Binary Search Trees
Counter Example
17

16 55

13
! 20 30 90

9 21 25 40

Violated the BST property at node 17!

01/19/22 5
Binary Search Trees
 The best and worst case time complexity of most BST
operations is proportional to the height of the tree.
6 2

3 7 3

2 5 8 7

2 BSTs
The one on the left is more efficient than the one 5 8
on the right and both contain the same keys.

5
01/19/22 6
Binary Search Trees
TREE-SEARCH(T,key)
if T = nil return ‘not found’
else if key = Tdata return ‘found’
else if (key < Tdata) TREE-SEARCH(Tleft,key)
else TREE-SEARCH(Tright,key)

Worst case time complexity: O(h)


h – the height of the tree
6
Worst case time complexity: O(n)
Best case time complexity: (i)
n – the number of nodes
3 7

2 5 8
01/19/22 7
Binary Search Trees
Ex. 7

3 9

1 4 8 16

2 10

What will be returned by TREE-SEARCH(T,10)?


14
What will be returned by TREE-SEARCH(T,5)?

01/19/22 8
Binary Search Trees
TREE-INSERT(T,z)
// z - node to be inserted with left and right set to nil
y  nil; x  T
while x <> nil {
yx
if zdata < xdata x  xleft
else x  xright
}
if y = nil T  z
6
else if zdata < ydata yleft  z
else yright  z
3 7

2 5 8
01/19/22 9
Binary Search Trees
TREE-INSERT(T,z)
// z - node to be inserted with left and right set to nil
if T = nil T  z
else if zdata = Tdata return ‘duplicate’
else if (zdata < Tdata) TREE-INSERT(Tleft,z)
else TREE-INSERT(Tright,z)

Worst case time complexity: O(h)


h – the height of the tree
6
Worst case time complexity: O(n)
Best case time complexity: (log2n)
3 7
n – the number of nodes

2 5 8
01/19/22 10
Binary Search Trees
Ex. After inserting 13
12

5 18

2 9 15 19

13 17

01/19/22 11
Binary Search Trees
 Create a BST by inserting each of the
given values:
1. 6, 7, 30, 43, 28, 25, 10, 2, 1
2. I, B, C, G, Y, H, E, T, O

01/19/22 12
Binary Search Trees
TREE-DELETE(T,z)
// z - node to be deleted

Cases:
1. z has no children
6
2. z has one child
3. z has children 3 7

2 5 8

01/19/22 13
Binary Search Trees
Case 1: z has no children 15

Delete 13 5 16

3 12 20

z
10 13 18 23

6 Just remove 13!

7
01/19/22 14
Binary Search Trees
Result: 15

5 16

3 12 20

10 18 23

7
01/19/22 15
Binary Search Trees
Case 2: z has one child 15

Delete 16 5 16

3 12 20

10 13 18 23

6 Just let the parent of 16, which is 15,


point to the child of 16, which is 20.

7
01/19/22 16
Binary Search Trees
Result: 15

5 20

3 12 18 23

10 13

6 Resulting tree after removing 16.

7
01/19/22 17
Binary Search Trees
Case 3: z has children 15
z

Delete 5 5 16

3 12 20

Replace 5 with its 10 13 18 23


successor which is 6.
Remove 6 by connecting
10 and 7.
6

7
01/19/22 18
Binary Search Trees
Result:
15

6 16

3 12 20

10 13 18 23

7 Resulting tree after removing 5.

01/19/22 19
Binary Search Trees
Ex. Delete 25
12

5 18

3 6 15 30

2 7 17 28 43

1 10 25

01/19/22 20
Binary Search Trees
Ex. Delete 6
12

5 18

3 6 15 30

2 7 17 28 43

1 10

01/19/22 21
Binary Search Trees
Ex. Delete 12
12

5 18
Successor of 12

3 7 15 30

2 10 17 28 43

01/19/22 22
Binary Search Trees
Ex. Delete 12
15 Successor of 15

5 18

3 7 17 30

2 10 28 43

01/19/22 23
Binary Search Trees
TreeDelete(T,z)
if zleft = nil or zright = nil y  z
else y = TREE-SUCCESSOR(z)
if yleft <> nil x  yleft
else x  yright
if x <> nil xparent  yparent
if yparent = nil T  x
elseif y = yparentleft yparentleft  x
else yparentright  x
if y <> z {
zdata  ydata; copy y’s satellite data into z
}
return y

Worst case time complexity: O(h)


h – the height of the tree

01/19/22 24
Binary Search Trees
Binary search trees can be used:
1. as a dictionary
2. in databases
3. as a priority queue

01/19/22 25
Binary Search Trees
A. Create a BST by performing the following
operations :
1. INSERT : 50, 9, 16, 3, 10, 14, 1, 4, 2, 8, 30,
70, 55, 25, 15, 20
DELETE : Root, 10, Root
2. Insert : M, K , L , O, P , A, H, R, B, C, G, E, S,
D, T
DELETE : M, H, ROOT, ROOT

B. Based on your final answer for problems a1 and


a2, determine the preorder, inorder and
postorder.
01/19/22 26

You might also like