Trees What Is a Tree? Components of a Tree

Trees What Is a Tree? Components of a Tree

What is a Tree? Non-linear data structure • Hierarchical arrangement of data Trees Has components named after natural trees • root • branches • leaves Drawn with root at the top Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen Components of a Tree More Tree Terminology Node: stores a data element Leaf (external) node: node with no children Parent: single node that directly precedes a Internal node: non-leaf node node Siblings: nodes which share same parent • all nodes have 1 parent except root (has 0) Subtree: a node and all its descendents Child: one or more nodes that directly follow • Ignoring the node’s parent, this is itself a tree a node Ordered tree: tree with defined order of Ancestor: any node which precedes a node children • itself, its parent, or an ancestor of its parent • enables ordered traversal Descendent: any node which follows a node Binary tree: ordered tree with up to two • itself, its child, or a descendent of its child children per node Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen Examples of Trees Comparison of Tree and List Directory tree • Organizes directories and files hierarchically • Directories are internal nodes, files are leaf nodes (usually) List Tree Class hierarchy • Object is root, other classes are descendents Start head root Decision tree # before 1 (prev) 1 (parent) • Binary tree • Path taken determined by boolean expression # after 1 (next) >= 1 (children) Expression tree • Operators are internal nodes, variables and constants are leaf nodes Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen 1 Tree methods Tree query utility methods root( ): returns root parent(v): returns parent of v children(v): returns iterator of children of v isInternal(v): test if node is internal size( ): returns number of nodes isExternal(v): test if node is external elements( ): returns iterator of all elements positions( ): returns iterator of all positions/nodes isRoot(v): test if node is root swapElements(v,w): swaps elements at two nodes replaceElement(v,e): replaces element of a node Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen Depth Height Depth of v is numbers of ancestors (excluding v) Height of v is maximum path length of • depth of root is 0 subtree • depth of node is depth of parent plus 1 • height of leaf node is 0 public static int depth (Tree T, Node v) { • height of internal node is maximum height of if (T.isRoot(v)) return 0; children + 1 else return 1 + depth(T, T.parent(v)); —height of a tree is height of root or maximum depth of a leaf } // running time? O(d(dvv)) Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen Naïve height algorithm Efficient height algorithm public static int height1(Tree T) { public static int height2(Tree T, Position v) { int h=0; if (T.isExternal(v)) return 0 else { PositionIterator it = T.positions(); else { int h=0; while (it.hasNext()) { PositionIterator children = T.children(v); Position v = it.nextPosition(); while (children.hasNext()) h = Math.max(h, if (T.isExternal(v)) height2(T,children.nextPosition())); h = Math.max(h, depth(T,v)); } return 1 + h; } } // running time? return h; } // running time? O(n^2) O(n^2) O((n):): eacheach nodenode visitedvisited onceonce Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen 2 Traversal Preorder Traversal Visit node, then visit children public void preorder(Tree T, Position v) { Ordered way of visiting all nodes of tree visit(v); Converts hierarchy into a linear sequence PositionIterator children = T.children(v); while (children.hasNext()) preorder(children.nextPosition()); } Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen Preorder Example Preorder Example Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen Preorder Example Preorder Example Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen 3 Preorder Example Preorder Example Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen Preorder Example Preorder Example Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen Preorder Example Preorder Example Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen 4 Preorder Example Preorder Example Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen Preorder Example Postorder Traversal Visit children, then visit node public void postorder(Tree T, Position v) { PositionIterator children = T.children(v); while (children.hasNext()) postorder(children.nextPosition()); visit(v); } Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen Postorder Example Postorder Example Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen 5 Postorder Example Postorder Example Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen Postorder Example Postorder Example Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen Postorder Example Postorder Example Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen 6 Postorder Example Postorder Example Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen Postorder Example Postorder Example Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen Postorder Example Postorder Example Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen 7 Postorder Example Postorder Example Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen Postorder Example Postorder Example Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen InIn-class-class Exercises... Preorder Letter Scramble ‘S’ ‘l’ ‘e’ ‘l’ ‘t’ ‘c’ (Paper and pencil recommended) ‘e’ ‘f’ ‘r’ ‘l’ ‘!’ ‘e’ ‘a’ ‘ ’ ‘s’ ‘a’ ‘s’ ‘p’ ‘a’ ‘ ’ ‘s’ Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen 8 Postorder Letter Scramble Binary Tree ‘!’ ‘e’ ‘e’ ‘r’ ‘r’ ‘r’ ‘ ’ ‘z’ Each node has no more than 2 children ‘r’ ‘T’ ‘e’ • Proper binary tree: each node has either 0 or 2 ‘a’ ‘ ’ ‘a’ ‘e’ ‘e’ ‘b’ children ‘s’ ‘ ’ ‘e’ Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us