Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Binary Trees Wellesley College CS230 Lecture 18 Monday, April 9

Similar presentations


Presentation on theme: "More Binary Trees Wellesley College CS230 Lecture 18 Monday, April 9"— Presentation transcript:

1 More Binary Trees Wellesley College CS230 Lecture 18 Monday, April 9
Handout #29 PS4 due 1:30pm Tuesday, April 10

2 Overview of Today’s Lecture
More practice writing binary tree methods Discussion of binary tree traversal

3 MBinTree Contract // Class methods
public static <T> MBinTree<T> leaf() ; public static <T> boolean isLeaf(MBinTree<T> tr) ; public static <T> MBinTree<T> node(MBinTree<T> lt, T val, MBinTree<T> rt) ; public static <T> T value (MBinTree<T> tr) ; public static <T> MBinTree<T> left (MBinTree<T> tr) ; public static <T> MBinTree<T> right (MBinTree<T> tr) ; public static <T> void setValue (MBinTree<T> tr, T newValue) ; public static <T> void setLeft (MBinTree<T> tr, MBinTree<T> newLeft) ; public static <T> void setRight (MBinTree<T> tr, MBinTree<T> newRight) ; // There are other class methods like size(), copy(), fromString(), // and traversal methods. // Instance methods public String toString (); public boolean equals (Object x);

4 Our Own toString() Class Method
Ftree F public static <T> String  toString (MBinTree<T> tr) { } A C D B G E toString(Ftree)  “(((* D *) A ((* E *) B *)) F (* C (* G *)))” Notes: (1) The toString() instance method is similar; (2) There is an “inverse” method public static MBinTree<String> fromString(String s)

5 Making a Shallow Copy of a Tree
copy(Ftree) public static <T> MBinTree<T>  copy (MBinTree<T> tr) { } Ftree F F A A C C D D B B G G E E

6 Non-Destructive Mapping Example
mapLowerCaseND(Ftree) public static MBinTree<String>  mapLowerCaseND (MBinTree<String> tr) { } Ftree F f A a C c D d B b G g E e

7 Destructive Mapping Example
State of Ftree after mapLowerCaseD(Ftree) public static void  mapLowerCaseD (MBinTree<String> tr) { } Ftree f a c d b g e

8 Tree Creation: heightTree()
3 public static MBinTree<Integer> heightTree (int ht) { } 2 2 1 1 1 1 With sharing, can use fewer nodes: heightTree(3) 3 2 1

9 Tree Creation: depthTree()
public static MBinTree<Integer> depthTree (int ht) { return depthTreeDepth(0,ht); } // Helper method with extra argument public static MBinTree<Integer> depthTreeDepth (int depth, int ht) { 1 1 2 2 2 2 With sharing, can use fewer nodes: depthTree(3) 1 2

10 Tree Creation: breadthTree()
public static MBinTree<Integer> breadthTree (int n) { return breadthTreeAux(1,n); } // Helper method public static MBinTree<Integer> breadthTreeAux (int root, int n) { 1 2 3 4 5 6 7 8 9 10 11 12 The tree values in breadthTree() are the binary addresses of the nodes.

11 Binary Tree Traversal preOrderPrint(Ftree)  inOrderPrint(Ftree) 
C D B G E preOrderPrint(Ftree)  inOrderPrint(Ftree)  postOrderPrint(Ftree)  breadthOrderPrint(Ftree) 


Download ppt "More Binary Trees Wellesley College CS230 Lecture 18 Monday, April 9"

Similar presentations


Ads by Google