1 / 11

More Binary Trees

More Binary Trees. Wellesley College CS230 Lecture 18 Monday, April 9 Handout #29. PS4 due 1:30pm Tuesday, April 10. Overview of Today’s Lecture. More practice writing binary tree methods Discussion of binary tree traversal. MBinTree C ontract. // Class methods

lwarden
Télécharger la présentation

More Binary Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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() heightTree(3) 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() depthTree(3) 0 public static MBinTree<Integer> depthTree (int ht) { return depthTreeDepth(0,ht); } // Helper method with extra argumentpublic static MBinTree<Integer> depthTreeDepth (int depth, int ht) { } 1 1 2 2 2 2 With sharing, can use fewer nodes: depthTree(3) 0 1 2

  10. Tree Creation: breadthTree() breadthTree(12) 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 Ftree F A C D B G E preOrderPrint(Ftree) inOrderPrint(Ftree) postOrderPrint(Ftree) breadthOrderPrint(Ftree)

More Related