definitions
play

Definitions Binary Search Tree: n Tree: hierarchical structure made - PDF document

Advanced Programming Binary Search Tree Binary Search Tree Introduction Binary Search Tree, o BST implement dictionaries or ordered queues elements stored are ordered according to a key Efficient (log n) operations S EARCH , M INIMUM , M


  1. Advanced Programming Binary Search Tree Binary Search Tree Introduction Binary Search Tree, o BST implement dictionaries or ordered queues – elements stored are ordered according to a key Efficient (log n) operations S EARCH , M INIMUM , M AXIMUM , P REDECESSOR , S UCCESSOR , I NSERT , D ELETE . 2 1

  2. Advanced Programming Binary Search Tree Definitions Binary Search Tree: n Tree: hierarchical structure made of nodes with father-son relations. One node has no father (root) and is unique. The path from the root to any node is unique n Binary: each node has at most 2 sons ( left e right ) and only one father (p) – except the root that has no father n Search: nodes have a key field and are ordered according to it. 3 Ordering relations (I) For each node x : n For all nodes y in left subtree of x, key[y] ≤ key[x] n For all nodes y in right subtree of x, key[y] ≥ key[x] 4 2

  3. Advanced Programming Binary Search Tree Ordering relations (II) x ≤ x ≥ x 5 Ex I This tree is a BST 5 3 7 2 5 8 6 3

  4. Advanced Programming Binary Search Tree Ex BST 2 3 7 5 8 5 7 Ex NO BST 2 3 7 9 8 5 8 4

  5. Advanced Programming Binary Search Tree Balanced BST A BST is balanced if, for each node, the height of its left subtree = the height of its right subtree +- 1 9 Height of a tree The height of a tree is the number of levels below the root node. h = 0: only root node h = 1: root node and at least a child h = 2: root, children and grandchild … In a balanced tree: h = ⎣ log 2 (n) ⎦ 10 5

  6. Advanced Programming Binary Search Tree Ex Balanced BST 5 3 7 2 5 8 11 Ex II Not Balanced BST 2 3 7 6 8 5 12 6

  7. Advanced Programming Binary Search Tree Complexity Operations have complexity proportional to the depth h of the tree For a balanced tree (n nodes) complexity = Θ (log n) For a completely unbalanced tree, the worst case, complexity = O(n). Average case Θ (log n) 13 Log vs. Log 2 Complexity proportional to depth of tree ( h ) Since h ~ log 2 ( n ) then complexity should be Θ ( log 2 (n) ) log b (x) log a (x) = But, remember that: log b (a) constant So Θ ( log 2 (n) ) ⟺ Θ ( log(n) ) 14 7

  8. Advanced Programming Binary Search Tree Traversals Going through all nodes can be done in 3 ways n Preorder: node, subtree left, subtree right n Inorder: subtree left, node, subtree right n Postorder: subtree left, subtree right, node 15 Preorder Preorder-Tree-Walk(x) 1 if x ≠ NIL 2 then print key[x] 3 Preorder-Tree-Walk(left[x]) 4 Preorder-Tree-Walk(right[x]) 16 8

  9. Advanced Programming Binary Search Tree Inorder 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]) 17 Postorder Postorder-Tree-Walk(x) 1 if x ≠ NIL 2 then Postorder-Tree-Walk(left[x]) 3 Postorder-Tree-Walk(right[x]) 4 print key[x] 18 9

  10. Advanced Programming Binary Search Tree Notes Inorder trasversal (see Inorder-Tree- Walk(root[T])) returns the elements in ascending order of key All traversal are Θ (n) (all nodes are passed once) 19 Ex. Trasversals? 15 6 18 3 7 17 20 2 4 13 9 20 10

  11. Advanced Programming Binary Search Tree Inorder 15 8 6 18 4 10 3 7 17 20 2 5 11 9 2 4 13 7 3 1 9 6 21 Preorder 1 15 2 9 6 18 3 6 3 7 17 20 10 11 7 2 4 13 4 5 8 9 22 11

  12. Advanced Programming Binary Search Tree Postorder 11 15 10 7 6 18 9 6 3 8 3 7 17 20 2 5 1 2 4 13 9 4 23 Search operations Search, Minimum/Maximum, Predecessor/ Successor. Are all O(h) 24 12

  13. Advanced Programming Binary Search Tree Tree-Search Tree-Search(x, k) 1 if x = NIL or k = key[x] 2 then return x 3 if k < key[x] 4 then return Tree-Search(left[x], k) 5 else return Tree-Search(right[x], k) 25 Ex 15 6 18 3 7 17 20 2 4 13 Tree-Search(13) 9 26 13

  14. Advanced Programming Binary Search Tree Tree-Search (iterative) Tree-Search-iterative(x, k) 1 while x ≠ NIL and k ≠ key[x] 2 do if k < key[x] 3 then x ← left[x] 4 else x ← right[x] 5 return x 27 Ex 15 6 18 3 7 17 20 2 4 13 Tree-Search-iterative(13) 9 28 14

  15. Advanced Programming Binary Search Tree Min / Max (iterative) Tree-Minimum(x) 15 15 6 6 18 18 1 while left[x] ≠ NIL 3 3 7 7 17 17 20 20 2 do x ← left[x] 2 2 4 4 13 13 3 return x 9 9 15 15 Tree-Maximum(x) 6 6 18 18 1 while right[x] ≠ NIL 3 3 7 7 17 17 20 20 2 do x ← right[x] 2 2 4 4 13 13 3 return x 9 9 29 Successor Given a node, find the closest - 2 cases: p[x] Min of right subtree x p[x] x First ancestor, of which the node is left heir 30 15

  16. Advanced Programming Binary Search Tree Successor Tree-Successor(x) 1 if right[x] ≠ NIL 2 then return Tree-Minimum(right[x]) 3 y ← p[x] 4 while y ≠ NIL and x = right[y] 5 do x ← y 6 y ← p[y] 7 return y 31 Ex 15 6 18 3 7 17 20 2 4 13 Tree-Successor (7) 9 32 16

  17. Advanced Programming Binary Search Tree Ex 15 6 18 3 7 17 20 2 4 13 Tree-Successor (7) 9 33 Ex 15 6 18 3 7 17 20 2 4 13 Tree-Successor (4) 9 34 17

  18. Advanced Programming Binary Search Tree Ex 15 6 18 3 7 17 20 2 4 13 Tree-Successor (4) 9 35 Ex 15 6 18 3 7 17 20 2 4 13 Tree-Successor (17) 9 36 18

  19. Advanced Programming Binary Search Tree Ex 15 6 18 3 7 17 20 2 4 13 Tree-Successor (4) 9 37 Predecessor Tree-Predecessor(x) 1 if left[x] ≠ NIL 2 then return Tree-Maximum(left[x]) 3 y ← p[x] 4 while y ≠ NIL and x = left[y] 5 do x ← y 6 y ← p[y] 7 return y 38 19

  20. Advanced Programming Binary Search Tree Insert Delete Must maintain the BST properties. 39 Insert To insert node z with key v: n Create node z with left[z]=right[z]=NIL n Search key[z] n Set pointers The new node is always a leaf 40 20

  21. Advanced Programming Binary Search Tree Tree-Insert (I) Tree-Insert(T, z) 1 y ← NIL search key[z] 2 x ← root[T] 3 while x ≠ NIL 4 do y ← x 5 if key[z]<key[x] 6 then x ← left[x] y 7 else x ← right[x] x=NIL z 41 Tree-Insert (II) insert z as son 8 p[z] ← y of y 9 if y = NIL 10 then root[T] ← z 11 else if key[z] < key[y] 12 then left[y] ← z 13 else right[y] ← z y x=NIL z 42 21

  22. Advanced Programming Binary Search Tree Ex 12 5 18 2 9 15 20 17 Tree-Insert (13) z 13 43 Ex 12 5 18 y 2 9 15 20 x 17 13 Tree-Insert (13) z 13 44 22

  23. Advanced Programming Binary Search Tree Ex 12 5 18 y 2 9 15 20 17 Tree-Insert (13) z 13 45 Delete Three cases, zero, 1, 2 sons 46 23

  24. Advanced Programming Binary Search Tree Delete: 0 sons 15 12 12 15 5 16 5 16 3 12 20 3 12 20 z 10 13 18 23 10 18 23 6 6 7 7 Just cancel z 47 Delete: 1 son 15 12 12 15 z 5 16 5 3 12 20 3 12 20 13 10 13 18 23 10 18 23 6 6 7 Son(z) becomes new 7 son of father(z) 48 24

  25. Advanced Programming Binary Search Tree Delete: 2 sons 12 15 12 15 z z 5 16 5 16 3 12 20 3 12 20 13 10 13 18 23 10 18 23 6 6 7 7 Copy succ (z) in position of z 49 12 y Tree-Delete (I) x Tree-Delete(T, z) 1 if left[z]=NIL or right[z]=NIL 2 then y ← z 3 else y ← Tree-Successor(z) 4 if left[y] ≠ NIL y: to be canceled 5 then x ← left[y] 6 else x ← right[y] x: unique son of y 50 25

  26. Advanced Programming Binary Search Tree 12 y Tree-Delete (II) x 7 if x ≠ NIL Update father(x) 8 then p[x] ← p[y] 9 if p[y] = NIL Y is root? x becomes root 10 then root[T] = x 11 else if y = left[p[y]] 12 then left[p[y]] ← x 13 else right[p[y]] ← x otherwise, link x to father(y) 51 Tree-Delete (III) 14 if y ≠ z 15 then key[z] ← key[y] 16 fields[z] ← fields[y] 17 return y If needed, copy info of successor in node to be canceled 52 26

  27. Advanced Programming Binary Search Tree Complexity For insert and cancel: O(h) If tree is balanced: O(log(n)) 53 Balancing The insert/cancel proposed do not guarantee to maintain the tree balanced. There are variants of BST that maintain the tree balanced when inserting and deleting nodes: Red-Black tree AA tree 54 27

  28. Advanced Programming Binary Search Tree Ex Build a BST with numbers 0 to 9 n Define an insert sequence that mantains the tree balanced n Define an insert sequence that mantains the tree unbalanced 55 Solution (I) 6 3 8 1 5 7 9 0 2 4 Insert(): 6, 3, 1, 0, 2, 5, 4, 8, 7, 9 56 28

  29. Advanced Programming Binary Search Tree Solution (II) 9 8 7 6 5 4 3 2 1 0 Insert() : 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 57 C: node typedef struct btnode *P_NODE; typedef struct btnode{ int key; P_NODE left, right; }NODE; 58 29

  30. Advanced Programming Binary Search Tree inorder void inorder( P_NODE head) { if( head == NULL) return; inorder( head->left); printf( "%d ", head->key); inorder( head->right); } 59 search P_NODE search( int key, P_NODE head) { if((head == NULL)!!(head->key == key)) return( head); else if( head->key < key) return head->right; else return head->left; } 60 30

Recommend


More recommend