40
Binary Tree Applications Robb T. Koether Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting from a BST Count-Balancing a BST Assignment Binary Tree Applications Lecture 32 Section 12.4 Robb T. Koether Hampden-Sydney College Wed, Apr 8, 2009

Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Binary Tree ApplicationsLecture 32

Section 12.4

Robb T. Koether

Hampden-Sydney College

Wed, Apr 8, 2009

Page 2: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Outline

1 Expression Trees

2 Binary Search TreesSearching a BSTInserting into a BSTDeleting from a BSTCount-Balancing a BST

3 Assignment

Page 3: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Expression Trees

Definition (Expression Tree)A binary expression tree is a binary tree that is used torepresent an expressions with binary operators. Eachinterior node represents an operator. At each interior node,the left subtree represents the left operand and the rightsubtree represents the right operand.

As a consequence, each terminal node represents anoperand.The order of operation is indicated by the structure ofthe tree.

Page 4: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Expression Trees

For example, (3 + 4) ∗ 5 may be represented as

+

3 4

Page 5: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Expression Trees

If there is more than one operator, the order ofoperation is indicated by the structure of the tree.

*

+ 5

+

3 *

3 4 4 5

Page 6: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Traversals and Expression Trees

Perform an in-order traversal of the expression tree andprint the nodes.

*

+ -

4 5 6 3

Page 7: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Traversals and Expression Trees

Perform a post-order traversal to evaluate theexpression tree.

*

+ -

4 5 6 3

Page 8: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Binary Search Trees

Definition (Binary Search Tree)A binary search tree is a binary tree with the followingproperties.

There is a total order relation on the members in thetree.At every node, every member of the left subtree is lessthan or equal to the node value.At every node, every member of the right subtree isgreater than or equal to the node value.

Page 9: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

A Binary Search Tree

50

30 80

20 40 60 90

10 70

110

130

140

100

120

Page 10: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

BinarySearchTree Implementation

The BinarySearchTree class is implemented as asubclass of the BinaryTree class.

Page 11: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Binary Search Tree Interface

Mutatorsvoid insert(const T& value);Insert a new node containing the value into the binarysearch tree.void remove(const T& value);Remove the node containing the value from the binarysearch tree.

Page 12: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Binary Search Tree Interface

Other Member FunctionsT* search(const T& value) const;Search the binary search tree for the value. Return apointer to the node where the value is found. ReturnNULL if the value is not found.void countBalance();Count-balance the binary search tree.

Page 13: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Searching a BinarySearchTree

Searching a Binary Search Tree

Beginning at the root node, apply the following stepsrecursively.

Compare the value to the node data.If it is equal, you are done.If it is less, search the left subtree.If it is greater, search the right subtree.If the subtree is empty, the value is not in the tree.

Page 14: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Inserting a Value into a BinarySearchTree

Inserting into a Binary Search Tree

Beginning at the root node, apply the following stepsrecursively.

Compare the value to the node data.If it is less (or equal), continue recursively with the leftsubtree.If is is greater, continue recursively with the rightsubtree.When the subtree is empty, attach the node as asubtree.

Page 15: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Deleting a Value from a BinarySearchTree

Perform a search to locate the value.This node will have

Two children, orOne child, orNo child.

Page 16: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

A Binary Search Tree

50

30 80

20 40 60 90

10 70

110

130

140

100

120

Page 17: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

A Binary Search Tree

50

30 80

20 40 60 90

10 70

110

130

140

100

120

Page 18: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Deleting a Value from a BinarySearchTree

Case 1: No ChildDelete the node.

Page 19: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Delete a Node with No Child

50

30 80

20 40 60 90

10 70

110

130

140

100

120

Page 20: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Delete a Node with No Child

50

30 80

20 40 60 90

70

110

130

140

100

120

Page 21: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Deleting a Value from a BinarySearchTree

Case 2: One ChildReplace the node with the subtree of which the child isthe root.

Page 22: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Delete a Node with Two Children

50

30 80

20 40 60 90

10 70

110

130

140

100

120

Page 23: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Delete a Node with Two Children

50

30 80

20 40 60 90

10 70

130

140

100

120

Page 24: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Delete a Node with Two Children

50

30 80

20 40 60 90

10 70

130

140

100

120

Page 25: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Deleting a Value from a BinarySearchTree

Case 3: Two ChildrenLocate the next smaller value in the tree. This value isthe rightmost value of the left subtree.

Move left one step.Move right as far as possible.

Swap this value with the value to be deleted.The node to be deleted now has at most one child.

Page 26: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Delete a Node with Three Children

50

30 80

20 40 60 90

10 70

110

130

140

100

120

Page 27: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Delete a Node with Three Children

50

30 80

20 40 60 90

10 70

110

130

140

100

120

Page 28: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Delete a Node with Three Children

50

30 80

20 40

60

90

10 70

110

130

140

100

120

Page 29: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Delete a Node with Three Children

30 80

20 40

60

90

10 70

110

130

140

100

120

Page 30: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Delete a Node with Three Children

30 80

20 40

60

90

10 70

110

130

140

100

120

Page 31: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Count-Balancing a BinarySearchTree

Write a function moveNodeRight() that will move thelargest value of the left subtree to the right subtree.

The moveNodeRight() FunctionLocate the largest value in the left subtree.Delete it (but save the value).Place it at the root.Insert the old root value into the right subtree.

Page 32: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

A Binary Search Tree

50

30 80

20 40 60 90

10 70

110

130

140

100

120

Page 33: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

A Binary Search Tree

50

30 80

20 40 60 90

10 70

110

130

140

100

120

Page 34: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

A Binary Search Tree

50

30 80

20 40 60

10 70

110

130

140

100

120

Page 35: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

A Binary Search Tree

50

30 80

20 40 60

10 70

110

130

140

100

120

90

Page 36: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Count-Balancing a BinarySearchTree

Count-Balancing a Tree

Write a similar function moveNodeLeft().Apply either moveNodeRight() or moveNodeLeft()repeatedly at the root node until the tree is balanced atthe root.Then apply these functions recursively, down to theleaves.

Page 37: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Building a Binary Search Tree

Suppose we wish to transmit the nodes of a balancedbinary search tree to another computer and reconstructthe tree there.In what order should the values be transmitted?

Page 38: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Building a Binary Search Tree

We could use an in-order traversal to transmit them.At the receiving end, simply call insert() to inserteach value into the tree.The constructed tree will be identical to the original.What do we get if we transmit the values using apre-order traversal?Using a post-order traversal?

Page 39: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

BinarySearchTree Implementation

BinarySearchTree ClassDownload binarysearchtree.h

Download and run BinarySearchTreeTest.cpp

Page 40: Binary Tree Applications - Lecture 32 Section 12people.hsc.edu/faculty-staff/robbk/Coms262/Lectures...Expression Trees Binary Search Trees Searching a BST Inserting into a BST Deleting

Binary TreeApplications

Robb T.Koether

ExpressionTrees

Binary SearchTreesSearching a BST

Inserting into a BST

Deleting from a BST

Count-Balancing aBST

Assignment

Assignment

HomeworkRead Section 12.4, pages 667 - 694.