binary trees continued
play

Binary Trees continued 15-121 Fall 2020 Margaret Reid-Miller Fall - PowerPoint PPT Presentation

Binary Trees continued 15-121 Fall 2020 Margaret Reid-Miller Fall 2020 15-121 (Reid-Miller) 1 Exam 2 is next Thursday, November 12 Topics: Writing methods for classes that implement Lists. Methods using Lists w/ ArrayList or


  1. Binary Trees continued 15-121 Fall 2020 Margaret Reid-Miller Fall 2020 15-121 (Reid-Miller) 1

  2. Exam 2 is next Thursday, November 12 Topics: • Writing methods for classes that implement Lists. • Methods using Lists w/ ArrayList or LinkedLists • Recursion – call tree, trace, implement • Interfaces • Stacks & Queues (implementations, using them) • Evaluate post-fix expressions (not implementation) • Big-O Fall 2020 15-121 (Reid-Miller) 2

  3. Today Today: • More on Binary Trees Fall 2020 15-121 (Reid-Miller) 3

  4. A binary tree is a nonlinear data structure • A binary tree is either • empty or • has a root node and left- and right-subtrees that are also binary trees. • The top node of a tree is called the root. • Any node in a binary tree has at most 2 children. • Any node (except the root) in a binary tree has exactly one parent node. Fall 2020 15-121 (Reid-Miller) 4

  5. PREORDER ABDECFG Binary Tree INORDER DBEAFCG Traversals POSTORDER DEBFGCA A B C D E F G Fall 2020 15-121 (Reid-Miller) 8

  6. Traversal Example A preorder C B ABDFGCEHI inorder D E BFDGAEIHC postorder F G H FGDBIHECA I Fall 2020 15-121 (Reid-Miller) 9

  7. Traversals on Expression Trees • What do you get when you perform a inorder traversal on an expression tree? * + - / 5 7 3 6 2 ((6 / 2) + 5) * (7 - 3) 6 / 2 + 5 * 7 – 3 Fall 2020 15-121 (Reid-Miller) 11

  8. Traversals on Expression Trees • What do you get when you perform a postorder traversal on an expression tree? * + - / 5 7 3 6 2 6 2 / 5 + 7 3 - * Fall 2020 15-121 (Reid-Miller) 12

  9. Traversals on Binary Search Trees • What do you get when you perform an inorder traversal on a binary search tree? 84 41 96 24 50 98 13 37 13 24 37 41 50 84 96 98 Fall 2020 15-121 (Reid-Miller) 13

  10. Binary trees ancestors and descendants • Consider two nodes in a tree, X and Y. • X is an ancestor of Y if X is the parent of Y, or X is the ancestor of the parent of Y. It's recursive! • Y is a descendant of X if Y is the child of X, or Y is the descendant of the child of X. Fall 2020 15-121 (Reid-Miller) 14

  11. Binary Trees Levels Level 0 A B C Level 1 D E F G Level 2 Fall 2020 15-121 (Reid-Miller) 15

  12. Binary Trees - Levels • The level of a node Y is • BASE CASE 0, if the Y is the root • RECURSIVE CASE 1 + the level of the parent of Y, if Y is not the root Fall 2020 15-121 (Reid-Miller) 16

  13. Binary Tree - Height • The height of a binary tree T is number of edges on the path from the root to the deepest leaf node. • BASE CASE 0, if T is a leaf • RECURSIVE CASE max(height(left(T)), height(right(T))) + 1, if T is a leaf Fall 2020 15-121 (Reid-Miller) 17

  14. Binary Tree Node public class BTNode<E> { data private E data; private BTNode<E> left; private BTNode<E> right; public BTNode(E d) { data = d; left = null; right = null; } public E getData() { return data; } public BTNode<E> getLeft() { return left; } public BTNode<E> getRight() { return right; } public void setData(E d) { data = d; } public void setLeft(BTNode<E> lt) { left = lt; } public void setRight(BTNode<E> rt) { right = rt; } } Fall 2020 15-121 (Reid-Miller) 18

  15. Return true if tree t contains value x public static <E> boolean contains(BTNode<E> t, E x) { if (t == null) return false; return t.getData().equals(x) || contains(t.getLeft(), x ) || contains(t.getRight(), x); } preorder What kind of traversal? O(n) in worst case What is the running time? O(1) in best case Fall 2020 15-121 (Reid-Miller) 19

  16. Return the number of leaves in tree t public static <E> int countLeaves(BTNode<E> t) { What is the base case? How do you determine if a node is a leaf? What do the recursive calls return? One in each group submit your answer to https://forms.gle/X2jbKYgZXVFGprat8 Fall 2020 15-121 (Reid-Miller) 20

  17. Return the number of leaves in tree t public static <E> int countLeaves(BTNode<E> t) { if (t == null) return 0; if (t.getLeft()==null && t.getRight()==null) return 1; else return countLeaves(t.getLeft()) + countLeaves(t.getRight()); } postorder What kind of traversal? What is the running time? O(n) Fall 2020 15-121 (Reid-Miller) 21

  18. Grow tree t by adding leaves with value x grow(t, "B"); A A A A A A A A A A A B A B B B B B B Fall 2020 15-121 (Reid-Miller) 22

  19. Grow tree t by adding leaves with value x // precondition: t != null public static <E> void grow(BTNode<E> t, E x){ if (t.getLeft() == null) t.setLeft(new BTNode<E>(x)); else grow(t.getLeft(), x); if (t.getRight() == null) t.setRight(new BTNode<E>(x)); else grow(t.getRight(), x); } Fall 2020 15-121 (Reid-Miller) 23

  20. Grow tree t by adding leaves x Returns reference to modified tree public static <E> BTNode<E> grow(BTNode<E> t, E x) { return new BTNode<E>(x); if (t == null) t.setLeft( ); grow(t.getLeft(), x) t.setRight(grow(t.getRight(), x)); return t; } Often when you modify a tree the code is cleaner when you return the modified tree! Fall 2020 15-121 (Reid-Miller) 24

  21. Determine if a tree t is a leaf // precondition: t != null private static <E> boolean isLeaf(BTNode<E> t){ return (t.getLeft() == null && t.getRight() == null); } A handy helper function Fall 2020 15-121 (Reid-Miller) 25

  22. Remove all leaves from tree t public static <E> void prune(BTNode<E> t){ if (t == null) return; BTNode<E> left = t.getLeft(); if (left != null) { if (isLeaf(left)) t.setLeft(null); else prune(left); } BTNode<E> right = t.getRight(); if (right != null) { if (isLeaf(right)) t.setRight(null); prune(right); else } } Fall 2020 15-121 (Reid-Miller) 26

  23. Remove all leaves from tree t Return reference to the resulting tree public static <E> BTNode<E> prune(BTNode<E> t){ What are the base case(s)? What does the recursive cases return? How do you update the tree? Submit answers to https://forms.gle/a5qj8rTF1tndp1tH6 Fall 2020 15-121 (Reid-Miller) 27

Recommend


More recommend