binary tree applications - lecture 32 section...
TRANSCRIPT
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
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
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.
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
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
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
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
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.
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
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.
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.
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.
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.
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.
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.
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
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
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.
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
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
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.
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
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
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
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.
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
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
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
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
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
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.
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
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
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
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
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.
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?
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?
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
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.