0% found this document useful (0 votes)
12 views34 pages

L11 Binary Tree2

data structure lab note

Uploaded by

양산일
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views34 pages

L11 Binary Tree2

data structure lab note

Uploaded by

양산일
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Data Structure

Binary Trees 2
(Chapter 5)

U Kang
Seoul National University
U Kang 1
In This Lecture
 Implementation and space overhead of binary tree

 Main idea and operations for BST (Binary Search


Tree)

 Complexity and implementations for BST

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)

Leaf implementation is identical to internal node implementation


U Kang 4
Binary Tree Implementation (2)

Distinct internal and leaf node implementations

Between implementations (1) and (2), which is better? Why?


U Kang 5
Inheritance (1)
/** Base class */
public interface VarBinNode {
public boolean isLeaf();
}
/** Leaf node */
class VarLeafNode implements VarBinNode {
private String operand;
public VarLeafNode(String val)
{ operand = val; }
public boolean isLeaf() { return true; }
public String value() { return operand; }
};

U Kang 6
Inheritance (2)

/** Internal node */


class VarIntlNode implements VarBinNode {
private VarBinNode left;
private VarBinNode right;
private Character operator;
public VarIntlNode(Character op,
VarBinNode l, VarBinNode r)
{ operator = op; left = l; right = r; }
public boolean isLeaf() { return false; }
public VarBinNode leftchild() { return left; }
public VarBinNode rightchild(){ return right; }
public Character value() { return operator; }
}

U Kang 7
Inheritance (3)

/** Preorder traversal */


public static void traverse(VarBinNode rt) {
if (rt == null) return;
if (rt.isLeaf())
VisitLeafNode(((VarLeafNode)rt).value());
else {
VisitInternalNode(((VarIntlNode)rt).value());
traverse(((VarIntlNode)rt).leftchild());
traverse(((VarIntlNode)rt).rightchild());
}
}

U Kang 8
Space Overhead (1)
 Space overhead = (non data space) / (total space)

 From the Full Binary Tree Theorem:


 Half of the pointers are null.

 If only leaves store data, then overhead depends on


whether the tree is full.

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);
}

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

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)

 Main idea (when the item to remove has 2 children)


 Find the minimum element from the right subtree of the
item to remove
 Swap the minimum element with the item to remove
U Kang 23
BST Remove (2)
/** Remove a node with key value k
@return The tree with the node removed */
private BSTNode<K,E>
removehelp(BSTNode<K,E> rt, K k) {
if (rt == null) return null;
if (rt.key().compareTo(k) > 0)
rt.setLeft(removehelp(rt.left(), k));
else if (rt.key().compareTo(k) < 0)
rt.setRight(removehelp(rt.right(), k));

removehelp guarantees to return the new root after the removal


U Kang 24
BST Remove (3)
else { // Found it, remove it
if (rt.left() == null)
return rt.right();
else if (rt.right() == null)
return rt.left();
else { // Two children
BSTNode<K,E> temp = getmin(rt.right());
rt.setElement(temp.element());
rt.setKey(temp.key());
rt.setRight(deletemin(rt.right()));
}
}
return rt;
}

U Kang 25
Time Complexity of BST Operations
 Find: O(h)

 Insert: O(h)

 Delete: O(h)

 h = height of the tree

U Kang 26
Time Complexity of BST Operations
 h = height of the tree

 h is O(log n) if tree is balanced. What is the worst


case?
Balanced tree
- The height of the left and right tree for any node
does not differ by more than 1.
- The left subtree of that node is also balanced.
- The right subtree of that node is also balanced.

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

 Rightsibling(r) = r + 1 if r is odd and r+1 < n

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

 How to implement link-based BST operations


 Insert, remove, deletemin, find …
 Get familiar with recursions in the operations
 Time complexity of BST operations

 Array-based implementations for complete binary


tree

U Kang 33
Questions?

U Kang 34

You might also like