VIEWS: 27 PAGES: 5 POSTED ON: 3/16/2011
Trees Overview 5 5 • Tree: recursive data structure (similar to list) 4 2 4 2 – each cell may have two or more successors (children) 8 7 8 9 7 – each cell has at most one predecessor (parent) General tree Binary tree • distinguished cell called root has no parent – all cells are reachable from root 5 5 • Binary tree: tree in which each 4 cell can have at most two 6 children 7 8 8 Not a tree List-like tree Terminology Class for binary tree cells • Directed Edge A B: A is said to be parent of B, and B is said class TreeCell { to be its child protected Object datum; protected TreeCell left; • Generalization of parent and protected TreeCell right; child: ancestor and descendant 5 Root of tree – root and A are ancestors of B Left sub-tree of root Right sub-tree of root public TreeCell(Object o) { • Leaf node: node with no datum = o; descendants A 4 2 } public TreeCell(Object o, TreeCell l, TreeCell r) { • Depth of node: length of path datum = o; from root to that node B 7 8 left = l; – depth(A) = 1 depth(B) = 2 right = r; • Height of node: length of Binary tree } longest path from node to leaf methods called getDatum, setDatum, – height(A) = 1 height(B) = 0 getLeft, setLeft, getRight, setRight • Height of tree = height of root with obvious code } – in example, height of tree = 2 1 Class for general trees 5 Applications of trees 4 2 • Most languages (natural and computer) have a class GTreeCell{ recursive, hierarchical structure. 7 8 9 protected Object datum; • This structure is implicit in ordinary textual protected GTreeCell left; representation. protected GTreeCell sibling; 7 8 3 1 ….appropriate getter and setter methods • Recursive structure can be made explicit by } General tree representing sentences in the language as trees: 5 abstract syntax trees (AST’s) • Parent node points directly only to its leftmost child. 4 2 • AST’s are easier to optimize, generate code • Leftmost child has pointer to next sibling which points from, etc. than textual representation. to next sibling etc. 7 8 9 • Converting textual representations to AST: job of parser 7 8 3 1 Tree represented using GTreeCell AST generation Example for simple expression language • Expression grammar: Text Tree representation static TreeCell expCodeGen(String fileName) {//returns AST for expression in file E integer CS211In f = new CS211In(fileName); return getExp(f); //no error checking to keep things simple E (E + E) -34 -34 } • Tree representation: static TreeCell getExp(CS211In f) {//no error checking to keep it simple – first rule: create a node with switch (f.peekAtKind()) { integer (2 + 3) + case CS211In.INTEGER: //E integer – second rule: create a node with return new TreeCell(f.getInt()); “+” as datum, tree for first case CS211In.OPERATOR: //E (E+E) 2 3 operand as its left sub-tree, and { f.check(‘(‘); tree for second operand as its TreeCell left = getExp(f); right sub-tree f.check(‘+’); TreeCell right = getExp(f); • In textual representation, f.check(‘)’); parentheses show hierarchical ((2+3) + (5+7)) + return new TreeCell(“+”, left, right); structure. In tree representation, } hierarchy is explicit in the default: return null; //error + + } structure of the tree. } 2 3 5 7 2 Recursion on trees Tree search • Recursive methods can be written to • Analog of linear search in lists: given tree operate on trees in the obvious way. and an object, find out if object is stored in tree. • In most problems • Trivial to write recursively; much harder to – base case: empty tree write iteratively. • sometimes base case is leaf node – recursive case: solve problem on left and right public static boolean treeSearch(Object o, TreeCell t) { 2 if (t == null) return false; sub-trees, and then put solutions together to else return t.getDatum().equals(o) || 9 0 compute solution for tree treeSearch(o, t.getleft()) || treeSearch(o, t.getRight()); 2 3 5 7 } Walks of tree In-order and post-order walks 1 • In-order walk: infix public static boolean treeSearch(Object o, • Example on last – process left sub-tree TreeCell t) { 2 + 3 if (t == null) return false; slide showed pre- – process root else return – process right sub-tree treeSearch(o, t.getleft()) || order walk of tree: _ x t.getDatum().equals(o) || – process root treeSearch(o, t.getRight()); 2 5 3 4 } – process left sub-tree – process right sub-tree • Post-order walk: postfix public static boolean treeSearch(Object o, public static boolean treeSearch(Object o, TreeCell t) { – process left sub-tree TreeCell t) { • Intuition: think of if (t == null) return false; if (t == null) return false; – process right sub-tree prefix representation else return else return t.getDatum().equals(o) || – process root treeSearch(o, t.getleft()) || of expressions treeSearch(o, t.getleft()) || treeSearch(o, t.getRight())|| treeSearch(o, t.getRight()); t.getDatum().equals(o); } } 3 Some useful routines Example //determine is a TreeCell is a leaf node public static boolean isLeaf(TreeCell t) { • Generate textual representation from AST. return (t != null) && (t.getLeft() == null) && (t.getRight() == null); } //compute height of tree using post-order walk public static String flatten(TreeCell t) { public static int height(TreeCell t) { if (t == null) return “”; if (t == null) return –1; //height is undefined for empty tree if (isLeaf(t)) return t.getDatum(); if (isLeaf(t)) return 0; else return “(“ + flatten(t.getLeft()) + t.getDatum() + flatten(t.getRight()) + “)” ; else return 1 + Math.max(height(t.getLeft()), height(t.getRight())); } + //compute number of nodes in tree using post-order walk public static int nNodes(TreeCell t) { + + if (t == null) return 0; else return 1 + nNodes(t.getLeft()) + nNodes(t.getRight()); 2 3 5 7 } Useful facts about binary trees Tree with header element depth • Maximum number of 0 • As in case of lists, some authors prefer to 5 nodes at depth d = 2d have an explicit Tree class which contains • If height of tree is h, 1 4 2 a reference to the root of the tree. – minimum number of nodes it can have = h+1 2 7 8 0 4 • With this design, methods that operate on – maximum number of nodes Height 2, trees can be made into instance methods it can have is = maximum number of nodes in this class, and the root of the tree does 20 + 21 + … + 2h = 2h+1 -1 5 not have to be passed in explicitly to • Full binary tree of height h: 2 method. – all levels of tree upto depth • Feel free to use whatever works for you. h are completely filled. 4 Height 2, minimum number of nodes 4 Tree with parent pointers Summary • In some applications, it is • Tree is a recursive data structure built from useful to have trees in which TreeCell class. nodes other than root have references to their parents. 5 – special case: binary tree • Tree analog of doubly-linked • Binary tree cells have both a left and a right 4 2 lists. “successor” class TreeWithPPCell{ – called children rather than successors 7 8 – similarly, parent rather than predecessor protected Object datum; protected TreeWithPPCell – generalization of parent and child to ancestors and left, right, parent; descendants …..appropriate getter and • Trees are useful for exposing the recursive setter methods… structure of natural language programs and } computer programs. 5