28
CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

Embed Size (px)

Citation preview

Page 1: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

CS 253: Algorithms

Chapter 12

Binary Search Trees

Credit: Dr. George Bebis

Page 2: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

2

Binary Search Trees

Tree representation:◦ A linked data structure in

which each node is an object

Node representation:◦ Key field◦ Data◦ Left: pointer to left child◦ Right: pointer to right child◦ p: pointer to parent

(p [root [T]] = NIL)

Satisfies the binary-search-tree property (see the next slide)

Left child Right child

L Rparent

key data

Page 3: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

3

Binary Search Tree Property

If y is in left subtree of node x, then key [y] ≤ key [x]

If y is in right subtree of node x, then key [y] ≥ key [x]

2

3

4

5

7

9

Page 4: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

Binary Search Trees

Support many dynamic set operations ◦ SEARCH◦ INSERT, DELETE◦ MINIMUM, MAXIMUM◦ PREDECESSOR, SUCCESSOR

Running time of basic operations on binary search trees◦ On average: (lgn)

The expected height of the tree is lgn

◦ In the worst case: (n) The tree is a linear chain of n nodes

Page 5: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

5

Worst Case

Page 6: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

6

Traversing a Binary Search TreeInorder tree walk:

Root is printed between the values of its left and right subtrees:

left, root, right keys are printed in sorted order

Preorder tree walk: root printed first: root, left, right

Postorder tree walk: root printed last: left, right, root

2

3

4

5

7

9

Preorder: 5 3 2 4 7 9

Inorder: 2 3 4 5 7 9

Postorder: 2 4 3 9 7 5

Page 7: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

Traversing a Binary Search Tree

Alg: INORDER-TREE-WALK(x)1. if x NIL

2. then INORDER-TREE-WALK ( left [x] )

3. print key [x]

4. INORDER-TREE-WALK ( right [x] )

E.g.:

Running time: ◦ (n), where n is the size of the tree rooted at

x

2

3

4

5

7

9

Output: 2 3 4 5 7 9

Page 8: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

Facts It is possible to construct a unique Binary Search

Tree

given only PREORDER traversal of the BST

It is possible to construct a unique Binary Search Tree

given only POSTORDER traversal of the BST

It is not possible to construct a unique Binary Search Tree given only INORDER traversal of a BST

However

It is not possible to construct a unique Binary Tree given

only INORDER or only POSTORDER or only PREORDER traversal of a Binary Tree.

Page 9: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

Searching for a Key Given a pointer to the root of a tree and a key k:

Return a pointer to a node with key k if one exists

Otherwise return NIL

Start at the root; trace down a path

by comparing k with the key of the current node x: If the keys are equal: we have found the key

If k < key[x] search in the left subtree of x

If k > key[x] search in the right subtree of x

2

3

4

5

7

9

Page 10: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

10

Example: TREE-SEARCH

Search for key 13:

15 6 7 13

3

2 4

6

7

13

15

18

17 20

9

Page 11: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

Searching for a Key

Alg: TREE-SEARCH(x, k)

1. if x = NIL or k = key [x]2. then return x3. if k < key [x]4. then return TREE-SEARCH(left [x],

k )5. else return TREE-SEARCH(right [x], k

)Running Time:

O (h), h – the height of the tree

2

3

4

5

7

9

Page 12: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

Finding the Minimum in a Binary Search Tree

Goal: find the minimum value in a BST

Following left child pointers from the root, until a NIL is encountered

Alg: TREE-MINIMUM(x)

1. while left [x] NIL

2. do x ← left [x]

3. return x

Running time

O(h), h – height of tree

3

2 4

6

7

13

15

18

17 20

9

Minimum = 2

Page 13: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

13

3

2 4

6

7

13

15

18

17 20

9

Maximum = 20

Finding the Maximum in a Binary Search Tree

Goal: find the maximum value in a BST

Following right child pointers from the root, until a NIL is encountered

Alg: TREE-MAXIMUM(x)

1. while right [x] NIL

2. do x ← right [x]

3. return x

Running time

O(h), h – height of tree

Page 14: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

SuccessorDef: successor (x ) = y, such that key [y]

is the smallest key > key [x]

e.g.: successor (15) = successor (13) =

successor (9) =successor (20) =?

Case 1: right (x) is non emptysuccessor (x ) = the minimum in right (x)

Case 2: right (x) is empty◦ go up the tree until the current node is a left

child: successor (x ) is the parent of the current node

◦ if you cannot go further (and you reached the root): x is the largest element (no successor!)

3

2 4

6

7

13

15

18

17 20

9

171513

x

y

Page 15: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

Finding the SuccessorAlg: TREE-SUCCESSOR(x)1. if right [x] NIL % Case 1

2. then return TREE-MINIMUM(right [x])

3. y ← p[x] % Case 2 - y parent of x

4. while y NIL and x == right [y]

5. do x ← y

6. y ← p[y]

7. return y

Running time:

O (h), h – height of the tree

Exercise: if x=20, what does this algorithm return?

3

2 4

6

7

13

15

18

17 20

9

x

y

Page 16: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

PredecessorDef: predecessor (x ) = y such that

key [y] is the biggest key < key [x]

e.g.: predecessor (15) = predecessor (9) =

predecessor (7) =

Case 1: left (x) is non empty◦ predecessor (x ) = the maximum in left

(x)

Case 2: left (x) is empty◦ go up the tree until the current node is a

right child: predecessor (x ) is the parent of the current node

◦ if you cannot go further (and you reached the root): x is the smallest element

3

2 4

6

7

13

15

18

17 20

9

1376

xy

Page 17: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

13

InsertionGoal: Insert value v into a binary search

tree

Idea:If key [x] < v move to the right child of x

else move to the left child of xWhen x == NIL, we found the correct position If v<key [y] insert the new node as y’s left

child

else insert it as y’s right child

Begining at the root, go down the tree and maintain:

Pointer x : traces the downward path (current node)

Pointer y : parent of x (“trailing pointer” )

2

1 3

5

9

12

18

15 19

17

Insert value 13

Page 18: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

Example: TREE-INSERT

2

1 3

5

9

12

18

15 19

17

x=root[T], y=NILInsert 13:

2

1 3

5

9

12

18

15 19

17

x

2

1 3

5

9

12

18

15 19

17

x

x = NILy = 15

13

2

1 3

5

9

12

18

15 19

17

y

y

Page 19: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

Alg: TREE-INSERT(T, z)

1. y ← NIL2. x ← root [T]3. while x ≠ NIL4. do y ← x5. if key [z] < key [x]6. then x ← left [x]7. else x ← right [x]8. p[z] ← y9. if y = NIL10. then root [T] ← z % Tree T was

empty

11. else if key [z] < key [y]12. then left [y] ← z13. else right [y] ← z

2

1 3

5

9

12

18

15 19

1713

Running time: O(h)

Page 20: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

20

DeletionGoal: Delete a given node z from a binary

search tree

Case 1: z has no children Delete z by making the parent of z point to NIL

15

16

20

18 23

6

5

123

7

10 13

delete

15

16

20

18 23

6

5

123

7

10z

Page 21: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

21

Case 2: z has one child Delete z by making the parent of z point to z’s child,

instead of to z

And parent of z becomes the parent of z’s child.

15

16

20

18 23

6

5

123

7

10 13

delete 15

20

18 23

6

5

123

7

10

z

Deletion

Page 22: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

22

Case 3: z has two children z’s successor (y) is the minimum node in z’s right subtree y has either no children or one right child (but no left child) Delete y from the tree (via Case 1 or 2) Replace z’s key and satellite data with y’s.

15

16

20

18 23

6

5

123

7

10 13

delete z

y

15

16

20

18 23

7

6

123

10 13

6

Deletion

Page 23: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

TREE-DELETE(T, z)

1. if left[z] = NIL or right[z] = NIL

2. then y ← z % z has at most one child: Case 1 or 2

3. else y ← TREE-SUCCESSOR(z) % z has 2 children: Case 3

<y will be deleted>

4. if left[y] NIL

5. then x ← left[y]

6. else x ← right[y]

7. if x NIL

8. then p[x] ← p[y]

15

16

20

18 23

6

5

123

7

10 13

y

x

z

Page 24: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

TREE-DELETE(T, z) – cont.<Exercise: check the correctness

of the pointer movements

below>

9. if p[y] = NIL

10. then root[T] ← x

11. else if y = left[p[y]]

12. then left[p[y]] ← x

13. else right[p[y]] ← x

14. if y z

15. then key[z] ← key[y]

16. copy y’s satellite data into

z

17. return y

15

16

20

18 23

6

5

123

7

10 13

y

x

Running time: O(h) due to TREE-SUCCESSOR operation

Page 25: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

25

Binary Search Trees - SummaryOperations on binary search trees:

◦ SEARCH O(h)

◦ PREDECESSOR O(h)

◦ SUCCESSOR O(h)

◦ MINIMUM O(h)

◦ MAXIMUM O(h)

◦ INSERT O(h)

◦ DELETE O(h)

These operations are fast if the height of the tree is small

Theorem 12.4The expected height of a randomly built binary search tree on n distinct keys is O(lgn)

Page 26: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

26

ProblemsExercise 12.1-2

What is the difference between the MAX-HEAP property and the binary search tree property?

Can the min-heap property be used to print out the keys of an n-node tree in sorted order in O(n) time?A: No. (sorting can not be done in O(n))

Add’l exercise:Can you use the heap property to design an

efficient algorithm that searches for an item in a binary tree?A: no, it will be very inefficient! (why?)

Page 27: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

ProblemsLet x be the root node of a binary search tree (BST). Write an algorithm BSTHeight(x) that determines the height of the tree. What would be its running time?

Alg: BSTHeight(x) if (x==NULL) return -1; else return max(BSTHeight(left[x]), BSTHeight(right[x]))+1;

This program should not take more than O(n) time. Why?

Page 28: CS 253: Algorithms Chapter 12 Binary Search Trees Credit: Dr. George Bebis

Problems In a binary search tree, are the insert and delete

operations commutative?

Insert:◦ Start with only 10 in a BST and try to insert 4 followed by 6

Then change the order of insertions and try again

Delete◦ Delete 5 followed by 6 in the following tree◦ Then Delete 6 followed by 5

4

2 6

5 8

7

4

2 8

7

4

2 7

8