What is a ?

Non-linear • 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 : 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 • Object is root, other classes are descendents Start head root Decision tree # before 1 (prev) 1 (parent) • Binary tree • 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 Cohen

Binary Tree ADT Binary Tree Traversal

leftChild(v): returns left child of v Preorder: node, left, right rightChild(v): returns right child of v Postorder: left, right, node sibling(v): returns sibling of v Inorder: left, node, right

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

Euler Tour Traversal Euler Tour

public void eulerTour(Tree T, position v) { visitPre(T,v); Generalizes preorder, inorder, and postorder if (T.hasLeft(v) eulerTour(T, T.leftChild(v)); Visit each internal node 3 times visitIn(T,v); if (T.hasRight(v) eulerTour(T, T.rightChild(v)); visitPost(T,v); return; }

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

9 Template Method Pattern Useful Binary Tree Definitions

Implement template or skeleton method for Level d: All nodes in a binary tree at depth d high-level algorithm • Maximum of 2d nodes in level d Extend the template’s class to override lower-level methods Complete binary tree: tree of height h with 2h leaf nodes EulerTour example • 2h-1 internal nodes • Override methods for visitPre, visitIn, and • Override methods for visitPre, visitIn, and h+1 visitPost • 2 -1 total 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

Binary Tree Properties Implementing Binary Tree

Linked (proper) Binary tree T of height h • Each node references left, right, and parent as h • Each node references left, right, and parent as • h+1 ≤ external nodes ≤ 2 well as element • h ≤ internal nodes ≤ 2h-1 Vector-based →2h+1 ≤ total nodes ≤ 2h+1-1 • Number nodes in level order →log(n+1)-1 ≤ h ≤ (n-1)/2 • Store nodes at rank according to number • external nodes = internal nodes+1 —Storage allocated for entire complete tree

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

10