Announcement • Final Exam Trees I – Tuesday, Feb 25 th – 12:30pm – 2:30pm – 01-A155 Definitions, Traversals, Binary Trees • Please report all exam conflicts now! Announcement Questions • Project 2 • On sorting, searching? – Minimum submission due Sunday, Feb 2 nd • Any other questions? Trees Trees I think that I shall never • In CS, we look at trees see from the bottom up A poem lovely as a tree -- J. Kilmer
Anatomy of a Tree Anatomy of a Tree Internal Root a a • Subtree b,c are childen of a nodes – All children of a node, a is the parent of b & can be considered the b c Level 1 b c c root of it’s own tree – These are called d e f g Level 2 d e f g This tree has a depth subtrees of 3 – I smell recursion! h i j h i j Level 3 Leaves Anatomy of a Tree Traversing a Tree a • Balanced Tree • A means to process all the nodes in a tree – A tree is balanced if all – A traversal starts at the root subtrees at the same b c – Visits each node exactly once level have the same • “Processes” the data in a node when visited depths – Nodes can be visited in different orders – This tree is not d e f g balanced since Node • Breadth-first traversal c’s subtree has a depth • Depth-first traversal of 2 but Node b’s – Preorder h i j subtree has a depth of – Inorder 1. – Postorder Anatomy of a Tree Anatomy of a Tree a • Breadth-first a • Pre-order – All the nodes at a given – At each node level are visited before b c b c • The node is visited first the nodes at the next • Pre-order traversal of level the left subtree d e f g d e f g • Pre-order traversal of – Example: the right subtree • a,b,c,d,e,f,g,h,i – Example: h i h i • a,b,d,h,i,e,c,f,g
Anatomy of a Tree Anatomy of a Tree a • in-order a • post-order – At each node – At each node b c • In-order traversal of the b c • Post-order traversal of left subtree the left subtree • The node is visited next • Post-order traversal of d e f g d e f g the right subtree • The In-order traversal of the right subtree • The node is visited next – Example: – Example: h i h i • h,d,i,b,e,a,f,c,g • h,i,d,e,b,f,g,c,a What are trees good for? What are trees good for? • Hierarchical relationships • Binary trees can be used to represent decision taxonomies Performer isA isA Mammal? Actor Musician no yes Bigger than a cat? Underwater? Guitarist Pianist Drummer yes no no yes Elephant Mouse Trout Bird What are trees good for? What are trees good for? • Branching can also imply ordering of node • Representing Hierarchical File Structures data – Hmmm… \ 45 > < bin src doc 30 55 < > assn1 assn2 > < 22 35 50 60 – Questions? C++ java
Anatomy of a Binary Tree Anatomy of a Binary Tree • Binary Tree a a • Full Binary Tree – Each node has at most – A binary tree is full if 2 children b c b c • All of it’s leaf nodes are – Children of nodes in a of the same depth binary tree are referred • Each non-leaf node has to as left child or right d e f g d e f g 2 children child • Node h is the left child of Node f h i • Node i is the right child of Node f Anatomy of a Tree Implementing a Binary Tree a • Complete Binary Tree • Define a binary tree node object – A binary tree is • Each node can be seen as the root of a complete if b c Binary Tree. • Each level (except the deepest) must contain d e f g as many nodes as possible • At the deepest level, all nodes as as far left as h i possible A Binary Tree Node Class BTNode • Class BTNode public class BTNode { – Member variables protected Object data; • data – data stored within the node (Object) protected BTNode leftChild; • leftChild – left subtree (BTNode) protected BTNode rightChild; • rightChild – right subtree(BTNode) protected BTNode parent; • parent – parent node (BTNode) – Methods • Constructors (for internal node, for leaf) • Get methods (getData, getLeft, getRight, getParent) • Set methods (setData, setLeft, setRight, setParent) • Traversal methods (inorder, preorder, postorder, visit)
BTNode -- constructors BTNode – get Methods // Get the right child // Constructor for interior node // get the Data public BTNode getRight () public BTNode (Object o, BTNode l, BTNode r) public Object getData() { { { return rightChild; data = o; } return data; parent = null; setLeft (l); } setRight(r); // Get the parent } // Get the left child public BTNode getParent () { public BTNode getLeft () // Constructor for a leaf return parent; public BTNode (Object o) { } { return leftChild; data = o; } parent = null; setLeft (null); setRight (null); } BTNode – set Methods BTNode – traversal // Set the right child // set the Data • Visit public void setRight (BTNode n) public void setData(Object o) { { – Default is to print rightChild = n; data = o; if (n!= null) – Assume will be overridden by subclasses } n.setParent (this); } // Set the left child public void setLeft (BTNode n) public void visit() // Set the parent { { public void setParent (BTNode n) leftChild = n; System.out.println (data.toString()); { if (n!= null) } parent = n; n.setParent (this); } } BTNode – traversal BTNode – traversal • Inorder • But won’t this recursion go on forever? – Process left child – Visit node – Process right child public void inorder() { leftChild.inorder(); visit(); rightChild.inorder(); }
BTNode – traversal BTNode – traversal • Inorder • Pre-order, post-order public void preorder() – Process left child { – Visit node visit(); if (leftChild != null) leftChild.preorder(); – Process right child if (rightChild != null) rightChild.preorder(); } Test Stop = do nothing public void inorder() public void postorder() { { if (leftChild != null) leftChild.inorder(); if (leftChild != null) leftChild.postorder(); visit(); if (rightChild != null) rightChild.postorder(); if (rightChild != null) rightChild.inorder(); visit(); } } Continue BTNode – let’s build a tree What can we do with binary trees? public static void main (String args[]) • Binary trees can be used to represent { // Level 2 arithmetic expressions. b BTNode h = new BTNode (“h”); BTNode i = new BTNode (“i”); – Interior nodes are operator (+, --, *, /) // Level 1 d e BTNode d = new BTNode (“d”, h, i); – Leaves are operands (numbers) BTNode e = new BTNode (“e”); – Example // Root + Operator BTNode root = new BTNode (“b”, d, e); • 5 + 7 h i // Do an inorder traversal root.inorder (); 5 7 Operand } Expression trees Expression Trees • The operand of one node, can itself be an – (2 * 3 ) + ((1 + 7) * 8) expression + – Children nodes can be roots of their own subtrees * * • Example: 2 3 8 – (2 * 3 ) + ((1 + 7) * 8) + 1 7
Expression trees Expression Trees • Evaluating expression trees – (2 * 3 ) + ((1 + 7) * 8) 70 – Leaf nodes evaluate to the number that they + represent 64 6 – Interior nodes: * * 8 1. Evaluate the left and right children 2 3 8 2. Apply appropriate operation on results of Step 1 2 3 8 + 1 7 What kind of traversal would this be? 1 7 Expression trees Expression trees • Let’s implement this public int eval() { int left = 0; int right = 0; public class ExpressionTreeNode extends BTNode { if (leftChild != null) left = leftChild.eval(); public int eval(); if (rightChild != null) right = rightChild.eval(); } if (data.equals (“+”)) return left + right; else if (data.equals (“-”)) return left - right; else if (data.equals (“*”)) return left * right; else if (data.equals (“/”)) return left / right; else return Integer.parseInt ((String)data); } Expression trees Summary public int eval() throws NumberFormatException • Trees { int left = 0; • Binary Trees int right = 0; – Implementation if (leftChild != null) left = leftChild.eval(); if (rightChild != null) right = rightChild.eval(); – Example: Expression Trees if (data.equals (“+”)) return left + right; else if (data.equals (“-”)) return left - right; else if (data.equals (“*”)) return left * right; else if (data.equals (“/”)) return left / right; else return Integer.parseInt ((String)data); }
Binary Search Trees • Branching can also imply ordering of node data 45 > < 30 55 < > > < 22 35 50 60
Recommend
More recommend