announcements
play

Announcements PA2 available soon, due 02/__, 11:59p. Today: BST - PowerPoint PPT Presentation

Announcements PA2 available soon, due 02/__, 11:59p. Today: BST remove code AVL intro Reminder: A dictionary is a structure supporting the following: void insert(kType & k, dType & d) dType find(kType & k) void remove(kType


  1. Announcements – PA2 available soon, due 02/__, 11:59p. Today: BST remove code AVL intro Reminder: A dictionary is a structure supporting the following: void insert(kType & k, dType & d) dType find(kType & k) void remove(kType & k)

  2. Binary Search Tree - Remove 38 38 13 13 51 51 40 40 84 84 10 10 25 25 12 12 37 37 66 66 89 89 95 95 void dict<K>::remove (Node *& croot, const K & k){ if (croot != NULL){ void dict<K>::doRemoval(Node * & cRoot) { 1 if (croot->key == key) if ((croot->left == NULL) && ((croot->right == NULL){ 2 doRemoval(cRoot); _____ChildRemove(cRoot); 3 else if (k < croot->key) else if ((croot->left != NULL) && ((croot->right != NULL){ 4 remove(cRoot->left, k); _____ChildRemove(cRoot); 5 else else 6 remove(cRoot->right, k); _____ChildRemove(cRoot); 7 } } 8 }

  3. Binary Search Tree - Remove 38 38 13 13 51 51 40 40 84 84 10 10 25 25 12 12 37 37 66 66 89 89 void dict<K>::noChildRemove(Node * & cRoot) { 1 95 95 Node * temp = cRoot; 2 void dict<K>::remove cRoot = NULL; 3 (Node *& croot, const K & k){ delete temp; 4 if (croot != NULL){ void dict<K>::doRemoval(Node * & cRoot) { } 5 if (croot->key == key) if ((croot->left == NULL) && ((croot->right == NULL){ doRemoval(cRoot); _____ChildRemove(cRoot); void dict<K>::oneChildRemove(Node * & cRoot) { 1 else if (k < croot->key) else if ((croot->left != NULL) && ((croot->right != NULL){ Node * temp = cRoot; 2 remove(cRoot->left, k); _____ChildRemove(cRoot); if (cRoot->left == NULL) cRoot = cRoot->right; 3 else else else cRoot = cRoot->left; 4 remove(cRoot->right, k); _____ChildRemove(cRoot); delete temp; 5 } } } 6 }

  4. Binary Search Tree - Remove 38 38 13 13 51 51 40 40 84 84 10 10 25 25 12 12 37 37 66 66 89 89 void dict<K>::noChildRemove(Node * & cRoot) { 95 95 Node * temp = cRoot; void dict<K>::remove cRoot = NULL; void dict<K>::twoChildRemove(Node * & cRoot) { (Node *& croot, const K & k){ 1 delete temp; Node * iop = rightMostChild(cRoot->left); 2 if (croot != NULL){ void dict<K>::doRemoval(Node * & cRoot) { } cRoot->key = iop->key; if (croot->key == key) 3 if ((croot->left == NULL) && ((croot->right == NULL){ doRemoval(cRoot); doRemoval(iop); 4 _____ChildRemove(cRoot); void dict<K>::oneChildRemove(Node * & cRoot) { } else if (k < croot->key) 5 else if ((croot->left != NULL) && ((croot->right != NULL){ Node * temp = cRoot; remove(cRoot->left, k); _____ChildRemove(cRoot); if (cRoot->left == NULL) cRoot = cRoot->right; else else else cRoot = cRoot->left; remove(cRoot->right, k); _____ChildRemove(cRoot); delete temp; } } } }

  5. Binary Search Tree - Remove 38 38 13 13 51 51 40 40 84 84 10 10 25 25 12 12 37 37 66 66 89 89 void dict<K>::noChildRemove(Node * & cRoot) { 95 95 Node * temp = cRoot; void dict<K>::remove cRoot = NULL; void dict<K>::twoChildRemove(Node * & cRoot) { (Node *& croot, const K & k){ delete temp; Node * iop = rightMostChild(cRoot->left); if (croot != NULL){ void dict<K>::doRemoval(Node * & cRoot) { } cRoot->key = iop->key; if (croot->key == key) if ((croot->left == NULL) && ((croot->right == NULL){ doRemoval(cRoot); doRemoval(iop); Node * & dict<K>::rightMostChild(Node * & cRoot) { 1 _____ChildRemove(cRoot); void dict<K>::oneChildRemove(Node * & cRoot) { } else if (k < croot->key) if (cRoot->right == NULL) return cRoot; 2 else if ((croot->left != NULL) && ((croot->right != NULL){ Node * temp = cRoot; remove(cRoot->left, k); else return rightMostChild(cRoot->right); 3 _____ChildRemove(cRoot); if (cRoot->left == NULL) cRoot = cRoot->right; else } 4 else else cRoot = cRoot->left; remove(cRoot->right, k); _____ChildRemove(cRoot); delete temp; } } } }

  6. Tying things together… 0 1 2 3 4 5 6 7 ___________

  7. Binary Search Tree - miscellaneous characteristics and analysis dict<int> myT; myT.insert(2); myT.insert(7); myT.insert(15); myT.insert(22); myT.insert(28); … Give a sequence of inserts that result in a tree that looks like: How many “bad” n-item trees are there?

  8. Binary Tree - The algorithms on BST depend on the height (h) of the tree. 38 38 51 51 13 13 The analysis should be in terms of the 40 40 84 84 10 10 25 25 amt of data (n) the tree contains. 12 12 37 37 66 66 89 89 So we need relationships between h 95 95 and n. h ≥ f(n) Reminder: h ≤ g(n) height(T) is: • ______ if T is empty • 1 + max{height(T L ), height(T R )}, otherwise

  9. Binary Tree (theory moment #1) what is maximum number of nodes in a tree of height h? what is the least possible height (h) for a tree of n nodes?

  10. Binary Tree (theory moment #2) what is minimum number of nodes (n) in a tree of height h? what is the greatest possible height (h) for a tree of n nodes? thus: lower bd on ht ______, upper bd on ht _______, good news or bad?

  11. Binary Search Tree - The height of a BST depends on the order in which the data is inserted into it. ex. 1 3 2 4 5 7 6 vs. 4 2 3 6 7 1 5 How many different ways are there to insert n keys into a tree? Avg height, over all arrangements of n keys is _____________. operation avg case worst case sorted array sorted list find insert delete traverse

  12. something new… which tree makes you happiest? 50 70 70 50 90 90 The “height balance” of a tree T is: b = height(T R ) - height(T L ) A tree T is “height balanced” if: • •

Recommend


More recommend