Design and Analysis of Algorithms BITS Pilani Campus
Design and Analysis of Algorithms BITS Pilani Campus Dr. Maheswari Karthikeyan 09/03/2013, Lecture 7
BITS Pilani Campus § Binary Search Tree
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. BITS Pilani, Pilani Campus
BINARY SEARCH TREE Tree representation: – A linked data structure in which each node is an object parent Node representation: L R key data – Key field – Satellite data – Left: pointer to left child Right child Left child – Right: pointer to right child – p: pointer to parent (p [root [T]] = NIL) Satisfies the binary-search-tree property BITS Pilani, Pilani Campus
BINARY SEARCH TREE BITS Pilani, Pilani Campus
BITS Pilani, Pilani Campus
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} BITS Pilani, Pilani Campus
BITS Pilani, Pilani Campus
BITS Pilani, Pilani Campus
BITS Pilani, Pilani Campus
Searching for a Key Given a pointer to the root of a tree and a key k: 5 – Return a pointer to a node with key k if one exists – Otherwise return NIL 3 2 7 4 9 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 BITS Pilani, Pilani Campus
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 ) BITS Pilani, Pilani Campus
BITS Pilani, Pilani Campus
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 BITS Pilani, Pilani Campus
MINIMUM TREE-MINIMUM(x) while left[x] ≠ NIL do x left[x] return x BITS Pilani, Pilani Campus
MAXIMUM TREE-MAXIMUM(x) while right[x] ≠ NIL do x right[x] return x BITS Pilani, Pilani Campus
Successor Def: successor (x ) = y, such that key [y] is the 15 smallest key > key [x] E. g. : successor (15) =17 6 successor (13) =15 3 7 17 successor (9) =13 2 4 13 18 20 9 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 BITS Pilani, Pilani Campus
Finding the Successor Alg: TREE-SUCCESSOR(x) 1. 2. 3. 4. 5. 6. 7. if right [x] NIL then return TREE-MINIMUM(right [x]) y ← p[x] while y NIL and x = right [y] 6 do x ← y 3 y ← p[y] 2 4 return y 15 y 7 17 13 18 x 20 9 Running time: O (h), h – height of the tree BITS Pilani, Pilani Campus
SUCCESSOR BITS Pilani, Pilani Campus
Predecessor Def: predecessor (x ) = y, such that key [y] is the 15 biggest key < key [x] E. g. : predecessor (15) predecessor (9) predecessor (13) =13 =7 =9 6 3 2 18 7 17 4 20 13 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): smallest element x is the BITS Pilani, Pilani Campus
Insertion Goal: – Insert value v into a binary search tree Idea: Insert value 13 – If key [x] < v move to the right child of x, else move to the left child of x 12 – When x is NIL, we found the correct position 5 – If v < key [y] insert the new node as y’s left child 2 else insert it as y’s right child 1 18 9 15 3 13 19 17 – 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” ) BITS Pilani, Pilani Campus
Example: TREE-INSERT Insert 13: x, y=NIL 12 y 12 x 5 2 1 18 9 15 3 5 19 18 2 1 17 9 15 3 17 12 12 5 2 1 x y 18 9 15 3 19 5 2 19 17 1 18 9 15 3 13 19 17 x = NIL y = 15 BITS Pilani, Pilani Campus
Alg: TREE-INSERT(T, z) 1. y ← NIL 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 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 12 5 2 1 18 9 15 3 13 19 17 Running time: O(h) BITS Pilani, Pilani Campus
INSERTION 15 18 10 12 05 1 7 16 20 11 TREE-INSERT(T, 17) BITS Pilani, Pilani Campus
INSERTION 15 18 10 12 05 1 7 11 16 20 17 TREE-INSERT(T, 17) BITS Pilani, Pilani Campus
INSERTION 15 18 10 12 05 1 7 16 20 11 TREE-INSERT(T, 4) BITS Pilani, Pilani Campus
INSERTION 15 18 10 12 05 1 7 16 20 11 TREE-INSERT(T, 14) BITS Pilani, Pilani Campus
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 15 5 16 3 12 10 13 18 delete 6 7 3 20 z 16 5 12 23 10 20 18 23 6 7 BITS Pilani, Pilani Campus
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 5 3 z 16 5 12 10 6 18 20 3 20 13 15 delete 12 18 23 10 23 6 7 7 BITS Pilani, Pilani Campus
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 delete 15 15 z 5 6 16 3 10 y 3 20 12 13 18 16 20 12 10 23 13 18 23 7 6 7 BITS Pilani, Pilani Campus
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 BITS Pilani, Pilani Campus
TREE-DELETE(T, z) 1. if left[z] = NIL or right[z] = NIL z has one child 2. then y ← z 3. else y ← TREE-SUCCESSOR(z) then x ← left[y] 6. else x ← right[y] 7. if x NIL 8. then p[x] ← p[y] y 15 4. if left[y] NIL 5. z has 2 children 5 16 3 x 20 12 10 13 18 23 6 7 BITS Pilani, Pilani Campus
TREE-DELETE(T, z) – cont. y 15 9. if p[y] = NIL 10. then root[T] ← x 11. else if y = left[p[y]] 16 5 3 12. then left[p[y]] ← x 13. else right[p[y]] ← x x 20 12 10 13 18 23 6 7 14. if y z 15. 16. then key[z] ← key[y] copy y’s satellite data into z 17. return y BITS Pilani, Pilani Campus
DELETION –case 1 15 18 10 12 05 1 7 16 20 11 z TREE-DELETE(T, 11) BITS Pilani, Pilani Campus
DELETION -case 1 15 18 10 12 05 1 16 20 7 TREE-DELETE(T, 11) BITS Pilani, Pilani Campus
DELETION –case 2 15 18 10 12 05 1 7 z 16 11 TREE-DELETE(T, 18) BITS Pilani, Pilani Campus
DELETION –case 2 15 y 18 10 12 05 1 7 z 16 11 TREE-DELETE(T, 18) BITS Pilani, Pilani Campus
DELETION –case 2 15 y 18 10 12 05 1 7 16 z x 11 TREE-DELETE(T, 18) BITS Pilani, Pilani Campus
DELETION –case 2 15 y 18 10 12 05 1 7 16 z x 11 TREE-DELETE(T, 18) BITS Pilani, Pilani Campus
DELETION –case 2 15 y x 16 z 10 12 05 1 7 11 TREE-DELETE(T, 18) BITS Pilani, Pilani Campus
DELETION –case 3 15 z 18 10 12 05 1 7 16 20 11 TREE-DELETE(T, 10) BITS Pilani, Pilani Campus
DELETION –case 3 15 z 18 10 12 05 1 7 11 16 20 y TREE-DELETE(T, 18) BITS Pilani, Pilani Campus
DELETION –case 3 15 z 18 10 12 05 1 7 11 y 16 20 x = NIL TREE-DELETE(T, 18) BITS Pilani, Pilani Campus
DELETION –case 3 y 11 15 z 18 10 12 05 1 16 20 7 TREE-DELETE(T, 18) BITS Pilani, Pilani Campus
DELETION –case 3 15 18 11 12 05 1 7 16 20 TREE-DELETE(T, 18) BITS Pilani, Pilani Campus
DELETION 20 25 09 12 05 1 6 11 23 21 30 24 26 TREE-DELETE(T, 30) TREE-ELETE(T, 09) TREE-DELETE(T, 6) BITS Pilani, Pilani Campus
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 BITS Pilani, Pilani Campus
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 BITS Pilani, Pilani Campus
Suppose that we have numbers between 1 and 1000 in binary search tree and want to search for the number 363. Which of the following sequences could not be the sequence of nodes examined? a. 2, 252, 401, 398, 330, 344, 397, 363 b. 924, 220, 911, 244, 898, 258, 362, 363 c. 925, 202, 911, 240, 912, 245, 363 d. 2, 399, 387, 219, 266, 382, 381, 278, 363 e. 935, 278, 347, 621, 299, 392, 358, 363 BITS Pilani, Pilani Campus
Suppose that we have numbers between 1 and 1000 in binary search tree and want to search for the number 363. Which of the following sequences could not be the sequence of nodes examined? a. 2, 252, 401, 398, 330, 344, 397, 363 b. 924, 220, 911, 244, 898, 258, 362, 363 c. 925, 202, 911, 240, 912, 245, 363 d. 2, 399, 387, 219, 266, 382, 381, 278, 363 e. 935, 278, 347, 621, 299, 392, 358, 363 BITS Pilani, Pilani Campus
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 BITS Pilani, Pilani Campus
- Slides: 51