Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design and Analysis of Algorithms

Similar presentations


Presentation on theme: "Design and Analysis of Algorithms"— Presentation transcript:

1 Design and Analysis of Algorithms
Dr. Maheswari Karthikeyan 09/03/2013, Lecture 7

2 Binary Search Tree

3 BINARY SEARCH TREE It is a data structure that supports the dynamic-set operations including search, maximum, minimum, predecessor, successor, insert and delete. It can be used both as a dictionary and as a priority queue.

4 BINARY SEARCH TREE Tree representation:
Left child Right child L R parent key data Tree representation: A linked data structure in which each node is an object Node representation: Key field Satellite 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

5 BINARY SEARCH TREE

6

7 BST of different heights
Draw binary search trees of height 2,3,4,5 and 6 on the set of keys {1,4,5,10,16,17,21}

8

9

10

11 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 Idea Starting at the root: trace down a path by comparing k with the key of the current node: 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

12 TREE-SEARCH(x,k) if x = nil or k = key[x] then return x if k < key[x] then return TREE-SEARCH( left[x],k ) else return TREE-SEARCH( right[x],k )

13

14 ITERATIVE-TREE-SEARCH (x, k)
while x ≠ NIL and k ≠ key[x] do if k < key[x] then x left[x] else x right[x] return x

15 MINIMUM TREE-MINIMUM(x) while left[x] ≠ NIL do x left[x] return x

16 MAXIMUM TREE-MAXIMUM(x) while right[x] ≠ NIL do x right[x] return x

17 Successor Def: successor (x ) = y, such that key [y] is the smallest key > key [x] E.g.: successor (15) = successor (13) = successor (9) = 3 2 4 6 7 13 15 18 17 20 9 17 15 13 Case 1: right (x) is non empty successor (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

18 Finding the Successor Alg: TREE-SUCCESSOR(x) if right [x]  NIL
then return TREE-MINIMUM(right [x]) y ← p[x] while y  NIL and x = right [y] do x ← y y ← p[y] return y Running time: O (h), h – height of the tree 3 2 4 6 7 13 15 18 17 20 9 y x

19 SUCCESSOR

20 Predecessor Def: predecessor (x ) = y, such that key [y] is the biggest key < key [x] E.g.: predecessor (15) = predecessor (9) = predecessor (13) = 3 2 4 6 7 13 15 18 17 20 9 13 7 9 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

21 Insertion Goal: 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 x When x is 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” ) Insert value 13 2 1 3 5 9 12 18 15 19 17 13

22 Example: TREE-INSERT x, y=NIL y Insert 13: 2 1 3 5 9 12 18 15 19 17 2
x = NIL y = 15

23 Alg: TREE-INSERT(T, z) y ← NIL x ← root [T] while x ≠ NIL do y ← x
if key [z] < key [x] then x ← left [x] else x ← right [x] p[z] ← y if y = NIL then root [T] ← z else if key [z] < key [y] then left [y] ← z else right [y] ← z 2 1 3 5 9 12 18 15 19 17 13 Running time: O(h)

24 INSERTION 15 18 10 12 16 20 05 1 7 11 TREE-INSERT(T,17)

25 INSERTION 15 18 10 12 16 20 05 1 7 11 17 TREE-INSERT(T,17)

26 INSERTION 15 18 10 12 16 20 05 1 7 11 TREE-INSERT(T,4)

27 INSERTION 15 18 10 12 16 20 05 1 7 11 TREE-INSERT(T,14)

28 Deletion Goal: Delete a given node z from a binary search tree Idea:
Case 1: z has no children Delete z by making the parent of z point to NIL, instead of to z 15 16 20 18 23 6 5 12 3 7 10 13 delete 15 16 20 18 23 6 5 12 3 7 10 z

29 Deletion Case 2: z has one child
Delete z by making the parent of z point to z’s child, instead of to z Update the parent of z’s child to be z’s parent 15 16 20 18 23 6 5 12 3 7 10 13 delete 15 20 18 23 6 5 12 3 7 10 z

30 Deletion 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. 6 15 16 20 18 23 7 6 12 3 10 13 15 16 20 18 23 6 5 12 3 7 10 13 delete z y

31 Idea for TREE-DELETE(T, z)
Determine a node y that has to be deleted If z has only 1 child  y = z (case 2) If z has 2 children  y = TREE-SUCCESSOR(z) (case 3) In any case y has at most 1 child!!! Set a node x to the non-nil child of y Delete node y: set the parent of x to be the parent of y If the y is the root x becomes the new root otherwise, update parent pointers accordingly If the deleted node was the successor of z: move y’s key and satellite data onto z The deleted node y is returned for recycling

32 TREE-DELETE(T, z) if left[z] = NIL or right[z] = NIL then y ← z
else y ← TREE-SUCCESSOR(z) if left[y]  NIL then x ← left[y] else x ← right[y] if x  NIL then p[x] ← p[y] z has one child z has 2 children 15 16 20 18 23 6 5 12 3 7 10 13 y x

33 TREE-DELETE(T, z) – cont.
15 16 20 18 23 6 5 12 3 7 10 13 y if p[y] = NIL then root[T] ← x else if y = left[p[y]] then left[p[y]] ← x else right[p[y]] ← x if y  z then key[z] ← key[y] copy y’s satellite data into z return y x

34 DELETION –case 1 15 18 10 12 16 20 05 1 7 11 z TREE-DELETE(T,11)

35 DELETION -case 1 15 18 10 12 16 20 05 1 7 TREE-DELETE(T,11)

36 DELETION –case 2 15 18 z 10 12 16 05 1 7 11 TREE-DELETE(T,18)

37 DELETION –case 2 15 y 18 z 10 12 16 05 1 7 11 TREE-DELETE(T,18)

38 DELETION –case 2 15 y 18 z 10 12 16 05 x 1 7 11 TREE-DELETE(T,18)

39 DELETION –case 2 15 y 18 z 10 12 16 05 x 1 7 11 TREE-DELETE(T,18)

40 DELETION –case 2 15 y x 16 z 10 12 05 1 7 11 TREE-DELETE(T,18)

41 DELETION –case 3 15 z 18 10 12 16 20 05 1 7 11 TREE-DELETE(T,10)

42 DELETION –case 3 15 z 18 10 12 16 20 05 1 7 11 y TREE-DELETE(T,18)

43 DELETION –case 3 15 z 18 10 12 16 20 05 1 7 11 y x = NIL
TREE-DELETE(T,18)

44 DELETION –case 3 y 15 11 z 18 10 12 16 20 05 1 7 TREE-DELETE(T,18)

45 DELETION –case 3 15 18 11 12 16 20 05 1 7 TREE-DELETE(T,18)

46 DELETION 20 25 09 12 23 30 05 1 6 11 26 21 24 TREE-DELETE(T,30)
TREE-ELETE(T,09) TREE-DELETE(T,6)

47 Suppose that we have numbers between 1 and 1000 stored in a binary search tree and we want to search for the number 259. Which of the following sequences could not be the sequence of keys examined? (a) 500, 300, 167, 290, 175, 259 (b) 999, 112, 602, 253, 411, 110, 259 (c) 19, 800, 310, 21, 176, 257, 258, 259

48 Suppose that we have numbers between 1 and 1000 stored in a binary search tree and we want to search for the number 259. Which of the following sequences could not be the sequence of keys examined? (a) 500, 300, 167, 290, 175, 259 (b) 999, 112, 602, 253, 411, 110, 259 (c) 19, 800, 310, 21, 176, 257, 258, 259

49 Suppose that we have numbers between 1 and in binary search tree and want to search for the number Which of the following sequences could not be the sequence of nodes examined? 2,252,401,398,330,344,397,363 924,220,911,244,898,258,362,363 925,202,911,240,912,245,363 2,399,387,219,266,382,381,278,363 935,278,347,621,299,392,358,363

50 Suppose that we have numbers between 1 and in binary search tree and want to search for the number Which of the following sequences could not be the sequence of nodes examined? 2,252,401,398,330,344,397,363 924,220,911,244,898,258,362,363 925,202,911,240,912,245,363 2,399,387,219,266,382,381,278,363 935,278,347,621,299,392,358,363

51 Binary Search Tree - Summary
Operations on binary search trees: SEARCH O(h) PREDECESSOR O(h) SUCCESOR 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 – otherwise their performance is similar to that of a linked list


Download ppt "Design and Analysis of Algorithms"

Similar presentations


Ads by Google