CPSC 221

BST Removal, and Why Balance Matters

Today

Announcements

  • Examlet 5 starts tomorrow: tree definitions and traversals
  • PA2 is due Monday 11/02


Today:

  • BST remove: the code
  • BST remove: the hard case
  • How tall is a BST?
  • Which tree makes you happiest?

Where were we?

Binary search tree — remove

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

Remember: find_ returns the pointer in the tree, by reference.

Node *& find_(const K & key, Node *& r) {
    if (r == nullptr) return r;
    else if (r->key == key) return r;
    else if (key < r->key) return find_(key, r->left);
    else return find_(key, r->right);
}
void remove(const K & key) {
    Node *& p = ______________________;

    if (_______________) {
        if (______________________________)
            twoChildRemove(p);
        else
            zeroOneChildRemove(p);
    }
}

BST — remove

38 13 51 10 25 40 84 12 37 66 89 95
void zeroOneChildRemove(Node *& p) {
    Node * temp = p;
    if (p->left == nullptr) p = p->right;
    else                    p = p->left;
    delete temp;
}

remove(37):   p is 25’s right

remove(89):   p is 84’s right

2-child remove

T.remove(38); How should we implement this?

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

How can you rearrange what’s left into a BST?   Describe as many ways as you can.

Watch:

Binary search tree — remove

38 13 51 10 25 40 84 12 37 66 89 95
void twoChildRemove(Node *& p) {
    Node *& iop = rightMost_(p->left);
    p->key  = iop->key;
    p->data = iop->data;
    zeroOneChildRemove(iop);
}
Node *& rightMost_(Node *& r) {
    if (r->right == nullptr) return r;
    else return rightMost_(r->right);
}

Recap

A dictionary is a structure supporting insert, find, and remove.

B_______ S_______ T_____ implementation of a dictionary incurs running time ________ for all of these.

dict<int> myT;
myT.insert(2);
myT.insert(7);
myT.insert(15);
myT.insert(22);
myT.insert(28);

How many “bad” \(n\)-item trees are there?

Binary search tree — height

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

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 amount of data (\(n\)) the tree contains.

So we need relationships between \(h\) and \(n\):

\(h \ge f(n)\)

\(h \le g(n)\)

Binary tree (theory moments)

#1

What is the max number of nodes in a tree of height \(h\)?

What is the min height (\(h\)) for a tree of \(n\) nodes?

#2

What is the max height (\(h\)) for a tree of \(n\) nodes?

Thus: lower bound on height ______, upper bound on height ______ — good news or bad (what’s the context?)

Your Turn

There are 7! = 5040 ways to insert the keys 1, 2, …, 7 into an empty BST.


How many of them build the perfect tree (height 2)?

How many build a stick (height 6)?

 

Guess what fraction for each.

All 5040

Average case!

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 \(\log n\) \(\log n\)
insert \(n\) \(n\)
delete \(n\) \(n\)
traverse \(n\) \(n\)

Foreshadowing Friday

50 70 90 70 50 90

Idea:   _____________________________________________________________

The “height balance” of a tree T is:    \(b = \text{height}(T_R) - \text{height}(T_L)\)

A tree T is “height balanced” if:

  •  
  •