L11 Binary Tree2
L11 Binary Tree2
Binary Trees 2
(Chapter 5)
U Kang
Seoul National University
U Kang 1
In This Lecture
Implementation and space overhead of binary tree
U Kang 2
Recursion Examples
int count(BinNode rt) {
if (rt == null) return 0;
return 1 + count(rt.left()) +
count(rt.right());
}
U Kang 3
Binary Tree Implementation (1)
U Kang 6
Inheritance (2)
U Kang 7
Inheritance (3)
U Kang 8
Space Overhead (1)
Space overhead = (non data space) / (total space)
U Kang 9
Space Overhead (2)
Ex: full tree, all nodes the same, with two pointers to children
and one to element:
Total space required is (3p + d)n
Overhead: 3pn
If p = d, this means 3p/(3p + d) = 3/4 overhead.
n: number of nodes
p: space for a pointer
d: space for a data item
How to decrease space overhead?
Idea: eliminate child pointers from the leaf nodes
𝑛
2
(2𝑝)+𝑛𝑝 2𝑝
Then, space overhead = 𝑛 =
2𝑝 +𝑛𝑝+𝑛𝑑 2𝑝+𝑑
2
If p = d, then the space overhead = 2/3
U Kang 10
Binary Search Trees
BST Property: All elements stored in the left subtree
of a node with value K have values < K. All elements
stored in the right subtree of a node with value K
have values ≥ K.
U Kang 11
BSTNode (1)
class BSTNode<K,E> implements BinNode<E> {
private K key;
private E element;
private BSTNode<K,E> left;
private BSTNode<K,E> right;
public BSTNode() {left = right = null; }
public BSTNode(K k, E val)
{ left = right = null; key = k; element = val; }
public BSTNode(K k, E val,
BSTNode<K,E> l, BSTNode<K,E> r)
{ left = l; right = r; key = k; element = val; }
public K key() { return key; }
public K setKey(K k) { return key = k; }
public E element() { return element; }
public E setElement(E v) { return element = v; }
U Kang 12
BSTNode (2)
public BSTNode<K,E> left() { return left; }
public BSTNode<K,E> setLeft(BSTNode<K,E> p)
{ return left = p; }
public BSTNode<K,E> right() { return right; }
public BSTNode<K,E> setRight(BSTNode<K,E> p)
{ return right = p; }
public boolean isLeaf()
{ return (left == null) && (right == null); }
}
U Kang 13
BST (1)
/** BST implementation for Dictionary ADT */
class BST<K, E>
implements Dictionary<K, E> {
private BSTNode<K,E> root; // Root of BST
private int nodecount; // Size of BST
/** Constructor */
BST() { root = null; nodecount = 0; }
/** Reinitialize tree */
public void clear()
{ root = null; nodecount = 0; }
/** Insert a record into the tree.
@param k Key value of the record.
@param e The record to insert. */
public void insert(K k, E e) {
root = inserthelp(root, k, e);
nodecount++;
} U Kang 14
BST (2)
/** Remove a record from the tree.
@param k Key value of record to remove.
@return Record removed, or null if
there is none. */
public E remove(K k) {
E temp = findhelp(root, k); // find it
if (temp != null) {
root = removehelp(root, k); // remove it
nodecount--;
}
return temp;
}
U Kang 15
BST (3)
/** Remove/return root node from tree.
@return The record removed, null if empty. */
public E removeAny() {
if (root != null) {
E temp = root.element();
root = removehelp(root, root.key());
nodecount--;
return temp;
}
else return null;
}
/** @return Record with key k, null if none.
@param k The key value to find. */
public E find(K k)
{ return findhelp(root, k); }
/** @return Number of records in dictionary. */
public int size() { return nodecount; }
} U Kang 16
Check BST
checkBST returns true when low ≤ rt ≤ high, or false o/w
boolean checkBST(BSTNode<Integer> rt,
Integer low, Integer high) {
if (rt == null) return true;
Integer rootkey = rt.key();
if ((rootkey < low) || (rootkey > high))
return false; // Out of range
if (!checkBST(rt.left(), low, rootkey-1))
return false; // Left side failed
return checkBST(rt.right(), rootkey, high);
}
U Kang 17
Check BST
Node rt bounds the range of the nodes in its subtrees
• All nodes in the left subtree < rt
• All nodes in the right subtree ≥ rt
23 24
36
U Kang 18
BST Search
private E findhelp(BSTNode<K,E> rt, K k) {
if (rt == null) return null;
if (rt.key().compareTo(k) > 0)
return findhelp(rt.left(), k);
else if (rt.key().compareTo(k) == 0)
return rt.element();
else return findhelp(rt.right(), k);
}
U Kang 19
BST Insert (1)
U Kang 20
BST Insert (2)
inserthelp always returns the new root after the insertion
private BSTNode<K,E>
inserthelp(BSTNode<K,E> rt, K k, E e) {
if (rt == null) return new BSTNode<K,E>(k, e);
if (rt.key().compareTo(k) > 0)
rt.setLeft(inserthelp(rt.left(), k, e));
else
rt.setRight(inserthelp(rt.right(), k, e));
return rt;
}
U Kang 21
Get/Remove Minimum Value
private BSTNode<K,E>
getmin(BSTNode<K,E> rt) {
if (rt.left() == null)
return rt;
else return getmin(rt.left());
}
private BSTNode<K,E>
deletemin(BSTNode<K,E> rt) {
if (rt.left() == null)
return rt.right();
else {
rt.setLeft(deletemin(rt.left()));
return rt;
}
}
deletemin guarantees to return the new root after the insertion
U Kang 22
BST Remove (1)
U Kang 25
Time Complexity of BST Operations
Find: O(h)
Insert: O(h)
Delete: O(h)
U Kang 26
Time Complexity of BST Operations
h = height of the tree
U Kang 27
Array Implementation (1)
For complete binary tree
Position 0 1 2 3 4 5 6 7 8 9 10 11
Parent -- 0 0 1 1 2 2 3 3 4 4 5
Left Child 1 3 5 7 9 11 -- -- -- -- -- --
Right Child 2 4 6 8 10 -- -- -- -- -- -- --
Left Sibling -- -- 1 -- 3 -- 5 -- 7 -- 9 --
Right Sibling -- 2 -- 4 -- 6 -- 8 -- 10 -- --
U Kang 28
Array Implementation (2)
Parent (r) =
Leftchild(r) =
Rightchild(r) =
Leftsibling(r) =
Rightsibling(r) =
U Kang 29
Array Implementation (3)
Parent (r) = floor ((r-1)/2) if r != 0
Leftchild(r) = 2r+1 if 2r+1 < n
Rightchild(r) = 2r+2 if 2r+2 < n
Leftsibling(r) = r – 1 if r is even
U Kang 30
BST and Traversal
What do we get from an inorder traversal from
BST?
U Kang 31
BST and Traversal
What do we get from an inorder traversal from
BST?
Sorted values (in increasing order) !
U Kang 32
What You Need to Know
Implementations and space overhead of binary
tree
U Kang 33
Questions?
U Kang 34