mount holyoke college | - binary trees · 2015. 11. 24. · tree traversals: pre-order, in-order,...

Post on 02-Jan-2021

5 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Binary Trees

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

OutlineMerge Sort discussion

Binary trees

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

Binary Tree Data Structure

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).

Binary Tree Data Structure

Happy

Doc

Bashful Grumpy

Sleepy

Sneezy

Binary Tree:Sketch of Nodes and Pointers

BinaryTreeNode

leftChild rightChild

data

BinaryTreeNode

leftChild rightChild

data

BinaryTreeNode

data

leftChild

rightChild

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()

BinaryTree

Doc

Happy

BinaryTree

root

BinaryTreeNode

data

leftChild

rightChild

BinaryTreeNode

data

leftChild

rightChild

BinaryTree

Doc

Happy

BinaryTree

root

BinaryTreeNode

data

leftChild

rightChild

BinaryTreeNode

data

leftChild

rightChild

Complete the sketch with all seven dwarves!

Example Tree: Seven Dwarves

Grumpy

Happy

Doc Sleepy

SneezyBashful

BinaryTree Methods

public BinaryTreeNode<T> getRoot()

public void setRoot(BinaryTreeNode<T> node)

public ______ isEmpty()

BinaryTree

root

BinaryTree Methods

public BinaryTreeNode<T> getRoot()

public void setRoot(BinaryTreeNode<T> node)

public ______ isEmpty()

public LinkedList<T> inorderTraversal()

...

BinaryTree

root

Tree Traversals

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.

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

Grumpy

Happy

Doc Sleepy

SneezyBashful

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

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

}

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() );

}

}

Binary Search Trees(preview next week)

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

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

Binary Search Tree search

15

45

50

8 75

88

Binary Search Tree maxElement

15

45

50

8 75

88

Binary Search Tree minElement

15

45

50

8 75

88

Binary Search Tree insert

15

45

50

8 75

88

Insertion

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

15

45

50

8 75

88

Insertion

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

15

45

50

8 75

88

Inserting a node

Sketch out pseudocode

What is the post-order traversal?

15

45

50

8 75

88

Binary Search Tree delete

15

45

50

8 75

886

10

12

Binary Search Tree delete

15

45

50

8 75

886

10

12

leaf

Binary Search Tree delete

15

45

50

8 75

886

10

12has 1 child

Binary Search Tree delete

15

45

50

8 75

886

10

12

has 2 children (a)

Replace with next greatest element

Binary Search Tree delete

15

45

50

8 75

886

10

12

has 2 children (b)

Replace with next greatest element

Binary Search Tree delete

15

45

50

8 75

886

10

12

has 2 children (c)

Replace with next greatest element

top related