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

binary trees continued
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Binary Trees continued

15-121 Fall 2020 Margaret Reid-Miller

15-121 (Reid-Miller) Fall 2020 1

slide-2
SLIDE 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

slide-3
SLIDE 3

Today

Today:

  • More on Binary Trees

15-121 (Reid-Miller) 3 Fall 2020

slide-4
SLIDE 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.

15-121 (Reid-Miller) Fall 2020 4

slide-5
SLIDE 5

Binary Tree Traversals A G F C D E B

PREORDER ABDECFG INORDER DBEAFCG POSTORDER DEBFGCA

15-121 (Reid-Miller) Fall 2020 8

slide-6
SLIDE 6

Traversal Example A G E D C B

preorder inorder postorder

F H I

ABDFGCEHI BFDGAEIHC FGDBIHECA

15-121 (Reid-Miller) Fall 2020 9

slide-7
SLIDE 7

Traversals

  • n Expression Trees
  • What do you get when you perform a

inorder traversal on an expression tree?

* +

  • /

2 5 6 3 7

6 / 2 + 5 * 7 – 3

15-121 (Reid-Miller) Fall 2020 11

((6 / 2) + 5) * (7 - 3)

slide-8
SLIDE 8

Traversals

  • n Expression Trees
  • What do you get when you perform a

postorder traversal on an expression tree?

* +

  • /

2 5 6 3 7 6 2 / 5 + 7 3 - *

15-121 (Reid-Miller) Fall 2020 12

slide-9
SLIDE 9

Traversals

  • n Binary Search Trees
  • What do you get when you perform an

inorder traversal on a binary search tree?

84 41 96 24 37 50 13 98 13 24 37 41 50 84 96 98

15-121 (Reid-Miller) Fall 2020 13

slide-10
SLIDE 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.

  • Y is a descendant of X if

Y is the child of X, or Y is the descendant of the child of X.

15-121 (Reid-Miller) Fall 2020 14

It's recursive!

slide-11
SLIDE 11

Binary Trees Levels A G F E D C B

Level 0 Level 1 Level 2

15-121 (Reid-Miller) Fall 2020 15

slide-12
SLIDE 12

Binary Trees - Levels

  • The level of a node Y is
  • BASE CASE
  • RECURSIVE CASE

0, if the Y is the root 1 + the level of the parent of Y, if Y is not the root

15-121 (Reid-Miller) Fall 2020 16

slide-13
SLIDE 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
  • RECURSIVE CASE

0, if T is a leaf max(height(left(T)), height(right(T))) + 1, if T is a leaf

15-121 (Reid-Miller) Fall 2020 17

slide-14
SLIDE 14

Binary Tree Node

public class BTNode<E> { 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; } }

data

15-121 (Reid-Miller) Fall 2020 18

slide-15
SLIDE 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); }

What kind of traversal? What is the running time?

15-121 (Reid-Miller) Fall 2020 19

O(n) in worst case O(1) in best case preorder

slide-16
SLIDE 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

15-121 (Reid-Miller) Fall 2020 20

slide-17
SLIDE 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()); }

What kind of traversal? What is the running time?

15-121 (Reid-Miller) Fall 2020 21

O(n) postorder

slide-18
SLIDE 18

Grow tree t by adding leaves with value x

grow(t, "B");

15-121 (Reid-Miller) Fall 2020 22

A A A A A A A A A A B B B A B B B A B

slide-19
SLIDE 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); }

15-121 (Reid-Miller) Fall 2020 23

slide-20
SLIDE 20

Grow tree t by adding leaves x Returns reference to modified tree

public static <E> BTNode<E> grow(BTNode<E> t, E x) { if (t == null) t.setLeft( ); t.setRight(grow(t.getRight(), x)); return t; }

15-121 (Reid-Miller) Fall 2020 24

Often when you modify a tree the code is cleaner when you return the modified tree!

return new BTNode<E>(x); grow(t.getLeft(), x)

slide-21
SLIDE 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); }

Fall 2020 15-121 (Reid-Miller) 25

A handy helper function

slide-22
SLIDE 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 } BTNode<E> right = t.getRight(); if (right != null) { if (isLeaf(right)) t.setRight(null); else } }

15-121 (Reid-Miller) Fall 2020 26

prune(left); prune(right);

slide-23
SLIDE 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

15-121 (Reid-Miller) Fall 2020 27