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

announcements
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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)

slide-2
SLIDE 2

Binary Search Tree - Remove

void dict<K>::remove (Node *& croot, const K & k){ if (croot != NULL){ if (croot->key == key) doRemoval(cRoot); else if (k < croot->key) remove(cRoot->left, k); else remove(cRoot->right, k); } }

13 13 38 38 51 51 40 40 84 84 10 10 25 25 12 12 37 37 89 89 66 66 95 95

1 2 3 4 5 6 7 8 void dict<K>::doRemoval(Node * & cRoot) { if ((croot->left == NULL) && ((croot->right == NULL){ _____ChildRemove(cRoot); else if ((croot->left != NULL) && ((croot->right != NULL){ _____ChildRemove(cRoot); else _____ChildRemove(cRoot); }

slide-3
SLIDE 3

Binary Search Tree - Remove

void dict<K>::remove (Node *& croot, const K & k){ if (croot != NULL){ if (croot->key == key) doRemoval(cRoot); else if (k < croot->key) remove(cRoot->left, k); else remove(cRoot->right, k); } }

13 13 38 38 51 51 40 40 84 84 10 10 25 25 12 12 37 37 89 89 66 66 95 95

void dict<K>::doRemoval(Node * & cRoot) { if ((croot->left == NULL) && ((croot->right == NULL){ _____ChildRemove(cRoot); else if ((croot->left != NULL) && ((croot->right != NULL){ _____ChildRemove(cRoot); else _____ChildRemove(cRoot); } 1 2 3 4 5 void dict<K>::noChildRemove(Node * & cRoot) { Node * temp = cRoot; cRoot = NULL; delete temp; } 1 2 3 4 5 6 void dict<K>::oneChildRemove(Node * & cRoot) { Node * temp = cRoot; if (cRoot->left == NULL) cRoot = cRoot->right; else cRoot = cRoot->left; delete temp; }

slide-4
SLIDE 4

Binary Search Tree - Remove

void dict<K>::remove (Node *& croot, const K & k){ if (croot != NULL){ if (croot->key == key) doRemoval(cRoot); else if (k < croot->key) remove(cRoot->left, k); else remove(cRoot->right, k); } }

13 13 38 38 51 51 40 40 84 84 10 10 25 25 12 12 37 37 89 89 66 66 95 95

void dict<K>::doRemoval(Node * & cRoot) { if ((croot->left == NULL) && ((croot->right == NULL){ _____ChildRemove(cRoot); else if ((croot->left != NULL) && ((croot->right != NULL){ _____ChildRemove(cRoot); else _____ChildRemove(cRoot); } void dict<K>::noChildRemove(Node * & cRoot) { Node * temp = cRoot; cRoot = NULL; delete temp; } void dict<K>::oneChildRemove(Node * & cRoot) { Node * temp = cRoot; if (cRoot->left == NULL) cRoot = cRoot->right; else cRoot = cRoot->left; delete temp; } 1 2 3 4 5 void dict<K>::twoChildRemove(Node * & cRoot) { Node * iop = rightMostChild(cRoot->left); cRoot->key = iop->key; doRemoval(iop); }

slide-5
SLIDE 5

Binary Search Tree - Remove

void dict<K>::remove (Node *& croot, const K & k){ if (croot != NULL){ if (croot->key == key) doRemoval(cRoot); else if (k < croot->key) remove(cRoot->left, k); else remove(cRoot->right, k); } }

13 13 38 38 51 51 40 40 84 84 10 10 25 25 12 12 37 37 89 89 66 66 95 95

void dict<K>::doRemoval(Node * & cRoot) { if ((croot->left == NULL) && ((croot->right == NULL){ _____ChildRemove(cRoot); else if ((croot->left != NULL) && ((croot->right != NULL){ _____ChildRemove(cRoot); else _____ChildRemove(cRoot); } void dict<K>::noChildRemove(Node * & cRoot) { Node * temp = cRoot; cRoot = NULL; delete temp; } void dict<K>::oneChildRemove(Node * & cRoot) { Node * temp = cRoot; if (cRoot->left == NULL) cRoot = cRoot->right; else cRoot = cRoot->left; delete temp; } void dict<K>::twoChildRemove(Node * & cRoot) { Node * iop = rightMostChild(cRoot->left); cRoot->key = iop->key; doRemoval(iop); } 1 2 3 4 Node * & dict<K>::rightMostChild(Node * & cRoot) { if (cRoot->right == NULL) return cRoot; else return rightMostChild(cRoot->right); }

slide-6
SLIDE 6

Tying things together…

___________

1 2 3 4 5 6 7

slide-7
SLIDE 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?

slide-8
SLIDE 8

Binary Tree -

Reminder: height(T) is:

  • ______ if T is empty
  • 1 + max{height(TL), height(TR)}, otherwise

The algorithms on BST depend on the height (h) of the tree. The analysis should be in terms of the amt of data (n) the tree contains. So we need relationships between h and n. h ≥ f(n) h ≤ g(n)

13 13 38 38 51 51 40 40 84 84 10 10 25 25 12 12 37 37 89 89 66 66 95 95

slide-9
SLIDE 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?

slide-10
SLIDE 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

  • r bad?
slide-11
SLIDE 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 _____________.

  • peration

avg case worst case sorted array sorted list find insert delete traverse

slide-12
SLIDE 12

something new… which tree makes you happiest?

50 70 90 50 70 90

The “height balance” of a tree T is: b = height(TR) - height(TL) A tree T is “height balanced” if: