Presentation is loading. Please wait.

Presentation is loading. Please wait.

2015-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.

Similar presentations


Presentation on theme: "2015-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas."— Presentation transcript:

1 2015-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas Kuehne, VUW COMP 103 Thomas Kuehne Binary Trees

2 2 RECAP-TODAY RECAP  Trees for efficient access and for representing naturally occurring, hierarchical structures. TODAY  Binary Trees  Chapter 16.4

3 3 Binary Trees : BinTreeNode public class BinTreeNode { private E item; private BinTreeNode left = null, right = null; public BinTreeNode(E it) { item = it; } public BinTreeNode(E it, BinTreeNode l, BinTreeNode r ) { item = it; left = l; right = r; } public void setItem (E it) { item = i; } public E getItem() { return item; } public void setLeft ( BinTreeNode l) { left = l; } public BinTreeNode getLeft() { return left; } public void setRight ( BinTreeNode l) { right = r; } public BinTreeNode getRight() { return right; } } two children references KZA

4 4 Building a Binary Tree : Top Down root = new BinTreeNode (“G”); k = new BinTreeNode (“K”); root.setLeft(k); m = new BinTreeNode (“M”); root.setRight(m); t = new BinTreeNode (“T”); k.setLeft(t); z = new BinTreeNode (“Z”); k.setRight(z); a = new BinTreeNode (“A”); m.setLeft(a); G G T T K K M M Z Z A A

5 5 Building a Binary Tree : Bottom Up t = new BinTreeNode (“T”); z = new BinTreeNode (“Z”); k = new BinTreeNode (“K”, t, z); a = new BinTreeNode (“A”); m = new BinTreeNode (“M”, a, null); root = new BinTreeNode (“G”, k, m); G G T T K K M M Z Z A A

6 6 Building a Binary Tree : Functional Style root = new BinTreeNode (“G”, new BinTreeNode (“K”, new BinTreeNode (“T”), new BinTreeNode (“Z”) ), new BinTreeNode (“M”, new BinTreeNode (“A”), null ) ); G G T T K K M M Z Z A A Look Ma’, no temporary variables!

7 7 Counting Nodes in a Binary Tree  Visit node and – if they exist – children public int count() { int num = 1; if (left != null) num += left.count(); if (right != null)num += right.count(); return num; }

8 8 Counting Nodes public int count() { int num = 1; if (left!=null) num += left.count(); if (right!=null) num += right.count(); return num; } 1 public int count() { int num = 1; if (left!=null) num += left.count(); if (right!=null) num += right.count(); return num; } 1 public int count() { int num = 1; if (left!=null) num += left.count(); if (right!=null) num += right.count(); return num; } 1 public int count() { int num = 1; if (left!=null) num += left.count(); if (right!=null) num += right.count(); return num; } 1 public int count() { int num = 1; if (left!=null) num += left.count(); if (right!=null) num += right.count(); return num; } 1 G G T T K K M M Z Z 2 3 4 5

9 9 123 Printing the Elements  Three choices for the primitive action (here “printing”) public void printAll() { System.out.println(item); if ( left != null )left.printAll(); System.out.println(item); if ( right != null )right.printAll(); System.out.println(item); }  These choices lead to different traversal schemes

10 10 Depth first traversal Variations:  1. pre-order:node, then children  2. in-order: left child, node, right child  3. post-order:children, then node A B E FD C A B C D E F C B D A F E C D B F E A A Maze Analogy keep your left hand on the wall,  1. pre-order:first encounter  2. in-order: second encounter  3. post-order:third encounter and act on

11 11  Polish Notation + x 5 2 / - 7 3 9 plus(times(5, 2), divide( minus(7, 3), 9 ) )  Infix Notation (5 x 2 ) + ( ( 7 – 3) / 9)  Reverse Polish Notation (RPN) 5 2 x 7 3 – 9 / + Arithmetic expression trees + x / -259 73 Look Ma’, No Parentheses

12 12 Summary: Kinds of Trees  General Trees, Binary trees,..., N-ary trees  Unordered children (Set), Ordered children (List),  Optional parent references  Different purposes  storing hierarchically structured data  tracking hierarchical processes  efficient access to unstructured data (coming soon…)


Download ppt "2015-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas."

Similar presentations


Ads by Google