Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order,...

37
Binary Trees

Transcript of Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order,...

Page 1: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Binary Trees

Page 2: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

AnnouncementsTA Office Hours - no OH tonight, during Thanksgiving break; resume next Monday Nov 30

Heather - no OH tonight; appts available 3:15-4:00 today

Assignment 6 - out today; due Dec 3

Page 3: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

OutlineMerge Sort discussion

Binary trees

Tree traversals: pre-order, in-order, post-order

Page 4: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Binary Tree Data Structure

Page 5: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

ReferencesNotes on CS201 Wiki: http://wiki.cs.mtholyoke.edu/mediawiki/cs201/index.php/Binary_trees

Chapter 9 of the Michael Main Data Structures text (on 3-hour course reserve at the library).

Page 6: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Binary Tree Data Structure

Happy

Doc

Bashful Grumpy

Sleepy

Sneezy

Page 7: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Binary Tree:Sketch of Nodes and Pointers

Page 8: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

BinaryTreeNode

leftChild rightChild

data

Page 9: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

BinaryTreeNode

leftChild rightChild

data

BinaryTreeNode

data

leftChild

rightChild

Page 10: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

BinaryTreeNode Methods

BinaryTreeNode

data

leftChild

rightChild

public T getData()

public void setData(T data)

public ______ getLeftChild()

public void setLeftChild(______)

public ______ getLeftChild()

public void setLeftChild(______)

public ______ isLeaf()

Page 11: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

BinaryTree

Doc

Happy

BinaryTree

root

BinaryTreeNode

data

leftChild

rightChild

BinaryTreeNode

data

leftChild

rightChild

Page 12: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

BinaryTree

Doc

Happy

BinaryTree

root

BinaryTreeNode

data

leftChild

rightChild

BinaryTreeNode

data

leftChild

rightChild

Complete the sketch with all seven dwarves!

Page 13: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Example Tree: Seven Dwarves

Grumpy

Happy

Doc Sleepy

SneezyBashful

Page 14: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

BinaryTree Methods

public BinaryTreeNode<T> getRoot()

public void setRoot(BinaryTreeNode<T> node)

public ______ isEmpty()

BinaryTree

root

Page 15: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

BinaryTree Methods

public BinaryTreeNode<T> getRoot()

public void setRoot(BinaryTreeNode<T> node)

public ______ isEmpty()

public LinkedList<T> inorderTraversal()

...

BinaryTree

root

Page 16: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Tree Traversals

Page 17: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Tree Traversal

For a BinaryTree instance, visit all of the nodes

Three ways:Pre-order: visit node, left subtree, right subtree.In-order: visit left subtree, node, right subtree.Post-order: visit left subtree, right subtree, node.

Page 18: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Traversals: pre-order, in-order, post-order

Grumpy

Happy

Doc Sleepy

SneezyBashful

Page 19: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Traversals: pre-order, in-order, post-order

Grumpy

Happy

Doc Sleepy

SneezyBashful

In-order: Bashful, Doc, Grumpy, Happy, Sleepy, SneezyPre-order: Happy, Doc, Bashful, Grumpy, Sleepy, SneezyPost-order: Bashful, Grumpy, Doc, Sneezy, Sleepy, Happy

Page 20: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Write pseudocode for traversing trees and printing nodes in-order public void printInOrderTraversal(BinaryTreeNode<T> node) {

}

Page 21: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Write pseudocode for traversing trees and printing nodes in-order

public void printInOrderTraversal(BinaryTreeNode<T> node) {

// Base case: null

if (node == null)

return;

// Base case: leaf

else if ( node.isLeaf() )

S.o.p.( node.getData() );

// recursive case

else {

printInOrderTraversal( node.getLeftChild() );

S.o.p.( node.getData() );

printInOrderTraversal( node.getRightChild() );

}

}

Page 22: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Binary Search Trees(preview next week)

http://wiki.cs.mtholyoke.edu/mediawiki/cs201/index.php/Binary_search_trees

Page 23: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Binary Search Tree (BST)

BST data structure● extension of of BinaryTree● invariant:

○ nodes in LEFT subtree are less than root○ nodes in RIGHT subtree are greater than root

Page 24: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Binary Search Tree search

15

45

50

8 75

88

Page 25: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Binary Search Tree maxElement

15

45

50

8 75

88

Page 26: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Binary Search Tree minElement

15

45

50

8 75

88

Page 27: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Binary Search Tree insert

15

45

50

8 75

88

Page 28: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Insertion

True or False: nodes that are inserted will always become leaves in the tree

15

45

50

8 75

88

Page 29: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Insertion

True or False: nodes that are inserted will always become leaves in the tree

15

45

50

8 75

88

Page 30: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Inserting a node

Sketch out pseudocode

Page 31: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

What is the post-order traversal?

15

45

50

8 75

88

Page 32: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Binary Search Tree delete

15

45

50

8 75

886

10

12

Page 33: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Binary Search Tree delete

15

45

50

8 75

886

10

12

leaf

Page 34: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Binary Search Tree delete

15

45

50

8 75

886

10

12has 1 child

Page 35: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Binary Search Tree delete

15

45

50

8 75

886

10

12

has 2 children (a)

Replace with next greatest element

Page 36: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Binary Search Tree delete

15

45

50

8 75

886

10

12

has 2 children (b)

Replace with next greatest element

Page 37: Mount Holyoke College | - Binary Trees · 2015. 11. 24. · Tree traversals: pre-order, in-order, post-order. Binary Tree Data Structure. ... Chapter 9 of the Michael Main Data Structures

Binary Search Tree delete

15

45

50

8 75

886

10

12

has 2 children (c)

Replace with next greatest element