Data Structures in Java
Lecture 8: Trees and Tree Traversals.
10/5/2015
1
Daniel Bauer
Data Structures in Java Lecture 8: Trees and Tree Traversals. - - PowerPoint PPT Presentation
Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 Daniel Bauer 1 Trees in Computer Science A lot of data comes in a hierarchical/nested structure. Mathematical expressions. Program structure. File systems.
Lecture 8: Trees and Tree Traversals.
10/5/2015
1
Daniel Bauer
Family Trees, …
+ + * a b c d e f g * + *
(a + b * c) + (d * e + f) * g
speed up algorithms.
certain problems:
set, add, remove, find, … T
set, add, remove, find, …
r
T1 T2 Tn
A B C E
F
G
D
Root Leaves
A B C
E F G
D Parent of B, C, D Children of A
A B C E F
G
D Siblings
A B C E F
G
D Parent of B, C, D Children of A Grandchildren of A Grandparent of E, F
A 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
A 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
A B C E F G D
Height of tree T: the length of the longest path from root to a leaf.
height = 3
references to children. n0 n1 n2 n3
two.
and algorithms.
public 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; … } … }
n0 n1 n2 n3 1st child next sibling
1st child next sibling
G C A
D
B E
F
G C A
D
B E
F
public class LinkedSiblingTree<E> { private TreeNode<E> root; private static class TreeNode<E> { E element; TreeNode<E> firstChild; TreeNode<E> nextSibling; … } ... }
G C A
D
B E
F
not full
G C A
D
B E
F
full
G C A
D
B E
F
which all levels (except possibly the last) are completely filled.
G C A
D
B E
F
full, but not complete
which all levels (except possibly the last) are completely filled.
C A
D
B E
levels (except possibly the last) are completely filled and every node is as far left as possible.
C A
D
B E
complete
levels (except possibly the last) are completely filled and every node is as far left as possible.
C A
D
B E
complete but not full
levels (except possibly the last) are completely filled and every node is as far left as possible.
F
C A
D
B E
F
A B C D E F
1 2 3 4 5 Structure of the tree only depends on the number of nodes.
+ + * a b c d e f g * + *
+ + * a b c d e f g * + *
(a + b * c) + (d * e + f) * g
+ + * a b c d e f g * + *
a b c * + d e * f + g * +
+ + * a b c d e f g * + *
+ + a * b c * + * d e f g
+
+ * a b c d e f g * + *
+
+
+
+ * a b c d e f g * + *
+ +
+
+
+ * a b c d e f g * + *
+ + a
a
+
+ * a b c d e f g * + *
+ + a
Depending on traversal order (in-/post order), keep node on stack
+
+
+ * a b c d e f g * + *
+ + a *
*
+
+ * a b c d e f g * + *
+ + a *
b
b
+
+ * a b c d e f g * + *
+ + a * b
*
+
+ * a b c d e f g * + *
+ + a * b
c
c
+
+ * a b c d e f g * + *
+ + a b c
*
*
+
+ * a b c d e f g * + *
+ a b c *
+
+
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
implementation either in BinaryNode or in BinaryTree.
5 27 2 3 * / +
c c
t1 t1
5
5 27 2 3 * / +
c c
t1 t1
5 27
5 27 2 3 * / +
c c
t1 t1
5 27 2
5 27 2 3 * / +
c c
t1 t1
5 27 2 3
5 27 2 3 * / +
c c
t1 t1
5 27 3 2 *
5 27 2 3 * / +
c c
t1 t1
5 3 2 * / 27
5 27 2 3 * / +
c c
t1 t1
3 2 * / 27 + 5