SlideShare a Scribd company logo
1 of 86
Chapter 8: Trees 1
Trees
Based on Chapter 8,
Koffmann and Wolfgang
Chapter 8: Trees 2
Chapter Outline
• How trees organize information hierarchically
• Using recursion to process trees
• The different ways of traversing a tree
• Binary trees, Binary Search trees, and Heaps
• Implementing binary trees, binary search trees, heaps
• Using linked data structures and arrays
• Using binary search trees for efficient data lookup
• Using Huffman trees for compact character storage
Chapter 8: Trees 3
Tree Terminology
• A tree is a collection of elements (nodes)
• Each node may have 0 or more successors
• (Unlike a list, which has 0 or 1 successor)
• Each node has exactly one predecessor
• Except the starting / top node, called the root
• Links from node to its successors are called branches
• Successors of a node are called its children
• Predecessor of a node is called its parent
• Nodes with same parent are siblings
• Nodes with no children are called leaves
Chapter 8: Trees 4
Tree Terminology (2)
• We also use words like ancestor and descendent
Chapter 8: Trees 5
Tree Terminology (3)
• Subtree of a node:
A tree whose root is a child of that node
• Level of a node:
A measure of its distance from the root:
Level of the root = 1
Level of other nodes = 1 + level of parent
Chapter 8: Trees 6
Tree Terminology (4)
Level 1
(root)
Level 2
Level 2
Level 2
Chapter 8: Trees 7
Binary Trees
• Binary tree: a node has at most 2 non-empty subtrees
• Set of nodes T is a binary tree if either of these is true:
• T is empty
• Root of T has two subtrees, both binary trees
• (Notice that this is a recursive definition)
This is a
binary tree
Chapter 8: Trees 8
Examples of Binary Trees
• Expression tree
• Non-leaf (internal) nodes contain operators
• Leaf nodes contain operands
• Huffman tree
• Represents Huffman codes for characters
appearing in a file or stream
• Huffman code may use different numbers of bits to
encode different characters
• ASCII or Unicode uses a fixed number of bits for
each character
Chapter 8: Trees 9
Examples of Binary Trees (2)
Chapter 8: Trees 10
Examples of Binary Trees (3)
Code for b = 100000
Code for w = 110001
Code for s = 0011
Code for e = 010
Chapter 8: Trees 11
Examples of Binary Trees (4)
• Binary search trees
• Elements in left subtree < element in subtree root
• Elements in right subtree > element in subtree
root
Search algorithm:
1. if the tree is empty, return null
2. if target equals to root node’s data, return that data
3. if target < root node data, return search(left subtree)
4. otherwise return search(right subtree)
Chapter 8: Trees 12
Fullness and Completeness
• (In computer science) trees grow from the top down
• New values inserted in new leaf nodes
• A binary tree is full if all leaves are at the same level
• In a full tree, every node has 0 or 2 non-null children
Chapter 8: Trees 13
Fullness and Completeness (2)
• A binary tree is complete if:
• All leaves are at level h or level h-1 (for some h)
• All level h-1 leaves are to the right
Chapter 8: Trees 14
General (Non-Binary) Trees
• Nodes can have any number of children
Chapter 8: Trees 15
General (Non-Binary) Trees (2)
• A general tree can be represented using a binary tree
Left link is to first child
Right link is to next younger sibling
Chapter 8: Trees 16
Traversals of Binary Trees
• Often want iterate over and process nodes of a tree
• Can walk the tree and visit the nodes in order
• This process is called tree traversal
• Three kinds of binary tree traversal:
• Preorder
• Inorder
• Postorder
• According to order of subtree root w.r.t. its children
Chapter 8: Trees 17
Binary Tree Traversals (2)
• Preorder: Visit root, traverse left, traverse right
• Inorder: Traverse left, visit root, traverse right
• Postorder: Traverse left, traverse right, visit root
Chapter 8: Trees 18
Visualizing Tree Traversals
• Can visualize traversal by imagining a mouse that
walks along outside the tree
• If mouse keeps the tree on its left, it traces a route
called the Euler tour:
• Preorder: record node first time mouse is there
• Inorder: record after mouse traverses left subtree
• Postorder: record node last time mouse is there
Chapter 8: Trees 19
Visualizing Tree Traversals (2)
Preorder:
a, b, d, g, e,
h, c, f, i, j
Chapter 8: Trees 20
Visualizing Tree Traversals (3)
Inorder:
d, g, b, h, e,
a, i, f, j, c
Chapter 8: Trees 21
Visualizing Tree Traversals (4)
Postorder:
g, d, h, e, b,
i, j, f, c, a
Chapter 8: Trees 22
Traversals of Binary Search Trees
• Inorder traversal of a binary search tree 
Nodes are visited in order of increasing data value
Inorder traversal visits in this order: canine, cat, dog, wolf
Chapter 8: Trees 23
Traversals of Expression Trees
• Inorder traversal can insert parentheses where they
belong for infix form
• Postorder traversal results in postfix form
• Prefix traversal results in prefix form
Infix form
Postfix form: x y + a b + c / *
Prefix form: * + x y / + a b c
Chapter 8: Trees 24
The Node<E> Class
• Like linked list, a node has data + links to successors
• Data is reference to object of type E
• Binary tree node has links for left and right subtrees
Chapter 8: Trees 25
The BinaryTree<E> Class
BinaryTree<E>;
remaining nodes are
Node<E>
Chapter 8: Trees 26
Code for Node<E>
protected static class Node<E>
implements Serializable {
protected E data;
protected Node<E> left;
protected Node<E> right;
public Node (E e,
Node<E> l, Node<E> r) {
this.data = e;
this.left = l;
this.right = r;
}
Chapter 8: Trees 27
Code for Node<E> (2)
public Node (E e) {
this(e, null, null);
}
public String toString () {
return data.toString();
}
}
Chapter 8: Trees 28
Code for BinaryTree<E>
import java.io.*;
public class BinaryTree<E>
implements Serializable {
protected Node<E> root;
protected static class Node<E> ...{...}
public BinaryTree () { root = null; }
protected BinaryTree (Node<E> root) {
this.root = root;
}
Chapter 8: Trees 29
Code for BinaryTree<E> (2)
public BinaryTree (E data,
BinaryTree<E> left,
BinaryTree<E> right) {
root = new Node<E>
(e,
(left == null) ? null : left .root,
(right == null) ? null : right.root);
}
Chapter 8: Trees 30
Code for BinaryTree<E> (3)
public BinaryTree<E> getLeftSubtree () {
if (root == null || root.left == null)
return null;
return new BinaryTree<E>(root.left);
}
public BinaryTree<E> getRightSubtree () {
if (root == null || root.right == null)
return null;
return new BinaryTree<E>(root.right);
}
Chapter 8: Trees 31
Code for BinaryTree<E> (4)
public boolean isLeaf () {
return root.left == null &&
root.right == null;
}
public String toString () {
StringBuilder sb = new StringBuilder();
preOrderTraverse(root, 1, sb);
return sb.toString();
}
Chapter 8: Trees 32
Code for BinaryTree<E> (5)
private void preOrderTraverse (Node<E> n,
int d, StringBuilder sb) {
for (int i = 1; i < d; ++i)
sb.append(“ “);
if (node == null)
sb.append(“nulln”);
else {
sb.append(node.toString());
sb.append(“n”);
preOrderTraverse(node.left , d+1, sb);
preOrderTraverse(node.right, d+1, sb);
}
}
Chapter 8: Trees 33
Code for BinaryTree<E> (6)
public static BinaryTree<String>
readBinaryTree (BufferedReader bR)
throws IOException {
String data = bR.readLine().trim();
if (data.equals(“null”))
return null;
BinaryTree<String> l =
readBinaryTree(bR);
BinaryTree<String> r =
readBinaryTree(bR);
return new BinaryTree<String>(
data, l, r);
}
Chapter 8: Trees 34
Overview of Binary Search Tree
Binary search tree definition:
T is a binary search tree if either of these is true
• T is empty; or
• Root has two subtrees:
• Each is a binary search tree
• Value in root > all values of the left subtree
• Value in root < all values in the right subtree
Chapter 8: Trees 35
Binary Search Tree Example
Chapter 8: Trees 36
Searching a Binary Tree
Chapter 8: Trees 37
Searching a Binary Tree: Algorithm
1. if root is null
2. item not in tree: return null
3. compare target and root.data
4. if they are equal
5. target is found, return root.data
6. else if target < root.data
7. return search(left subtree)
8. else
9. return search(right subtree)
Chapter 8: Trees 38
Class TreeSet<E> and
Interface SearchTree<E>
• Java API offers TreeSet<E>
• Implements binary search trees
• Has operations such as add, contains, remove
• We define SearchTree<E>, offering some of these
Chapter 8: Trees 39
Code for Interface SearchTree<E>
public interface SearchTree<E> {
// add/remove say whether tree changed
public boolean add (E e);
boolean remove (E e);
// contains tests whether e is in tree
public boolean contains (E e);
// find and delete return e if present,
// or null if it is not present
public E find (E e);
public E delete (E e);
}
Chapter 8: Trees 40
Code for BinarySearchTree<E>
public class BinarySearchTree<
E extends Comparable<E>>
extends BinaryTree<E>
implements SearchTree<E> {
protected boolean addReturn;
protected E deleteReturn;
// these two hold the “extra” return
// values from the SearchTree adding
// and deleting methods
...
}
Chapter 8: Trees 41
BinarySearchTree.find(E e)
public E find (E e) {
return find(root, e);
}
private E find (Node<E> n, E e) {
if (n == null) return null;
int comp = e.compareTo(n.data);
if (comp == 0) return n.data;
if (comp < 0)
return find(n.left , e);
else
return find(n.right, e);
}
Chapter 8: Trees 42
Insertion into a Binary Search Tree
Chapter 8: Trees 43
Example Growing a Binary Search Tree
1. insert 7
2. insert 3
3. insert 9
4. insert 5
5. insert 6
7
3 9
5
6
Chapter 8: Trees 44
Binary Search Tree Add Algorithm
1. if root is null
2. replace empty tree with new data leaf; return true
3. if item equals root.data
4. return false
5. if item < root.data
6. return insert(left subtree, item)
7. else
8. return insert(right subtree, item)
Chapter 8: Trees 45
BinarySearchTree.add(E e)
public boolean add (E e) {
root = add(root, item);
return addReturn;
}
Chapter 8: Trees 46
BinarySearchTree.add(E e) (2)
private Node<E> add (Node<E> n, E e) {
if (n == null) { addReturn = true;
return new Node<E>(e);
}
int comp = e.compareTo(n.data);
if (comp == 0)
addReturn = false;
else if (comp < 0)
n.left = add(n.left , e);
else
n.right = add(n.right, e);
return n;
}
Chapter 8: Trees 47
Removing from a Binary Search Tree
• Item not present: do nothing
• Item present in leaf: remove leaf (change to null)
• Item in non-leaf with one child:
Replace current node with that child
• Item in non-leaf with two children?
• Find largest item in the left subtree
• Recursively remove it
• Use it as the parent of the two subtrees
• (Could use smallest item in right subtree)
Chapter 8: Trees 48
Removing from a Binary Search Tree (2)
Chapter 8: Trees 49
Example Shrinking a Binary Search Tree
1. delete 9
2. delete 5
3. delete 3
7
3 9
5
6
1
2
6
2
Chapter 8: Trees 50
BinarySearchTree.delete(E e)
public E delete (E e) {
root = delete(root, item);
return deleteReturn;
}
private Node<E> delete (Node<E> n E e) {
if (n == null) {
deleteReturn = null;
return null;
}
int comp = e.compareTo(n.data);
...
Chapter 8: Trees 51
BinarySearchTree.delete(E e) (2)
if (comp < 0) {
n.left = delete(n.left , e);
return n;
} else if (comp > 0) {
n.right = delete(n.right, e);
return n;
} else {
// item is in n.data
deleteReturn = n.data;
...
}
}
Chapter 8: Trees 52
BinarySearchTree.delete(E e) (3)
// deleting value in n: adjust tree
if (n.left == null) {
return n.right; // ok if also null
} else if (n.right == null) {
return n.left;
} else {
// case where node has two children
...
}
Chapter 8: Trees 53
BinarySearchTree.delete(E e) (4)
// case where node has two children
if (n.left.right == null) {
// largest to left is in left child
n.data = n.left.data;
n.left = n.left.left;
return n;
} else {
n.data = findLargestChild(n.left);
return n;
}
Chapter 8: Trees 54
findLargestChild
// finds and removes largest value under n
private E findLargestChild (Node<E> n) {
// Note: called only for n.right != null
if (n.right.right == null) {
// no right child: this value largest
E e = n.right.data;
n.right = n.right.left; // ok if null
return e;
}
return findLargestChild(n.right);
}
Chapter 8: Trees 55
Using Binary Search Trees
• Want an index of words in a paper, by line #
• Put word/line # strings into a binary search tree
• “java, 0005”, “a, 0013”, and so on
• Performance?
• Average BST performance is O(log n)
• Its rare worst case is O(n)
• Happens (for example) with sorted input
• Ordered list performance is O(n)
• Later consider guaranteeing O(log n) trees
Chapter 8: Trees 56
Using Binary Search Trees: Code
public class IndexGen {
private TreeSet<String> index;
public IndexGen () {
index = new TreeSet<String>();
}
public void showIndex () {
for (String next : index)
System.out.println(next);
}
...
}
Chapter 8: Trees 57
Using Binary Search Trees: Code (2)
public void build (BufferedReader bR)
throws IOException {
int lineNum = 0;
String line;
while ((line = bR.readLine()) != null) {
// process line
}
}
Chapter 8: Trees 58
Using Binary Search Trees: Code (3)
// processing a line:
StringTokenizer tokens =
new StringTokenizer(line, ...);
while (tokens.hasNextToken()) {
String tok =
tokens.nextToken().toLowerCase();
index.add(
String.format(“%s, %04d”,
tok, lineNum));
}
Chapter 8: Trees 59
Heaps
• A heap orders its node, but in a way different from a
binary search tree
• A complete tree is a heap if
• The value in the root is the smallest of the tree
• Every subtree is also a heap
• Equivalently, a complete tree is a heap if
• Node value < child value, for each child of the node
• Note: This use of the word “heap” is entirely different
from the heap that is the allocation area in Java
Chapter 8: Trees 60
Example of a Heap
Chapter 8: Trees 61
Inserting an Item into a Heap
1. Insert the item in the next position across the bottom
of the complete tree: preserve completeness
2. Restore “heap-ness”:
1. while new item not root and < parent
2. swap new item with parent
Chapter 8: Trees 62
Example Inserting into a Heap
Insert 1 2
3 4
5 1
11 7
8
5 1
4
1
2
Add as leaf
Swap up
Swap up
Chapter 8: Trees 63
Removing an Item from a Heap
• Removing an item is always from the top:
• Remove the root (minimum element):
• Leaves a “hole”:
• Fill the “hole” with the last item (lower right-hand) L
• Preserve completeness
• Swap L with smallest child, as necessary
• Restore “heap-ness”
Chapter 8: Trees 64
Example Removing From a Heap
Remove: returns 1
3 4
5
11 7
8
5 1
4
1
2
4
4
2
Move 4 to root
Swap down
Chapter 8: Trees 65
Implementing a Heap
• Recall: a heap is a complete binary tree
• (plus the heap ordering property)
• A complete binary tree fits nicely in an array:
• The root is at index 0
• Children of node at index i are at indices 2i+1, 2i+2
3
5
11 7
8
5 4
2 0
1 2
3 4 5
2 5 4 11 8 7
0 1 2 3 4 5
Chapter 8: Trees 66
Inserting into an Array(List) Heap
1. Insert new item at end; set child to size-1
2. Set parent to (child – 1)/2
3. while (parent ≥ 0 and a[parent] > a[child])
4. Swap a[parent] and a[child]
5. Set child equal to parent
6. Set parent to (child – 1) / 2
Chapter 8: Trees 67
Deleting from an Array(List) Heap
1. Set a[0] to a[size-1], and shrink size by 1
2. Set parent to 0
3. while (true)
4. Set lc to 2 * parent + 1, and rc to lc + 1
5. If lc ≥ size, break out of while (we’re done)
6. Set minc to lc
7. If rc < size and a[rc] < a[lc], set minc to rc
8. If a[parent]  a[minc], break (we’re done)
9. Swap a[parent] and a[minc]; set parent to minc
Chapter 8: Trees 68
Performance of Array(List) Heap
• A complete tree of height h has:
• Less than 2h nodes
• At least 2h-1 nodes
• Thus complete tree of n nodes has height O(log n)
• Insertion and deletion at most do constant amount of
work at each level
• Thus these operations are O(log n), always
• Heap forms the basis of heapsort algorithm (Ch. 10)
• Heap also useful for priority queues
Chapter 8: Trees 69
Priority Queues
• A priority queue de-queues items in priority order
• Not in order of entry into the queue (not FIFO)
• Heap is an efficient implementation of priority queue
• Operations cost at most O(log n)
public boolean offer (E e); // insert
public E remove (); // return smallest
public E poll (); // smallest or null
public E peek (); // smallest or null
public E element (); // smallest
Chapter 8: Trees 70
Design of Class PriQueue<E>
ArrayList<E> data; // holds the data
PriQueue () // uses natural order of E
PriQueue (int n, Comparator<E> comp)
// uses size n, uses comp to order
private int compare (E left, E right)
// compares two E objects: -1, 0, 1
private void swap (int i, int j)
// swaps elements at positions i and j
Chapter 8: Trees 71
Implementing PriQueue<E>
public class PriQueue<E>
extends AbstractQueue<E>
implements Queue<E> {
private ArrayList<E> data;
Comparator<E> comparator = null;
public PriQueue () {
data = new ArrayList<E>();
}
...
Chapter 8: Trees 72
Implementing PriQueue<E> (2)
public PriQueue (int n,
Comparator<E> comp) {
if (n < 1)
throw new IllegalArgumentException();
data = new ArrayList<E>(n);
this.comp = comp;
}
Chapter 8: Trees 73
Implementing PriQueue<E> (3)
private int compare (E lft, E rt) {
if (comp != null)
return comp.compare(lft, rt);
else
return
((Comparable<E>)lft).compareTo(rt);
}
Chapter 8: Trees 74
Implementing PriQueue<E> (4)
public boolean offer (E e) {
data.add(e);
int child = data.size() – 1;
int parent = (child – 1) / 2;
while (parent >= 0 &&
compare(data.get(parent),
data.get(child)) > 0) {
swap(parent, child);
child = parent;
parent = (child – 1) / 2;
}
return true;
}
Chapter 8: Trees 75
Implementing PriQueue<E> (5)
public E poll () {
if (isEmpty()) return null;
E result = data.get(0);
if (data.size() == 1) {
data.remove(0);
return result;
}
// remove last item and store in posn 0
data.set(0, data.remove(data.size()-1));
int parent = 0;
while (true)
... swap down as necessary ...
return result;
Chapter 8: Trees 76
Implementing PriQueue<E> (6)
// swapping down
int lc = 2 * parent + 1;
if (lc >= data.size()) break;
int rc = lc + 1;
int minc = lc;
if (rc < data.size() &&
compare(data.get(lc),
data.get(rc)) > 0)
minc = rc;
if (compare(data.get(parent),
data.get(minc)) <= 0) break;
swap(parent, minc);
parent = minc;
Chapter 8: Trees 77
Huffman Trees
Problem Input: A set of symbols, each with a frequency
of occurrence.
Desired output: A Huffman tree giving a code that
minimizes the bit length of strings consisting of those
symbols with that frequency of occurrence.
Strategy: Starting with single-symbol trees, repeatedly
combine the two lowest-frequency trees, giving one
new tree of frequency = sum of the two frequencies.
Stop when we have a single tree.
Chapter 8: Trees 78
Huffman Trees (2)
Implementation approach:
• Use a priority queue to find lowest frequency trees
• Use binary trees to represent the Huffman
(de)coding trees
Example: b=13, c=22, d=32 a=64 e=103
Combine b and c: bc=35
Combine d and bc: d(bc)=67
Combine a and d(bc): a(d(bc))=131
Combine e and a(d(bc)): e(a(d(bc)))=234 ... done
Chapter 8: Trees 79
Huffman Tree Example
e=103
a=64
d=32
b=13 c=22
0
0
0
0 1
1
1
1
0
10
110
1110 1111
Chapter 8: Trees 80
Design of Class HuffTree
private BinaryTree<HuffData> huffTree;
// HuffData are pairs: weight, symbol
buildTree (HuffData[] input)
String decode (String message)
printcode (Printstream out)
Chapter 8: Trees 81
Implementing HuffTree
public class HuffTree
implements Serializable {
public static class HuffData
implements Serializable {
private double weight;
private Character symbol;
public HuffData (double weight,
Character symbol) {
this.weight = weight;
this.symbol = symbol;
}
}
Chapter 8: Trees 82
Implementing HuffTree (2)
private BinaryTree<HuffData> tree;
private static class CompHuff
implements
Comparator<BinaryTree<HuffData>> {
public int compare (
BinaryTree<HuffData> lft,
BinaryTree<HuffData> rt) {
double wL = lft.getData().weight;
double wR = rt .getdata().weight;
return Double.compare(wL, wR);
}
}
Chapter 8: Trees 83
Implementing HuffTree (3)
public void buildTree (HuffData[] syms) {
Queue<BinaryTree<HuffData>> q =
new PriQueue<BinaryTree<HuffData>>(
syms.length, new CompHuffTree());
for (HuffData sym : syms) {
BinaryTree<HuffData> tree =
new BinaryTree<HuffData>(sym);
q.offer(tree);
}
... on to second half ...
Chapter 8: Trees 84
Implementing HuffTree (4)
while (q.size() > 1) {
BinaryTree<HuffData> lft = q.poll();
BinaryTree<HuffData> rt = q.poll();
double wl = lft.getData().weight;
double wr = rt .getData().weight;
HuffData sum =
new HuffData(wl+wr, null);
BinaryTree<HuffData> nTree =
new BinaryTree<HuffData>
(sum, lft, rt);
q.offer(nTree);
}
this.tree = q.poll();
Chapter 8: Trees 85
Implementing HuffTree (5)
private void printCode
(PrintStream out, String code,
BinaryTree<HuffData> tree) {
HuffData data = tree.getData;
if (data.symbol != null) {
if (data.symbols.equals(“ “))
out.println(“space: “ + code);
else
out.println(data.symbol+”: “+code);
} else {
printCode(out,code+”0”, tree.left ());
printCode(out,code+”1”, tree.right());
} }
Chapter 8: Trees 86
Implementing HuffTree (6)
public string decode (String msg) {
StringBuilder res = new StringBuilder();
BinaryTree<HuffData> curr = tree;
for (int i = 0; i < msg.length(); ++i) {
if (msg.charAt(i) == ‘1’)
curr = curr.getRightSubtree();
else
curr = curr.getLeftSubtree();
if (curr.isLeaf()) {
HuffData data = curr.getData();
res.append(data.symbol);
curr = tree;
} } return res.toString();

More Related Content

Similar to lecture-i-trees.ppt

tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptxMouDhara1
 
Mca admission in india
Mca admission in indiaMca admission in india
Mca admission in indiaEdhole.com
 
Tree Introduction.pptx
Tree Introduction.pptxTree Introduction.pptx
Tree Introduction.pptxRahulAI
 
nptel 2nd presentation.pptx
nptel 2nd presentation.pptxnptel 2nd presentation.pptx
nptel 2nd presentation.pptxKeshavBandil2
 
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_treesDanish Aakash
 
Admissions in india 2015
Admissions in india 2015Admissions in india 2015
Admissions in india 2015Edhole.com
 
Hub102 - Lesson4 - Data Structure
Hub102 - Lesson4 - Data StructureHub102 - Lesson4 - Data Structure
Hub102 - Lesson4 - Data StructureTiểu Hổ
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfssuser034ce1
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structureAnusruti Mitra
 
BinarySearchTrees.ppt
BinarySearchTrees.pptBinarySearchTrees.ppt
BinarySearchTrees.pptItsStranger1
 
BinarySearchTrees (1).ppt
BinarySearchTrees (1).pptBinarySearchTrees (1).ppt
BinarySearchTrees (1).pptplagcheck
 
data structure very BinarySearchTrees.ppt
data structure very BinarySearchTrees.pptdata structure very BinarySearchTrees.ppt
data structure very BinarySearchTrees.pptDharmannaRathod1
 

Similar to lecture-i-trees.ppt (20)

tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
Binary trees
Binary treesBinary trees
Binary trees
 
Mca admission in india
Mca admission in indiaMca admission in india
Mca admission in india
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Tree Introduction.pptx
Tree Introduction.pptxTree Introduction.pptx
Tree Introduction.pptx
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
nptel 2nd presentation.pptx
nptel 2nd presentation.pptxnptel 2nd presentation.pptx
nptel 2nd presentation.pptx
 
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_trees
 
Admissions in india 2015
Admissions in india 2015Admissions in india 2015
Admissions in india 2015
 
Hub102 - Lesson4 - Data Structure
Hub102 - Lesson4 - Data StructureHub102 - Lesson4 - Data Structure
Hub102 - Lesson4 - Data Structure
 
Unit 4.1 (tree)
Unit 4.1 (tree)Unit 4.1 (tree)
Unit 4.1 (tree)
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdf
 
Unit III.ppt
Unit III.pptUnit III.ppt
Unit III.ppt
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
BinarySearchTrees.ppt
BinarySearchTrees.pptBinarySearchTrees.ppt
BinarySearchTrees.ppt
 
BinarySearchTrees.ppt
BinarySearchTrees.pptBinarySearchTrees.ppt
BinarySearchTrees.ppt
 
BinarySearchTrees (1).ppt
BinarySearchTrees (1).pptBinarySearchTrees (1).ppt
BinarySearchTrees (1).ppt
 
Binary searchtrees
Binary searchtreesBinary searchtrees
Binary searchtrees
 
data structure very BinarySearchTrees.ppt
data structure very BinarySearchTrees.pptdata structure very BinarySearchTrees.ppt
data structure very BinarySearchTrees.ppt
 

More from MouDhara1

datacommunicationnetworking-161025074805.pptx
datacommunicationnetworking-161025074805.pptxdatacommunicationnetworking-161025074805.pptx
datacommunicationnetworking-161025074805.pptxMouDhara1
 
06 Recursion in C.pptx
06 Recursion in C.pptx06 Recursion in C.pptx
06 Recursion in C.pptxMouDhara1
 
JSP-Servlet.ppt
JSP-Servlet.pptJSP-Servlet.ppt
JSP-Servlet.pptMouDhara1
 
09 Structures in C.pptx
09 Structures in C.pptx09 Structures in C.pptx
09 Structures in C.pptxMouDhara1
 
Chapter 12 - Heaps.ppt
Chapter 12 - Heaps.pptChapter 12 - Heaps.ppt
Chapter 12 - Heaps.pptMouDhara1
 
04_Recurrences_1.ppt
04_Recurrences_1.ppt04_Recurrences_1.ppt
04_Recurrences_1.pptMouDhara1
 
javascript12.pptx
javascript12.pptxjavascript12.pptx
javascript12.pptxMouDhara1
 
Linked list.pptx
Linked list.pptxLinked list.pptx
Linked list.pptxMouDhara1
 
For Client vs.pptx
For Client vs.pptxFor Client vs.pptx
For Client vs.pptxMouDhara1
 
Form tag.pptx
Form tag.pptxForm tag.pptx
Form tag.pptxMouDhara1
 
Web technology.pptx
Web technology.pptxWeb technology.pptx
Web technology.pptxMouDhara1
 
web tech.pptx
web tech.pptxweb tech.pptx
web tech.pptxMouDhara1
 
Introduction to Data Structure.pptx
Introduction to Data Structure.pptxIntroduction to Data Structure.pptx
Introduction to Data Structure.pptxMouDhara1
 

More from MouDhara1 (19)

ppt_dcn.pdf
ppt_dcn.pdfppt_dcn.pdf
ppt_dcn.pdf
 
datacommunicationnetworking-161025074805.pptx
datacommunicationnetworking-161025074805.pptxdatacommunicationnetworking-161025074805.pptx
datacommunicationnetworking-161025074805.pptx
 
06 Recursion in C.pptx
06 Recursion in C.pptx06 Recursion in C.pptx
06 Recursion in C.pptx
 
JSP-Servlet.ppt
JSP-Servlet.pptJSP-Servlet.ppt
JSP-Servlet.ppt
 
09 Structures in C.pptx
09 Structures in C.pptx09 Structures in C.pptx
09 Structures in C.pptx
 
Chapter 12 - Heaps.ppt
Chapter 12 - Heaps.pptChapter 12 - Heaps.ppt
Chapter 12 - Heaps.ppt
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
DTD1.pptx
DTD1.pptxDTD1.pptx
DTD1.pptx
 
04_Recurrences_1.ppt
04_Recurrences_1.ppt04_Recurrences_1.ppt
04_Recurrences_1.ppt
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
javascript12.pptx
javascript12.pptxjavascript12.pptx
javascript12.pptx
 
Linked list.pptx
Linked list.pptxLinked list.pptx
Linked list.pptx
 
For Client vs.pptx
For Client vs.pptxFor Client vs.pptx
For Client vs.pptx
 
Form tag.pptx
Form tag.pptxForm tag.pptx
Form tag.pptx
 
Web technology.pptx
Web technology.pptxWeb technology.pptx
Web technology.pptx
 
cse.pptx
cse.pptxcse.pptx
cse.pptx
 
web tech.pptx
web tech.pptxweb tech.pptx
web tech.pptx
 
Introduction to Data Structure.pptx
Introduction to Data Structure.pptxIntroduction to Data Structure.pptx
Introduction to Data Structure.pptx
 

Recently uploaded

Cost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based questionCost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based questionSneha Padhiar
 
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdfDEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdfAkritiPradhan2
 
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School CourseKatarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Coursebim.edu.pl
 
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESCME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESkarthi keyan
 
SOFTWARE ESTIMATION COCOMO AND FP CALCULATION
SOFTWARE ESTIMATION COCOMO AND FP CALCULATIONSOFTWARE ESTIMATION COCOMO AND FP CALCULATION
SOFTWARE ESTIMATION COCOMO AND FP CALCULATIONSneha Padhiar
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...Erbil Polytechnic University
 
Prach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism CommunityPrach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism Communityprachaibot
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating SystemRashmi Bhat
 
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENTFUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENTSneha Padhiar
 
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHTEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHSneha Padhiar
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdfCaalaaAbdulkerim
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
signals in triangulation .. ...Surveying
signals in triangulation .. ...Surveyingsignals in triangulation .. ...Surveying
signals in triangulation .. ...Surveyingsapna80328
 
Artificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewArtificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewsandhya757531
 
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfModule-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfManish Kumar
 
OOP concepts -in-Python programming language
OOP concepts -in-Python programming languageOOP concepts -in-Python programming language
OOP concepts -in-Python programming languageSmritiSharma901052
 
Engineering Drawing section of solid
Engineering Drawing     section of solidEngineering Drawing     section of solid
Engineering Drawing section of solidnamansinghjarodiya
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Erbil Polytechnic University
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communicationpanditadesh123
 
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdfPaper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdfNainaShrivastava14
 

Recently uploaded (20)

Cost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based questionCost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based question
 
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdfDEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
 
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School CourseKatarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Course
 
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESCME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
 
SOFTWARE ESTIMATION COCOMO AND FP CALCULATION
SOFTWARE ESTIMATION COCOMO AND FP CALCULATIONSOFTWARE ESTIMATION COCOMO AND FP CALCULATION
SOFTWARE ESTIMATION COCOMO AND FP CALCULATION
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...
 
Prach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism CommunityPrach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism Community
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating System
 
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENTFUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
 
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHTEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdf
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
signals in triangulation .. ...Surveying
signals in triangulation .. ...Surveyingsignals in triangulation .. ...Surveying
signals in triangulation .. ...Surveying
 
Artificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewArtificial Intelligence in Power System overview
Artificial Intelligence in Power System overview
 
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfModule-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
 
OOP concepts -in-Python programming language
OOP concepts -in-Python programming languageOOP concepts -in-Python programming language
OOP concepts -in-Python programming language
 
Engineering Drawing section of solid
Engineering Drawing     section of solidEngineering Drawing     section of solid
Engineering Drawing section of solid
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communication
 
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdfPaper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
 

lecture-i-trees.ppt

  • 1. Chapter 8: Trees 1 Trees Based on Chapter 8, Koffmann and Wolfgang
  • 2. Chapter 8: Trees 2 Chapter Outline • How trees organize information hierarchically • Using recursion to process trees • The different ways of traversing a tree • Binary trees, Binary Search trees, and Heaps • Implementing binary trees, binary search trees, heaps • Using linked data structures and arrays • Using binary search trees for efficient data lookup • Using Huffman trees for compact character storage
  • 3. Chapter 8: Trees 3 Tree Terminology • A tree is a collection of elements (nodes) • Each node may have 0 or more successors • (Unlike a list, which has 0 or 1 successor) • Each node has exactly one predecessor • Except the starting / top node, called the root • Links from node to its successors are called branches • Successors of a node are called its children • Predecessor of a node is called its parent • Nodes with same parent are siblings • Nodes with no children are called leaves
  • 4. Chapter 8: Trees 4 Tree Terminology (2) • We also use words like ancestor and descendent
  • 5. Chapter 8: Trees 5 Tree Terminology (3) • Subtree of a node: A tree whose root is a child of that node • Level of a node: A measure of its distance from the root: Level of the root = 1 Level of other nodes = 1 + level of parent
  • 6. Chapter 8: Trees 6 Tree Terminology (4) Level 1 (root) Level 2 Level 2 Level 2
  • 7. Chapter 8: Trees 7 Binary Trees • Binary tree: a node has at most 2 non-empty subtrees • Set of nodes T is a binary tree if either of these is true: • T is empty • Root of T has two subtrees, both binary trees • (Notice that this is a recursive definition) This is a binary tree
  • 8. Chapter 8: Trees 8 Examples of Binary Trees • Expression tree • Non-leaf (internal) nodes contain operators • Leaf nodes contain operands • Huffman tree • Represents Huffman codes for characters appearing in a file or stream • Huffman code may use different numbers of bits to encode different characters • ASCII or Unicode uses a fixed number of bits for each character
  • 9. Chapter 8: Trees 9 Examples of Binary Trees (2)
  • 10. Chapter 8: Trees 10 Examples of Binary Trees (3) Code for b = 100000 Code for w = 110001 Code for s = 0011 Code for e = 010
  • 11. Chapter 8: Trees 11 Examples of Binary Trees (4) • Binary search trees • Elements in left subtree < element in subtree root • Elements in right subtree > element in subtree root Search algorithm: 1. if the tree is empty, return null 2. if target equals to root node’s data, return that data 3. if target < root node data, return search(left subtree) 4. otherwise return search(right subtree)
  • 12. Chapter 8: Trees 12 Fullness and Completeness • (In computer science) trees grow from the top down • New values inserted in new leaf nodes • A binary tree is full if all leaves are at the same level • In a full tree, every node has 0 or 2 non-null children
  • 13. Chapter 8: Trees 13 Fullness and Completeness (2) • A binary tree is complete if: • All leaves are at level h or level h-1 (for some h) • All level h-1 leaves are to the right
  • 14. Chapter 8: Trees 14 General (Non-Binary) Trees • Nodes can have any number of children
  • 15. Chapter 8: Trees 15 General (Non-Binary) Trees (2) • A general tree can be represented using a binary tree Left link is to first child Right link is to next younger sibling
  • 16. Chapter 8: Trees 16 Traversals of Binary Trees • Often want iterate over and process nodes of a tree • Can walk the tree and visit the nodes in order • This process is called tree traversal • Three kinds of binary tree traversal: • Preorder • Inorder • Postorder • According to order of subtree root w.r.t. its children
  • 17. Chapter 8: Trees 17 Binary Tree Traversals (2) • Preorder: Visit root, traverse left, traverse right • Inorder: Traverse left, visit root, traverse right • Postorder: Traverse left, traverse right, visit root
  • 18. Chapter 8: Trees 18 Visualizing Tree Traversals • Can visualize traversal by imagining a mouse that walks along outside the tree • If mouse keeps the tree on its left, it traces a route called the Euler tour: • Preorder: record node first time mouse is there • Inorder: record after mouse traverses left subtree • Postorder: record node last time mouse is there
  • 19. Chapter 8: Trees 19 Visualizing Tree Traversals (2) Preorder: a, b, d, g, e, h, c, f, i, j
  • 20. Chapter 8: Trees 20 Visualizing Tree Traversals (3) Inorder: d, g, b, h, e, a, i, f, j, c
  • 21. Chapter 8: Trees 21 Visualizing Tree Traversals (4) Postorder: g, d, h, e, b, i, j, f, c, a
  • 22. Chapter 8: Trees 22 Traversals of Binary Search Trees • Inorder traversal of a binary search tree  Nodes are visited in order of increasing data value Inorder traversal visits in this order: canine, cat, dog, wolf
  • 23. Chapter 8: Trees 23 Traversals of Expression Trees • Inorder traversal can insert parentheses where they belong for infix form • Postorder traversal results in postfix form • Prefix traversal results in prefix form Infix form Postfix form: x y + a b + c / * Prefix form: * + x y / + a b c
  • 24. Chapter 8: Trees 24 The Node<E> Class • Like linked list, a node has data + links to successors • Data is reference to object of type E • Binary tree node has links for left and right subtrees
  • 25. Chapter 8: Trees 25 The BinaryTree<E> Class BinaryTree<E>; remaining nodes are Node<E>
  • 26. Chapter 8: Trees 26 Code for Node<E> protected static class Node<E> implements Serializable { protected E data; protected Node<E> left; protected Node<E> right; public Node (E e, Node<E> l, Node<E> r) { this.data = e; this.left = l; this.right = r; }
  • 27. Chapter 8: Trees 27 Code for Node<E> (2) public Node (E e) { this(e, null, null); } public String toString () { return data.toString(); } }
  • 28. Chapter 8: Trees 28 Code for BinaryTree<E> import java.io.*; public class BinaryTree<E> implements Serializable { protected Node<E> root; protected static class Node<E> ...{...} public BinaryTree () { root = null; } protected BinaryTree (Node<E> root) { this.root = root; }
  • 29. Chapter 8: Trees 29 Code for BinaryTree<E> (2) public BinaryTree (E data, BinaryTree<E> left, BinaryTree<E> right) { root = new Node<E> (e, (left == null) ? null : left .root, (right == null) ? null : right.root); }
  • 30. Chapter 8: Trees 30 Code for BinaryTree<E> (3) public BinaryTree<E> getLeftSubtree () { if (root == null || root.left == null) return null; return new BinaryTree<E>(root.left); } public BinaryTree<E> getRightSubtree () { if (root == null || root.right == null) return null; return new BinaryTree<E>(root.right); }
  • 31. Chapter 8: Trees 31 Code for BinaryTree<E> (4) public boolean isLeaf () { return root.left == null && root.right == null; } public String toString () { StringBuilder sb = new StringBuilder(); preOrderTraverse(root, 1, sb); return sb.toString(); }
  • 32. Chapter 8: Trees 32 Code for BinaryTree<E> (5) private void preOrderTraverse (Node<E> n, int d, StringBuilder sb) { for (int i = 1; i < d; ++i) sb.append(“ “); if (node == null) sb.append(“nulln”); else { sb.append(node.toString()); sb.append(“n”); preOrderTraverse(node.left , d+1, sb); preOrderTraverse(node.right, d+1, sb); } }
  • 33. Chapter 8: Trees 33 Code for BinaryTree<E> (6) public static BinaryTree<String> readBinaryTree (BufferedReader bR) throws IOException { String data = bR.readLine().trim(); if (data.equals(“null”)) return null; BinaryTree<String> l = readBinaryTree(bR); BinaryTree<String> r = readBinaryTree(bR); return new BinaryTree<String>( data, l, r); }
  • 34. Chapter 8: Trees 34 Overview of Binary Search Tree Binary search tree definition: T is a binary search tree if either of these is true • T is empty; or • Root has two subtrees: • Each is a binary search tree • Value in root > all values of the left subtree • Value in root < all values in the right subtree
  • 35. Chapter 8: Trees 35 Binary Search Tree Example
  • 36. Chapter 8: Trees 36 Searching a Binary Tree
  • 37. Chapter 8: Trees 37 Searching a Binary Tree: Algorithm 1. if root is null 2. item not in tree: return null 3. compare target and root.data 4. if they are equal 5. target is found, return root.data 6. else if target < root.data 7. return search(left subtree) 8. else 9. return search(right subtree)
  • 38. Chapter 8: Trees 38 Class TreeSet<E> and Interface SearchTree<E> • Java API offers TreeSet<E> • Implements binary search trees • Has operations such as add, contains, remove • We define SearchTree<E>, offering some of these
  • 39. Chapter 8: Trees 39 Code for Interface SearchTree<E> public interface SearchTree<E> { // add/remove say whether tree changed public boolean add (E e); boolean remove (E e); // contains tests whether e is in tree public boolean contains (E e); // find and delete return e if present, // or null if it is not present public E find (E e); public E delete (E e); }
  • 40. Chapter 8: Trees 40 Code for BinarySearchTree<E> public class BinarySearchTree< E extends Comparable<E>> extends BinaryTree<E> implements SearchTree<E> { protected boolean addReturn; protected E deleteReturn; // these two hold the “extra” return // values from the SearchTree adding // and deleting methods ... }
  • 41. Chapter 8: Trees 41 BinarySearchTree.find(E e) public E find (E e) { return find(root, e); } private E find (Node<E> n, E e) { if (n == null) return null; int comp = e.compareTo(n.data); if (comp == 0) return n.data; if (comp < 0) return find(n.left , e); else return find(n.right, e); }
  • 42. Chapter 8: Trees 42 Insertion into a Binary Search Tree
  • 43. Chapter 8: Trees 43 Example Growing a Binary Search Tree 1. insert 7 2. insert 3 3. insert 9 4. insert 5 5. insert 6 7 3 9 5 6
  • 44. Chapter 8: Trees 44 Binary Search Tree Add Algorithm 1. if root is null 2. replace empty tree with new data leaf; return true 3. if item equals root.data 4. return false 5. if item < root.data 6. return insert(left subtree, item) 7. else 8. return insert(right subtree, item)
  • 45. Chapter 8: Trees 45 BinarySearchTree.add(E e) public boolean add (E e) { root = add(root, item); return addReturn; }
  • 46. Chapter 8: Trees 46 BinarySearchTree.add(E e) (2) private Node<E> add (Node<E> n, E e) { if (n == null) { addReturn = true; return new Node<E>(e); } int comp = e.compareTo(n.data); if (comp == 0) addReturn = false; else if (comp < 0) n.left = add(n.left , e); else n.right = add(n.right, e); return n; }
  • 47. Chapter 8: Trees 47 Removing from a Binary Search Tree • Item not present: do nothing • Item present in leaf: remove leaf (change to null) • Item in non-leaf with one child: Replace current node with that child • Item in non-leaf with two children? • Find largest item in the left subtree • Recursively remove it • Use it as the parent of the two subtrees • (Could use smallest item in right subtree)
  • 48. Chapter 8: Trees 48 Removing from a Binary Search Tree (2)
  • 49. Chapter 8: Trees 49 Example Shrinking a Binary Search Tree 1. delete 9 2. delete 5 3. delete 3 7 3 9 5 6 1 2 6 2
  • 50. Chapter 8: Trees 50 BinarySearchTree.delete(E e) public E delete (E e) { root = delete(root, item); return deleteReturn; } private Node<E> delete (Node<E> n E e) { if (n == null) { deleteReturn = null; return null; } int comp = e.compareTo(n.data); ...
  • 51. Chapter 8: Trees 51 BinarySearchTree.delete(E e) (2) if (comp < 0) { n.left = delete(n.left , e); return n; } else if (comp > 0) { n.right = delete(n.right, e); return n; } else { // item is in n.data deleteReturn = n.data; ... } }
  • 52. Chapter 8: Trees 52 BinarySearchTree.delete(E e) (3) // deleting value in n: adjust tree if (n.left == null) { return n.right; // ok if also null } else if (n.right == null) { return n.left; } else { // case where node has two children ... }
  • 53. Chapter 8: Trees 53 BinarySearchTree.delete(E e) (4) // case where node has two children if (n.left.right == null) { // largest to left is in left child n.data = n.left.data; n.left = n.left.left; return n; } else { n.data = findLargestChild(n.left); return n; }
  • 54. Chapter 8: Trees 54 findLargestChild // finds and removes largest value under n private E findLargestChild (Node<E> n) { // Note: called only for n.right != null if (n.right.right == null) { // no right child: this value largest E e = n.right.data; n.right = n.right.left; // ok if null return e; } return findLargestChild(n.right); }
  • 55. Chapter 8: Trees 55 Using Binary Search Trees • Want an index of words in a paper, by line # • Put word/line # strings into a binary search tree • “java, 0005”, “a, 0013”, and so on • Performance? • Average BST performance is O(log n) • Its rare worst case is O(n) • Happens (for example) with sorted input • Ordered list performance is O(n) • Later consider guaranteeing O(log n) trees
  • 56. Chapter 8: Trees 56 Using Binary Search Trees: Code public class IndexGen { private TreeSet<String> index; public IndexGen () { index = new TreeSet<String>(); } public void showIndex () { for (String next : index) System.out.println(next); } ... }
  • 57. Chapter 8: Trees 57 Using Binary Search Trees: Code (2) public void build (BufferedReader bR) throws IOException { int lineNum = 0; String line; while ((line = bR.readLine()) != null) { // process line } }
  • 58. Chapter 8: Trees 58 Using Binary Search Trees: Code (3) // processing a line: StringTokenizer tokens = new StringTokenizer(line, ...); while (tokens.hasNextToken()) { String tok = tokens.nextToken().toLowerCase(); index.add( String.format(“%s, %04d”, tok, lineNum)); }
  • 59. Chapter 8: Trees 59 Heaps • A heap orders its node, but in a way different from a binary search tree • A complete tree is a heap if • The value in the root is the smallest of the tree • Every subtree is also a heap • Equivalently, a complete tree is a heap if • Node value < child value, for each child of the node • Note: This use of the word “heap” is entirely different from the heap that is the allocation area in Java
  • 60. Chapter 8: Trees 60 Example of a Heap
  • 61. Chapter 8: Trees 61 Inserting an Item into a Heap 1. Insert the item in the next position across the bottom of the complete tree: preserve completeness 2. Restore “heap-ness”: 1. while new item not root and < parent 2. swap new item with parent
  • 62. Chapter 8: Trees 62 Example Inserting into a Heap Insert 1 2 3 4 5 1 11 7 8 5 1 4 1 2 Add as leaf Swap up Swap up
  • 63. Chapter 8: Trees 63 Removing an Item from a Heap • Removing an item is always from the top: • Remove the root (minimum element): • Leaves a “hole”: • Fill the “hole” with the last item (lower right-hand) L • Preserve completeness • Swap L with smallest child, as necessary • Restore “heap-ness”
  • 64. Chapter 8: Trees 64 Example Removing From a Heap Remove: returns 1 3 4 5 11 7 8 5 1 4 1 2 4 4 2 Move 4 to root Swap down
  • 65. Chapter 8: Trees 65 Implementing a Heap • Recall: a heap is a complete binary tree • (plus the heap ordering property) • A complete binary tree fits nicely in an array: • The root is at index 0 • Children of node at index i are at indices 2i+1, 2i+2 3 5 11 7 8 5 4 2 0 1 2 3 4 5 2 5 4 11 8 7 0 1 2 3 4 5
  • 66. Chapter 8: Trees 66 Inserting into an Array(List) Heap 1. Insert new item at end; set child to size-1 2. Set parent to (child – 1)/2 3. while (parent ≥ 0 and a[parent] > a[child]) 4. Swap a[parent] and a[child] 5. Set child equal to parent 6. Set parent to (child – 1) / 2
  • 67. Chapter 8: Trees 67 Deleting from an Array(List) Heap 1. Set a[0] to a[size-1], and shrink size by 1 2. Set parent to 0 3. while (true) 4. Set lc to 2 * parent + 1, and rc to lc + 1 5. If lc ≥ size, break out of while (we’re done) 6. Set minc to lc 7. If rc < size and a[rc] < a[lc], set minc to rc 8. If a[parent]  a[minc], break (we’re done) 9. Swap a[parent] and a[minc]; set parent to minc
  • 68. Chapter 8: Trees 68 Performance of Array(List) Heap • A complete tree of height h has: • Less than 2h nodes • At least 2h-1 nodes • Thus complete tree of n nodes has height O(log n) • Insertion and deletion at most do constant amount of work at each level • Thus these operations are O(log n), always • Heap forms the basis of heapsort algorithm (Ch. 10) • Heap also useful for priority queues
  • 69. Chapter 8: Trees 69 Priority Queues • A priority queue de-queues items in priority order • Not in order of entry into the queue (not FIFO) • Heap is an efficient implementation of priority queue • Operations cost at most O(log n) public boolean offer (E e); // insert public E remove (); // return smallest public E poll (); // smallest or null public E peek (); // smallest or null public E element (); // smallest
  • 70. Chapter 8: Trees 70 Design of Class PriQueue<E> ArrayList<E> data; // holds the data PriQueue () // uses natural order of E PriQueue (int n, Comparator<E> comp) // uses size n, uses comp to order private int compare (E left, E right) // compares two E objects: -1, 0, 1 private void swap (int i, int j) // swaps elements at positions i and j
  • 71. Chapter 8: Trees 71 Implementing PriQueue<E> public class PriQueue<E> extends AbstractQueue<E> implements Queue<E> { private ArrayList<E> data; Comparator<E> comparator = null; public PriQueue () { data = new ArrayList<E>(); } ...
  • 72. Chapter 8: Trees 72 Implementing PriQueue<E> (2) public PriQueue (int n, Comparator<E> comp) { if (n < 1) throw new IllegalArgumentException(); data = new ArrayList<E>(n); this.comp = comp; }
  • 73. Chapter 8: Trees 73 Implementing PriQueue<E> (3) private int compare (E lft, E rt) { if (comp != null) return comp.compare(lft, rt); else return ((Comparable<E>)lft).compareTo(rt); }
  • 74. Chapter 8: Trees 74 Implementing PriQueue<E> (4) public boolean offer (E e) { data.add(e); int child = data.size() – 1; int parent = (child – 1) / 2; while (parent >= 0 && compare(data.get(parent), data.get(child)) > 0) { swap(parent, child); child = parent; parent = (child – 1) / 2; } return true; }
  • 75. Chapter 8: Trees 75 Implementing PriQueue<E> (5) public E poll () { if (isEmpty()) return null; E result = data.get(0); if (data.size() == 1) { data.remove(0); return result; } // remove last item and store in posn 0 data.set(0, data.remove(data.size()-1)); int parent = 0; while (true) ... swap down as necessary ... return result;
  • 76. Chapter 8: Trees 76 Implementing PriQueue<E> (6) // swapping down int lc = 2 * parent + 1; if (lc >= data.size()) break; int rc = lc + 1; int minc = lc; if (rc < data.size() && compare(data.get(lc), data.get(rc)) > 0) minc = rc; if (compare(data.get(parent), data.get(minc)) <= 0) break; swap(parent, minc); parent = minc;
  • 77. Chapter 8: Trees 77 Huffman Trees Problem Input: A set of symbols, each with a frequency of occurrence. Desired output: A Huffman tree giving a code that minimizes the bit length of strings consisting of those symbols with that frequency of occurrence. Strategy: Starting with single-symbol trees, repeatedly combine the two lowest-frequency trees, giving one new tree of frequency = sum of the two frequencies. Stop when we have a single tree.
  • 78. Chapter 8: Trees 78 Huffman Trees (2) Implementation approach: • Use a priority queue to find lowest frequency trees • Use binary trees to represent the Huffman (de)coding trees Example: b=13, c=22, d=32 a=64 e=103 Combine b and c: bc=35 Combine d and bc: d(bc)=67 Combine a and d(bc): a(d(bc))=131 Combine e and a(d(bc)): e(a(d(bc)))=234 ... done
  • 79. Chapter 8: Trees 79 Huffman Tree Example e=103 a=64 d=32 b=13 c=22 0 0 0 0 1 1 1 1 0 10 110 1110 1111
  • 80. Chapter 8: Trees 80 Design of Class HuffTree private BinaryTree<HuffData> huffTree; // HuffData are pairs: weight, symbol buildTree (HuffData[] input) String decode (String message) printcode (Printstream out)
  • 81. Chapter 8: Trees 81 Implementing HuffTree public class HuffTree implements Serializable { public static class HuffData implements Serializable { private double weight; private Character symbol; public HuffData (double weight, Character symbol) { this.weight = weight; this.symbol = symbol; } }
  • 82. Chapter 8: Trees 82 Implementing HuffTree (2) private BinaryTree<HuffData> tree; private static class CompHuff implements Comparator<BinaryTree<HuffData>> { public int compare ( BinaryTree<HuffData> lft, BinaryTree<HuffData> rt) { double wL = lft.getData().weight; double wR = rt .getdata().weight; return Double.compare(wL, wR); } }
  • 83. Chapter 8: Trees 83 Implementing HuffTree (3) public void buildTree (HuffData[] syms) { Queue<BinaryTree<HuffData>> q = new PriQueue<BinaryTree<HuffData>>( syms.length, new CompHuffTree()); for (HuffData sym : syms) { BinaryTree<HuffData> tree = new BinaryTree<HuffData>(sym); q.offer(tree); } ... on to second half ...
  • 84. Chapter 8: Trees 84 Implementing HuffTree (4) while (q.size() > 1) { BinaryTree<HuffData> lft = q.poll(); BinaryTree<HuffData> rt = q.poll(); double wl = lft.getData().weight; double wr = rt .getData().weight; HuffData sum = new HuffData(wl+wr, null); BinaryTree<HuffData> nTree = new BinaryTree<HuffData> (sum, lft, rt); q.offer(nTree); } this.tree = q.poll();
  • 85. Chapter 8: Trees 85 Implementing HuffTree (5) private void printCode (PrintStream out, String code, BinaryTree<HuffData> tree) { HuffData data = tree.getData; if (data.symbol != null) { if (data.symbols.equals(“ “)) out.println(“space: “ + code); else out.println(data.symbol+”: “+code); } else { printCode(out,code+”0”, tree.left ()); printCode(out,code+”1”, tree.right()); } }
  • 86. Chapter 8: Trees 86 Implementing HuffTree (6) public string decode (String msg) { StringBuilder res = new StringBuilder(); BinaryTree<HuffData> curr = tree; for (int i = 0; i < msg.length(); ++i) { if (msg.charAt(i) == ‘1’) curr = curr.getRightSubtree(); else curr = curr.getLeftSubtree(); if (curr.isLeaf()) { HuffData data = curr.getData(); res.append(data.symbol); curr = tree; } } return res.toString();