CS 61B | Lecture 10 | ADTs, Set, Map, Binary Search Tree (BST)


ADTs

An Abstract Data Type (ADT) is defined only by its operations, not by its implementation.

BST

Binary Search Trees

Basics of Tree

A tree consists of:

  • A set of nodes
  • A set of edges that connect nodes
    • Constraint: There is exactly one path (may contain several edges) between any two nodes.

In a rooted Rooted Tree, we call one node the root.

  • Every node N except the root has exactly one parent, defined as the first node on the path from N to the root.
  • A node with no child is called a leaf.
  • Binary Tree: Every node has either 0, 1, or 2 children (subtrees).

Definition (BST)

The BST is one of the most important ideas that you’ll ever learn in all of computer science.

Can think of the BST as representing a Set or even a Map.

Linked Lists are great, but it takes a long time to search for an item, even if the list is sorted! If the item is at the end of the list, that would take linear time.

Fundamental Problem: Slow search, even though it’s in order.

Definition: A binary search tree is a rooted binary tree with the BST property.

  • Every key in the left subtree is less than X’s key.
  • Every key in the right subtree is greater than X’s key.

Ordering

Ordering must be complete, transitive, and antisymmetric. Given keys $p$ and $q$:

  • Exactly one of $p \prec q$ and $q \prec p$ is true (not both).
  • $p \prec q$ and $q \prec r$ imply $p \prec r$. (transitive)

What about an example of not complete: Family tree. Compared to your parents, you are “less than”; but compared to your cousins, there is no answer and it’s not a complete thing (in different branches).

One consequence of these rules: No duplicate keys allowed!

It keeps things simple. Most real world implementations follow this rule.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private class BST<key> {
private Key key;
private BST left;
private BST right;

public BST(Key key, BST left, BST right) {
this.key = key;
this.left = left;
this.right = right;
}

public BST(Key key) {
this.key = key;
}
}

For a BST that is bushy (short and fat), we can search in $O(\log N)$ time where N is the number of nodes. For a BST that is spindly (tall and skinny), our search will take $O(N)$ time.

Operations

$\Theta(\log N)$, Height of the tree is $\sim \log N$

1
2
3
4
5
6
7
8
9
10
11
12
13
/* exist - return boolean */
public boolean exist(Key searchKey) {
int result = comp(searchKey, this.key);
if (result == 0) { // equal
return true;
} else if (result < 0 && left != null) { // go left
return left.search(searchKey);
} else if (result > 0 && right != null) { // go right
return right.search(searchKey);
} else {
return false;
}
}
1
2
3
4
5
6
7
8
9
10
public static BST find(BST T, Key sk) {
if (T == null) { return null; }
if (sk.equals(T.key)) {
return T;
} else if (sk < T.key) {
return find(T.left, sk);
} else {
return find(T.right, sk);
}
}

Insert

Search for key:

  • If found, do nothing
  • If not found:
    • Create new node
    • Set appropriate link

We always insert at a leaf node!

My Code (not good):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static void insert(BST T, Key sk) {
Key found = find(T, sk);
if (found == null) { /* not exist */
while (true) {
int result = comp(sk, T.key);
if (result < 0) { /* left */
if (T.left == null) {
T.left = new BST(sk);
break;
} else {
T = T.left;
}
} else { /* right (impossible to be equal) */
if (T.right == null) {
T.right = new BST(sk);
break;
} else {
T = T.right;
}
}
}
} /* if exist, do nothing */
}

Delete

3 Cases:

  • Case 1: No children

    Just sever the parent’s link, and the deleted node will be garbage collected.

  • Case 2: Has 1 child

    We can use recursion to avoid pointer backup.

    What about deleting the root?

  • Case 3: Has 2 children

    No. Moving bag is not a good idea.

    We can choose either predecessor "cat" or successor "elf".

    In other words, cat is the largest node in the left tree; elf is the smallest node in the right tree.

    • Delete cat or elf, and stick new copy in the root position: This deletion guaranteed to be either case 1 or 2 (no two children). Why?

    This method is known as Hibbard deletion.

    For example, delete dog and move elf to the root; since we need to delete elf either, eyes will be the left child of flat (case 1).

1
2
3
4
5
6
7
//      dog
// / \
// bag flat
// / \
// alf cat
// /
// abc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public static BST delete(BST T, Key dk) {
if (T == null) { return T; } /* T == null and base case */
if (dk < T.key) { /* T.key != dk */
T.left = delete(T.left, dk);
} else if (dk > T.key) {
T.right = delete(T.right, dk);
} else { /* T.key == dk */
if (T.left == null || T.right == null) { /* No Children & One Child */
if (T.left != null) { /* Has a left child */
T = T.left; /* connect left child to its parent */
} else { /* Has a right child or no child */
T = T.right; /* connect to right child or null */
}
} else { /* 2 Children */
// 1. find pred (largest node in the left subtree)
BST pred = T.left;
while (pred.right != null) { pred = pred.right; } // pred == alf
// 2. delete pred - it's very critical - this step connects the below elements - so we don't need to take care of the children of pred
T.left = delete(T.left, pred.key);
// 3. move pred to T (or exchange 2. and 3.)
T.key = pred.key; /* connect pred to its parent */
}
}
/* size */
if (T != null) {
T.size = 1 + size(T.left) + size(T.right);
}
/* ----- */
return T;
}

Lab 7 (BSTMap)

Lab 7: BSTMap.java & Map61B.java
Algs: BST.java

numberOfNodes(BSTMap b)$sim \Theta(n)$ (b has n nodes)

z is a positive integer.

What is the running time (in big O notation) of mystery(b, z)?

1
2
3
4
5
6
7
8
9
10
public Key mystery(BSTMap b, int z) {
if (z > numberOfNodes(b) || z <= 0)
return null;
if (numberOfNodes(b.left) == z-1)
return b.root.key;
else if (numberOfNodes(b.left) > z)
return mystery(b.left, z);
else
return mystery(b.right, z - numberOfNodes(b.left) - 1);
}

Ex (Discussion & Guide)

C Level

Give two orderings of the keys A X C S E R H that, when inserted into an empty BST, yield the best case height. Draw the tree.

In order: A C E H R S X

H - C - S - …
H - S - C - …

B Level

  1. Suppose that a certain BST has keys that are integers between 1 and 10, and we search for 5. Which sequence(s) of keys below are possible during the search for 5?

    Each time one level, no backward

    a. 10, 9, 8, 7, 6, 5 (o)
    b. 4, 10, 8, 7, 5 (o)
    c. 1, 10, 2, 9, 3, 8, 4, 7, 6, 5 (o) a chain
    d. 2, 7, 3, 8, 4, 5 (x) 8 > 7
    e. 1, 2, 10, 4, 8, 5 (o)

  2. Is the delete operation commutative? In other words, if we delete x, then y, do we always get the same tree as if we delete y, then x?

    Maybe yes! Couldn’t think of any case.

  3. Problem 1 from the Fall 2014 midterm #2.

    • Either 0 or 2 not-null children (complete tree) - $O(\log N)$
    • Either 0 or 2 children - $O(N)$
    • Either 0 or 1 child - $O(N)$

A+ Level

Problem 3 from the Fall 2009 midterm.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* The operation is [destructive], and creates no new nodes.
* In the [worst] case, it takes time [linear] in the total number
* of items in T and L */
public static IntTree mergeRight(IntTree T, IntTree L) {
T.left = mergeRight2(T.left, Integer.MAX_VALUE, L);
/* Why Integer.MAX_VALUE? Consider node 26 (always going right in T;
* there is no actual next boundary) */
}

/* Assuming T is a BST, and L is the sentinel of a right-leaning BST,
* return the result of inserting all items of L that are <= NEXT in T,
* removing them from L. */
private static IntTree mergeRight2(IntTree T, int next, IntTree L) {
if (L.left == null) { return T; } /* Done */
if (T == null) {
if (L.left.data <= next) { /* next is the boundary */
IntTree p = L.left;
L.left = L.left.right; /* Delete node */
/* continue inserting nodes from L, if possible */
p.right = mergeRight2(null, next, L); /* Why p.right? Because L is right-leaning */
return p; /* connect to T */
}
} else { /* T != null */
/* If go left, next will be T.data;
* if go right, next will be the previous next (not changing) */
if (L.left.data <= T.data) {
T.left = mergeRight2(T.left, T.data, L);
} else {
T.right = mergeRight2(T.right, next, L); /* for node 11: next is 12 */
}
return T;
}
}

I think the worst case is $O(L \log{T})$

Is This a BST?

From Discussion 7, Spring 2019

Buggy Version:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class TreeNode {
int val;
TreeNode left;
TreeNode right;
}

public static boolean buggyIsBST(TreeNode T) {
if (T == null) {
return true;
} else if (T.left != null && T.left.val > T.val) {
return false;
} else if (T.right != null && T.right.val < T.val) {
return false;
} else {
return buggyIsBST(T.left) && buggyIsBST(T.right);
}
}

// 5
// / \
// 3 8
// / \ / \
// 1 6 7 9 (6 is larger than 5)

Bug-free Version:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static boolean isBST(TreeNode T) {
return isBSTHelper(T, Integer.MIN_VALUE , Integer.MAX_VALUE);
}

public static boolean isBSTHelper(TreeNode T, int prev, int next) {
if (T == null) {
return true;
// } else if (T.left != null) {
// if (T.left.val > T.val || T.left.val < prev) {
// return false;
// }
// } else if (T.right != null) {
// if (T.right.val < T.val || T.right.val > next) {
// return false;
// }
} else if (T.val < prev || T.val > next) {
return false;
} else {
// go left => update MAX restraint (next)
// go right => update MIN restraint (prev)
return isBSTHelper(T.left, prev, T.val) && isBSTHelper(T.right, T.val, next);
}
}

Challenge Lab 7

Challenge Lab 7: link
Code: link

The Internal Path Length of a BST is defined as the average depth times the number of nodes. Or equivalently, it is the sum of the lengths of the paths to every node. The internal and external path lengths are related by:

where $n$ is the number of internal nodes.

Implementation of OIPL:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* Returns the internal path length for an optimum binary search tree of
* size N. Examples:
* N = 1, OIPL: 0
* N = 2, OIPL: 1
* N = 3, OIPL: 2
* N = 4, OIPL: 4
* N = 5, OIPL: 6
* N = 6, OIPL: 8
* N = 7, OIPL: 10
* N = 8, OIPL: 13
*/
public static int optimalIPL(int N) {
if (N == 1 || N == 0) {
return 0;
}
int plus = (int) log(2, N);
return plus + optimalIPL(N - 1);
}

private static double log(int base, int x) {
return Math.log(x) / Math.log(base);
}

Reference: Research Problem about Average Depth

Randomly picking between successor and predecessor will result in 88% of the starting depth. Nobody knows why this happens.

0%