data structures in java
play

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.


  1. Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 Daniel Bauer 1

  2. 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, …

  3. Example: Expression Trees (a + b * c) + (d * e + f) * g + + * a * + g b c * f d e

  4. 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. 


  5. Tree ADT • A tree T consists of T • A root node r. • zero or more nonempty subtrees T 1 , T 2 , … T N, • each connected by a directed edge from r. • Support typical collection operations: size, get, set, add, remove, find, …

  6. Tree ADT r • A tree T consists of • A root node r. T 1 T 2 T n • zero or more nonempty subtrees T 1 , T 2 , … T N, • each connected by a directed edge from r. • Support typical collection operations: size, get, set, add, remove, find, …

  7. Tree Terminology Root A D C B Leaves F E G

  8. Tree Terminology A Parent of B, C, D Children of A C D B E F G

  9. Tree Terminology A C D Siblings B E F G

  10. Tree Terminology Grandparent of E, F A Parent of B, C, D Children of A C D B E F Grandchildren of A G

  11. Tree Terminology Path from A to E. Length = 2 A C D B E F G Path from n 1 to n k : the sequence of nodes n k , n 2 , …, n k , such that n i is the parent of n i+1 for 1 ≤ i<k. Length of a path: k-1 = number of edges on the path

  12. Tree Terminology Path from A to E A depth of E = 2 C D B E F G Depth of n k: the length of the path from root to n k.

  13. Tree Terminology height = 3 A C D B E F G Height of tree T : the length of the longest path from root to a leaf .

  14. Representing Trees • Option 1: Every node has fixed number of references to children. n 0 n 1 n 2 n 3 • Problem: Only reasonable for small or constant number of children.

  15. 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.

  16. Implementing Binary Trees 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; … } … }

  17. Representing Trees • Option 2: Organize siblings as a linked list. n 0 1st child next sibling n 1 n 2 n 3 1st child next sibling • Problem: Takes longer to find a node from the root.

  18. Siblings as Linked List A D C B F E G

  19. Siblings as Linked List A D C B F E G

  20. 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; … } ... }

  21. 
 
 
 
 
 Full Binary Trees • In a full binary tree every node • is either a leaf. • or has exactly two children. 
 A B C D E F G not full

  22. 
 
 
 
 
 Full Binary Trees • In a full binary tree every node • is either a leaf. • or has exactly two children. 
 A B C D E F G full

  23. Complete Binary Trees • A complete binary tree is a full binary tree in which all levels (except possibly the last) are completely filled. A B C D E F G

  24. Complete Binary Trees • A complete binary tree is a full binary tree in which all levels (except possibly the last) are completely filled. A B C D E F G full, but not complete

  25. Complete Binary Trees • 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. A B C D E

  26. Complete Binary Trees • 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. A B C D E complete

  27. Complete Binary Trees • 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. A B C D F E complete but not full

  28. Storing Complete Binary Trees in Arrays A B C D E F 2 0 1 3 4 5 A B C D F E Structure of the tree only depends on the number of nodes.

  29. Example Binary Tree: Expression Trees + + * a + g * b c * f d e

  30. Tree Traversals: In-order 1. Process left child (a + b * c) + (d * e + f) * g 2. Process root 3. Process right child + + * a * + g b c * f d e

  31. Tree Traversals: Post-order a b c * + d e * f + g * + 1. Process left child 2. Process right child 3. Process root + + * a * + g b c * f d e

  32. Tree Traversals: Pre-order + + a * b c * + * d e f g 1. Process root 2. Process left child 3. Process right child + + * a * + g b c * f d e

  33. Tree Traversals and Stacks • Keep nodes that still need to be processed on a stack. + + + * a * + g b c * f + d e

  34. Tree Traversals and Stacks • Keep nodes that still need to be processed on a stack. + + * + a * + g b c * + f + d e

  35. Tree Traversals and Stacks • Keep nodes that still need to be processed on a stack. a + + * a a * + g b c * + f + d e

  36. Tree Traversals and Stacks • 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. a * + g b c * + f + d e

  37. Tree Traversals and Stacks • Keep nodes that still need to be processed on a stack. a + + * a * + g * * b c * + f + d e

  38. Tree Traversals and Stacks • Keep nodes that still need to be processed on a stack. a b + + * a * + g * b c b * + f + d e

  39. Tree Traversals and Stacks • Keep nodes that still need to be processed on a stack. a b + + * a * + g * * b c * + f + d e

  40. Tree Traversals and Stacks • Keep nodes that still need to be processed on a stack. a c b + + * a * + g * b c c * + f + d e

  41. Tree Traversals and Stacks • Keep nodes that still need to be processed on a stack. a b c * + + * a * + g * b c * + f + d e

  42. Tree Traversals and Stacks • Keep nodes that still need to be processed on a stack. a b c * + + + * + a * + g b c * f + d e

  43. 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

  44. Bare-bones Implementation of a Binary Tree Public methods in BinaryTree usually call recursive methods, • implementation either in BinaryNode or in BinaryTree. (sample code) •

  45. Constructing Expression Trees using a Stack 5 27 2 3 * / + • for c in input • if c is an operand, push a tree 
 c • if c is an operator: • pop the top 2 trees t 1 and t 2 • push c t 1 t 1 • pop the result. 5

  46. Constructing Expression Trees using a Stack 5 27 2 3 * / + • for c in input • if c is an operand, push a tree 
 c • if c is an operator: • pop the top 2 trees t 1 and t 2 • push c t 1 t 1 • pop the result. 27 5

  47. Constructing Expression Trees using a Stack 5 27 2 3 * / + • for c in input • if c is an operand, push a tree 
 c • if c is an operator: • pop the top 2 trees t 1 and t 2 • push c t 1 t 1 2 • pop the result. 27 5

  48. Constructing Expression Trees using a Stack 5 27 2 3 * / + • for c in input • if c is an operand, push a tree 
 c • if c is an operator: • pop the top 2 trees t 1 and t 2 • push c 3 t 1 t 1 2 • pop the result. 27 5

  49. Constructing Expression Trees using a Stack 5 27 2 3 * / + • for c in input • if c is an operand, push a tree 
 c • if c is an operator: • pop the top 2 trees t 1 and t 2 • push c * t 1 t 1 2 3 • pop the result. 27 5

  50. Constructing Expression Trees using a Stack 5 27 2 3 * / + • for c in input • if c is an operand, push a tree 
 c • if c is an operator: • pop the top 2 trees t 1 and t 2 • push c / 27 t 1 t 1 * • pop the result. 2 3 5

  51. Constructing Expression Trees using a Stack 5 27 2 3 * / + • for c in input • if c is an operand, push a tree 
 c • if c is an operator: • pop the top 2 trees t 1 and t 2 • push c + t 1 t 1 5 / • pop the result. 27 * 2 3

Recommend


More recommend