Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data...
Transcript of Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data...
![Page 1: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/1.jpg)
Data Structures in JavaLecture 8: Trees and Tree Traversals.
10/5/2015
1
Daniel Bauer
![Page 2: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/2.jpg)
Trees in Computer Science• A lot of data comes in a hierarchical/nested structure.
• Mathematical expressions.
• Program structure.
• File systems.
• Decision trees.
• Natural Language Syntax, Taxonomies, Family Trees, …
![Page 3: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/3.jpg)
Example: Expression Trees
+
+ *
a
b c
d e
f
g* +
*
(a + b * c) + (d * e + f) * g
![Page 4: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/4.jpg)
More Efficient Algorithms with Trees
• Sometimes we can represent data in a tree to speed up algorithms.
• Only need to consider part of the tree to solve certain problems:
• Searching, Sorting,…
• Can often speed up O(N) algorithms to O(log N) once data is represented as a tree.
![Page 5: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/5.jpg)
Tree ADT
• A tree T consists of
• A root node r.
• zero or more nonempty subtrees T1, T2, … TN,
• each connected by a directed edge from r.
• Support typical collection operations: size, get, set, add, remove, find, …
T
![Page 6: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/6.jpg)
Tree ADT
• A tree T consists of
• A root node r.
• zero or more nonempty subtrees T1, T2, … TN,
• each connected by a directed edge from r.
• Support typical collection operations: size, get, set, add, remove, find, …
r
T1 T2 Tn
![Page 7: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/7.jpg)
Tree TerminologyA
B C
E F
G
D
Root
Leaves
![Page 8: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/8.jpg)
Tree TerminologyA
B C
E F
G
D
Parent of B, C, D
Children of A
![Page 9: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/9.jpg)
Tree TerminologyA
B C
E F
G
D Siblings
![Page 10: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/10.jpg)
Tree TerminologyA
B C
E F
G
D
Parent of B, C, D
Children of A
Grandchildren of A
Grandparent of E, F
![Page 11: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/11.jpg)
Tree TerminologyA
B C
E F
G
D
Path from A to E. Length = 2
Path from n1 to nk : the sequence of nodes nk, n2, …, nk, such that ni is the parent of ni+1 for 1≤i<k.
Length of a path: k-1 = number of edges on the path
![Page 12: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/12.jpg)
Tree TerminologyA
B C
E F
G
D
Path from A to E
Depth of nk: the length of the path from root to nk.
depth of E = 2
![Page 13: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/13.jpg)
Tree TerminologyA
B C
E F
G
D
Height of tree T: the length of the longest path from root to a leaf.
height = 3
![Page 14: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/14.jpg)
Representing Trees• Option 1: Every node has fixed number of
references to children.
n0
n1 n2 n3
• Problem: Only reasonable for small or constant number of children.
![Page 15: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/15.jpg)
Binary Trees
• For binary trees, the number of children is at most two.
• Binary trees are very common in data structures and algorithms.
• Binary tree algorithms are convenient to analyze.
![Page 16: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/16.jpg)
Implementing Binary Treespublic class BinaryTree<T> {
// The BinaryTree is essentially just a wrapper around the // linked structure of BinaryNodes, rooted in root. private BinaryNode<T> root;
/** * Represent a binary subtree. */ private static class BinaryNode<T>{ public T data; public BinaryNode<T> left; public BinaryNode<T> right; … } …}
![Page 17: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/17.jpg)
Representing Trees• Option 2: Organize siblings as a linked list.
n0
n1 n2 n3
1st child next sibling
• Problem: Takes longer to find a node from the root.
1st child next sibling
![Page 18: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/18.jpg)
Siblings as Linked List
G
C
A
DB
E F
![Page 19: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/19.jpg)
Siblings as Linked List
G
C
A
DB
E F
![Page 20: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/20.jpg)
Implementing Siblings as Linked List
public class LinkedSiblingTree<E> {
private TreeNode<E> root;
private static class TreeNode<E> { E element; TreeNode<E> firstChild; TreeNode<E> nextSibling; … }
...}
![Page 21: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/21.jpg)
Full Binary Trees• In a full binary tree every node
• is either a leaf.
• or has exactly two children.
G
C
A
D
B
E
F
not full
![Page 22: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/22.jpg)
Full Binary Trees
G
C
A
D
B
E
F
full
• In a full binary tree every node
• is either a leaf.
• or has exactly two children.
![Page 23: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/23.jpg)
Complete Binary Trees
G
C
A
D
B
E
F
• A complete binary tree is a full binary tree in which all levels (except possibly the last) are completely filled.
![Page 24: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/24.jpg)
Complete Binary Trees
G
C
A
D
B
E
F
full, but not complete
• A complete binary tree is a full binary tree in which all levels (except possibly the last) are completely filled.
![Page 25: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/25.jpg)
Complete Binary Trees
C
A
D
B
E
• A complete binary tree is a binary tree in which all levels (except possibly the last) are completely filled and every node is as far left as possible.
![Page 26: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/26.jpg)
Complete Binary Trees
C
A
D
B
E
complete
• A complete binary tree is a binary tree in which all levels (except possibly the last) are completely filled and every node is as far left as possible.
![Page 27: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/27.jpg)
Complete Binary Trees
C
A
D
B
E
complete but not full
• A complete binary tree is a binary tree in which all levels (except possibly the last) are completely filled and every node is as far left as possible.
F
![Page 28: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/28.jpg)
Storing Complete Binary Trees in Arrays
C
A
D
B
E F
A B C D E F
0 1 2 3 4 5
Structure of the tree only depends on the number of nodes.
![Page 29: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/29.jpg)
Example Binary Tree: Expression Trees
+
+ *
a
b c
d e
f
g* +
*
![Page 30: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/30.jpg)
Tree Traversals: In-order
+
+ *
a
b c
d e
f
g* +
*
(a + b * c) + (d * e + f) * g1. Process left child 2. Process root 3. Process right child
![Page 31: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/31.jpg)
Tree Traversals: Post-order
+
+ *
a
b c
d e
f
g* +
*
1. Process left child 2. Process right child 3. Process root
a b c * + d e * f + g * +
![Page 32: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/32.jpg)
Tree Traversals: Pre-order
+
+ *
a
b c
d e
f
g* +
*
1. Process root 2. Process left child 3. Process right child
+ + a * b c * + * d e f g
![Page 33: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/33.jpg)
Tree Traversals and Stacks
+
+ *
a
b c
d e
f
g* +
*
• Keep nodes that still need to be processed on a stack.
+
+
![Page 34: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/34.jpg)
Tree Traversals and Stacks
+
+ *
a
b c
d e
f
g* +
*
• Keep nodes that still need to be processed on a stack.
++
+
![Page 35: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/35.jpg)
Tree Traversals and Stacks
+
+ *
a
b c
d e
f
g* +
*
• Keep nodes that still need to be processed on a stack.
++
a
a
![Page 36: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/36.jpg)
Tree Traversals and Stacks
+
+ *
a
b c
d e
f
g* +
*
• Keep nodes that still need to be processed on a stack.
++
a
Depending on traversal order (in-/post order), keep node on stack or pop it. Let’s do post order.
+
![Page 37: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/37.jpg)
Tree Traversals and Stacks
+
+ *
a
b c
d e
f
g* +
*
• Keep nodes that still need to be processed on a stack.
++
a
**
![Page 38: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/38.jpg)
Tree Traversals and Stacks
+
+ *
a
b c
d e
f
g* +
*
• Keep nodes that still need to be processed on a stack.
++
a
*b
b
![Page 39: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/39.jpg)
Tree Traversals and Stacks
+
+ *
a
b c
d e
f
g* +
*
• Keep nodes that still need to be processed on a stack.
++
a
*
b
*
![Page 40: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/40.jpg)
Tree Traversals and Stacks
+
+ *
a
b c
d e
f
g* +
*
• Keep nodes that still need to be processed on a stack.
++
a
*
b
c
c
![Page 41: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/41.jpg)
Tree Traversals and Stacks
+
+ *
a
b c
d e
f
g* +
*
• Keep nodes that still need to be processed on a stack.
++
a b c
*
*
![Page 42: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/42.jpg)
Tree Traversals and Stacks
+
+ *
a
b c
d e
f
g* +
*
• Keep nodes that still need to be processed on a stack.
+
a b c *
+
+
![Page 43: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/43.jpg)
Tree Traversal using Recursion• We often use recursion to traverse trees (making use of
Java’s method call stack implicitly).
public void printTree(int indent ) { for (i=0;i<indent;i++) System.out.print(" ");
System.out.println( data); // Node if( left != null ) left.printTree(indent + 1); // Left if( right != null ) right.printTree(indent + 1); // Right }
30
![Page 44: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/44.jpg)
Bare-bones Implementation of a Binary Tree
• Public methods in BinaryTree usually call recursive methods, implementation either in BinaryNode or in BinaryTree.
• (sample code)
![Page 45: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/45.jpg)
Constructing Expression Trees using a Stack
5 27 2 3 * / + • for c in input
• if c is an operand, push a tree
• if c is an operator: • pop the top 2 trees t1 and t2 • push
• pop the result.
c
c
t1 t1
5
![Page 46: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/46.jpg)
Constructing Expression Trees using a Stack
5 27 2 3 * / + • for c in input
• if c is an operand, push a tree
• if c is an operator: • pop the top 2 trees t1 and t2 • push
• pop the result.
c
c
t1 t1
5
27
![Page 47: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/47.jpg)
Constructing Expression Trees using a Stack
5 27 2 3 * / + • for c in input
• if c is an operand, push a tree
• if c is an operator: • pop the top 2 trees t1 and t2 • push
• pop the result.
c
c
t1 t1
5
27
2
![Page 48: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/48.jpg)
Constructing Expression Trees using a Stack
5 27 2 3 * / + • for c in input
• if c is an operand, push a tree
• if c is an operator: • pop the top 2 trees t1 and t2 • push
• pop the result.
c
c
t1 t1
5
27
2
3
![Page 49: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/49.jpg)
Constructing Expression Trees using a Stack
5 27 2 3 * / + • for c in input
• if c is an operand, push a tree
• if c is an operator: • pop the top 2 trees t1 and t2 • push
• pop the result.
c
c
t1 t1
5
27
32
*
![Page 50: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/50.jpg)
Constructing Expression Trees using a Stack
5 27 2 3 * / + • for c in input
• if c is an operand, push a tree
• if c is an operator: • pop the top 2 trees t1 and t2 • push
• pop the result.
c
c
t1 t1
5
32
*
/
27
![Page 51: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer](https://reader033.fdocuments.net/reader033/viewer/2022042915/5f525df1bc498462834968c3/html5/thumbnails/51.jpg)
Constructing Expression Trees using a Stack
5 27 2 3 * / + • for c in input
• if c is an operand, push a tree
• if c is an operator: • pop the top 2 trees t1 and t2 • push
• pop the result.
c
c
t1 t1
32
*
/
27
+
5