Data Strutures in Java Leture 8: Trees and Tree Traversals. 10/5/2015 Daniel Bauer 1
Trees in Computer Siene A lot of data omes in a hierarhial/nested struture. Mathematial expressions. Program struture. File systems. Deision trees. Natural Language Syntax, Taxonomies, Family Trees,
Example: Expression Trees (a b ) (d e f) g a g b f d e
More Effiient Algorithms with Trees Sometimes we an represent data in a tree to speed up algorithms. Only need to onsider part of the tree to solve ertain problems: Searhing, Sorting, Can often speed up O(N) algorithms to O(log N) one data is represented as a tree.
Tree ADT A tree T onsists of A root node r. T zero or more nonempty subtrees T1, T2, TN, eah onneted by a direted edge from r. Support typial olletion operations: size, get, set, add, remove, find,
Tree ADT r A tree T onsists of A root node r. T1 T2 Tn zero or more nonempty subtrees T1, T2, TN, eah onneted by a direted edge from r. Support typial olletion operations: size, get, set, add, remove, find,
Tree Terminology A Root B C D Leaves E F G
Tree Terminology A Parent of B, C, D B C D Children of A E F G
Tree Terminology A B C D Siblings E F G
Tree Terminology Grandparent of E, F A Parent of B, C, D B C D Children of A E F Grandhildren of A G
Tree Terminology A Path from A to E. Length = 2 B C D E F Path from n1 to nk : the sequene of nodes nk, n2,, nk, suh that ni is the parent of ni1 for 1 i<k. G Length of a path: k-1 = number of edges on the path
Tree Terminology A Path from A to E depth of E = 2 B C D E F Depth of nk: the length of the path from root to nk. G
Tree Terminology A height = 3 B C D E F Height of tree T: the length of the longest path from root to a leaf. G
Representing Trees Option 1: Every node has fixed number of referenes to hildren. n0 n1 n2 n3 Problem: Only reasonable for small or onstant number of hildren.
Binary Trees For binary trees, the number of hildren is at most two. Binary trees are very ommon in data strutures and algorithms. Binary tree algorithms are onvenient to analyze.
Implementing Binary Trees publi lass BinaryTree<T> { // The BinaryTree is essentially just a wrapper around the // linked struture of BinaryNodes, rooted in root. private BinaryNode<T> root; } / Represent a binary subtree. / private stati lass BinaryNode<T>{ } publi T data; publi BinaryNode<T> left; publi BinaryNode<T> right;
Representing Trees Option 2: Organize siblings as a linked list. 1st hild n0 next sibling n1 n2 n3 1st hild next sibling Problem: Takes longer to find a node from the root.
Siblings as Linked List A B C D E F G
Siblings as Linked List A B C D E F G
Implementing Siblings as Linked List publi lass LinkedSiblingTree<E> { private TreeNode<E> root; private stati lass TreeNode<E> { E element; TreeNode<E> firstchild; TreeNode<E> nextsibling; } }...
Full Binary Trees In a full binary tree every node is either a leaf. or has exatly two hildren. D B A C F E G not full
Full Binary Trees In a full binary tree every node is either a leaf. or has exatly two hildren. D F B E A G C full
Complete Binary Trees A omplete binary tree is a full binary tree in whih all levels (exept possibly the last) are ompletely filled. A B C D E F G
Complete Binary Trees A omplete binary tree is a full binary tree in whih all levels (exept possibly the last) are ompletely filled. A B C D E F G full, but not omplete
Complete Binary Trees A omplete binary tree is a binary tree in whih all levels (exept possibly the last) are ompletely filled and every node is as far left as possible. A B C D E
Complete Binary Trees A omplete binary tree is a binary tree in whih all levels (exept possibly the last) are ompletely filled and every node is as far left as possible. A B C D E omplete
Complete Binary Trees A omplete binary tree is a binary tree in whih all levels (exept possibly the last) are ompletely filled and every node is as far left as possible. A B C D E F omplete but not full
Storing Complete Binary Trees in Arrays A B C D E F 0 1 2 3 4 5 A B C D E F Struture of the tree only depends on the number of nodes.
Example Binary Tree: Expression Trees a g b f d e
Tree Traversals: In-order 1. Proess left hild 2. Proess root 3. Proess right hild (a b ) (d e f) g a g b f d e
Tree Traversals: Post-order 1. Proess left hild 2. Proess right hild 3. Proess root a b d e f g a g b f d e
Tree Traversals: Pre-order 1. Proess root 2. Proess left hild 3. Proess right hild a b d e f g a g b f d e
Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a g b f d e
Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a g b f d e
Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a a g b f d e
Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a a Depending on traversal order (in-/post order), keep node on stak or pop it. Let s do post order. g b f d e
Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a a g b f d e
Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a b a g b f d e
Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a b a g b f d e
Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a b a g b f d e
Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a b a g b f d e
Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a b a g b f d e
Tree Traversal using Reursion We often use reursion to traverse trees (making use of Java s method all stak impliitly). publi 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
Bare-bones Implementation of a Binary Tree Publi methods in BinaryTree usually all reursive methods, implementation either in BinaryNode or in BinaryTree. (sample ode)
Construting Expression Trees using a Stak 5 27 2 3 / for in input if is an operand, push a tree if is an operator: pop the top 2 trees t1 and t2 push t1 t1 pop the result. 5
Construting Expression Trees using a Stak 5 27 2 3 / for in input if is an operand, push a tree if is an operator: pop the top 2 trees t1 and t2 push t1 t1 27 pop the result. 5
Construting Expression Trees using a Stak 5 27 2 3 / for in input if is an operand, push a tree if is an operator: pop the top 2 trees t1 and t2 push 2 27 t1 pop the result. t1 5
Construting Expression Trees using a Stak 5 27 2 3 / for in input if is an operand, push a tree if is an operator: 3 pop the top 2 trees t1 and t2 push 2 27 t1 pop the result. t1 5
Construting Expression Trees using a Stak 5 27 2 3 / for in input if is an operand, push a tree if is an operator: pop the top 2 trees t1 and t2 push 2 3 t1 t1 27 pop the result. 5
Construting Expression Trees using a Stak 5 27 2 3 / for in input if is an operand, push a tree if is an operator: / pop the top 2 trees t1 and t2 push 27 t1 t1 2 3 pop the result. 5
Construting Expression Trees using a Stak 5 27 2 3 / for in input if is an operand, push a tree if is an operator: pop the top 2 trees t1 and t2 push 5 / t1 t1 27 pop the result. 2 3