1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community...
-
Upload
eugene-davidson -
Category
Documents
-
view
226 -
download
4
Transcript of 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community...
![Page 1: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/1.jpg)
1
Nell Dale
Chapter 8
Binary Search Trees
Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus
C++ Plus Data Structures
![Page 2: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/2.jpg)
2
for an element in a sorted list stored sequentially
in an array: O(Log2N) in a linked list: ? (midpoint = ?)
Binary search
![Page 3: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/3.jpg)
3
Introduce some basic tree vocabulary
Develop algorithms
Implement operations needed to use a binary search tree
Goals of this chapter
![Page 4: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/4.jpg)
4
Jake’s Pizza Shop
Owner Jake
Manager Chef Brad Carol
Waitress Waiter Cook Helper Joyce Chris Max Len
![Page 5: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/5.jpg)
5
Owner Jake
Manager Chef Brad Carol
Waitress Waiter Cook Helper Joyce Chris Max Len
A Tree Has a Root Node
ROOT NODE
![Page 6: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/6.jpg)
6
Owner Jake
Manager Chef Brad Carol
Waitress Waiter Cook Helper Joyce Chris Max Len
Leaf nodes have no children
LEAF NODES
![Page 7: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/7.jpg)
7
Owner Jake
Manager Chef Brad Carol
Waitress Waiter Cook Helper Joyce Chris Max Len
A Tree Has Levels
LEVEL 0
Level of a node: its distance from the root
![Page 8: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/8.jpg)
8
Owner Jake
Manager Chef Brad Carol
Waitress Waiter Cook Helper Joyce Chris Max Len
Level One
LEVEL 1
![Page 9: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/9.jpg)
9
Owner Jake
Manager Chef Brad Carol
Waitress Waiter Cook Helper Joyce Chris Max Len
Level Two
LEVEL 2
![Page 10: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/10.jpg)
10
Maximum number of levels: N
Minimum number of levels: logN + 1
e.g., N=8, 16, …, 1000
A binary tree with N nodes:
![Page 11: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/11.jpg)
11
the maximum level in a tree
The height of a tree:
-- a critical factor in determining how efficiently we can search for elements
![Page 12: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/12.jpg)
12
Owner Jake
Manager Chef Brad Carol
Waitress Waiter Cook Helper Joyce Chris Max Len
A Subtree
LEFT SUBTREE OF ROOT NODE
![Page 13: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/13.jpg)
13
Owner Jake
Manager Chef Brad Carol
Waitress Waiter Cook Helper Joyce Chris Max Len
Another Subtree
RIGHT SUBTREE OF ROOT NODE
![Page 14: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/14.jpg)
14
A binary tree is a structure in which:
Each node can have at most two children, and in which a unique path exists from the root to every other node.
The two children of a node are called the left child and the right child, if they exist.
Binary Tree
![Page 15: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/15.jpg)
15
A Binary Tree
Q
V
T
K S
A E
L
![Page 16: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/16.jpg)
16
A Binary Tree ?
Q
V
T
K S
A E
L
![Page 17: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/17.jpg)
17
How many leaf nodes?
Q
V
T
K S
A E
L
![Page 18: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/18.jpg)
18
How many descendants of Q?
Q
V
T
K S
A E
L
![Page 19: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/19.jpg)
19
How many ancestors of K?
Q
V
T
K S
A E
L
![Page 20: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/20.jpg)
20
Implementing a Binary Tree with Pointers and Dynamic Data
Q
V
T
K S
A E
L
![Page 21: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/21.jpg)
21
Each node contains two pointers
template< class ItemType >struct TreeNode {
ItemType info; // Data member
TreeNode<ItemType>* left; // Pointer to left child
TreeNode<ItemType>* right; // Pointer to right child
};
. left . info . right
NULL ‘A’ 6000
![Page 22: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/22.jpg)
// BINARY SEARCH TREE SPECIFICATION
template< class ItemType >
class TreeType { public:
TreeType ( ); // constructor ~TreeType ( ); // destructor bool IsEmpty ( ) const; bool IsFull ( ) const; int NumberOfNodes ( ) const; void InsertItem ( ItemType item ); void DeleteItem (ItemType item ); void RetrieveItem ( ItemType& item, bool& found ); void PrintTree (ofstream& outFile) const;
. . .
private:
TreeNode<ItemType>* root;}; 22
![Page 23: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/23.jpg)
23
TreeType<char> CharBST;
‘J’
‘E’
‘A’
‘S’
‘H’
TreeType
~TreeType
IsEmpty
InsertItem
Private data:
root
RetrieveItem
PrintTree . . .
![Page 24: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/24.jpg)
24
A Binary Tree
Q
V
T
K S
A E
L
Search for ‘S’?
![Page 25: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/25.jpg)
25
A special kind of binary tree in which:
1. Each node contains a distinct data value,
2. The key values in the tree can be compared using “greater than” and “less than”, and
3. The key value of each node in the tree is
less than every key value in its right subtree, and greater than every key value in its left subtree.
A Binary Search Tree (BST) is . . .
![Page 26: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/26.jpg)
26
Depends on its key values and their order of insertion.
Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ in that order.
The first value to be inserted is put into the root node.
Shape of a binary search tree . . .
‘J’
![Page 27: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/27.jpg)
27
Thereafter, each value to be inserted begins by comparing itself to the value in the root node, moving left it is less, or moving right if it is greater. This continues at each level until it can be inserted as a new leaf.
Inserting ‘E’ into the BST
‘J’
‘E’
![Page 28: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/28.jpg)
28
Begin by comparing ‘F’ to the value in the root node, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a leaf.
Inserting ‘F’ into the BST
‘J’
‘E’
‘F’
![Page 29: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/29.jpg)
29
Begin by comparing ‘T’ to the value in the root node, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a leaf.
Inserting ‘T’ into the BST
‘J’
‘E’
‘F’
‘T’
![Page 30: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/30.jpg)
30
Begin by comparing ‘A’ to the value in the root node, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a leaf.
Inserting ‘A’ into the BST
‘J’
‘E’
‘F’
‘T’
‘A’
![Page 31: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/31.jpg)
31
is obtained by inserting
the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order?
What binary search tree . . .
‘A’
![Page 32: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/32.jpg)
32
obtained by inserting
the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order.
Binary search tree . . .
‘A’
‘E’
‘F’
‘J’
‘T’A “degenerate” tree!
![Page 33: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/33.jpg)
33
Another binary search tree
Add nodes containing these values in this order:
‘D’ ‘B’ ‘L’ ‘Q’ ‘S’ ‘V’ ‘Z’
‘J’
‘E’
‘A’ ‘H’
‘T’
‘M’
‘K’ ‘P’
![Page 34: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/34.jpg)
34
Is ‘F’ in the binary search tree?
‘J’
‘E’
‘A’ ‘H’
‘T’
‘M’
‘K’
‘V’
‘P’ ‘Z’‘D’
‘Q’‘L’‘B’
‘S’
![Page 35: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/35.jpg)
// BINARY SEARCH TREE SPECIFICATION
template< class ItemType >
class TreeType { public:
TreeType ( ) ; // constructor ~TreeType ( ) ; // destructor bool IsEmpty ( ) const ; bool IsFull ( ) const ; int NumberOfNodes ( ) const ; void InsertItem ( ItemType item ) ; void DeleteItem (ItemType item ) ; void RetrieveItem ( ItemType& item , bool& found ) ; void PrintTree (ofstream& outFile) const ;
. . .
private:
TreeNode<ItemType>* root ;}; 35
![Page 36: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/36.jpg)
// SPECIFICATION (continued) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// RECURSIVE PARTNERS OF MEMBER FUNCTIONS
template< class ItemType >void PrintHelper ( TreeNode<ItemType>* ptr,
ofstream& outFile ) ;
template< class ItemType > void InsertHelper ( TreeNode<ItemType>*& ptr,
ItemType item ) ;
template< class ItemType > void RetrieveHelper ( TreeNode<ItemType>* ptr,
ItemType& item, bool& found ) ;
template< class ItemType > void DestroyHelper ( TreeNode<ItemType>* ptr ) ;
36
![Page 37: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/37.jpg)
// BINARY SEARCH TREE IMPLEMENTATION // OF MEMBER FUNCTIONS AND THEIR HELPER FUNCTIONS
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - template< class ItemType > TreeType<ItemType> :: TreeType ( ) // constructor { root = NULL ;}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template< class ItemType >bool TreeType<ItemType> :: IsEmpty( ) const{ return ( root == NULL ) ;}
37
![Page 38: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/38.jpg)
template< class ItemType > void TreeType<ItemType> :: RetrieveItem ( ItemType& item,
bool& found ){ RetrieveHelper ( root, item, found ) ; }
template< class ItemType > void RetrieveHelper ( TreeNode<ItemType>* ptr, ItemType& item,
bool& found){ if ( ptr == NULL )
found = false ; else if ( item < ptr->info ) // GO LEFT
RetrieveHelper( ptr->left , item, found ) ; else if ( item > ptr->info ) // GO RIGHT
RetrieveHelper( ptr->right , item, found ) ; else { // item = ptr->info ;
found = true ; }}
38
![Page 39: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/39.jpg)
template< class ItemType, class KF > void TreeType<ItemType, KF> :: RetrieveItem ( ItemType& item,
KF key, bool& found )// Searches for an element with the same key as item’s key. // If found, store it into item{ RetrieveHelper ( root, item, key, found ) ; }template< class ItemType > void RetrieveHelper ( TreeNode<ItemType,KF>* ptr, ItemType& item,
KF key, bool& found){ if ( ptr == NULL )
found = false ; else if ( key < ptr->info.key() ) // GO LEFT
RetrieveHelper( ptr->left , item, key, found ) ; else if ( key > ptr->info.key() ) // GO RIGHT
RetrieveHelper( ptr->right , item, key, found ) ; else { item = ptr->info ; found = true ; }}
39
![Page 40: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/40.jpg)
template< class ItemType > void TreeType<ItemType> :: InsertItem ( ItemType item ){
InsertHelper ( root, item ) ; }
template< class ItemType > void InsertHelper ( TreeNode<ItemType>*& ptr, ItemType item ){ if ( ptr == NULL ) { // INSERT item HERE AS LEAF
ptr = new TreeNode<ItemType> ;ptr->right = NULL ;ptr->left = NULL ;ptr->info = item ;
} else if ( item < ptr->info ) // GO LEFT
InsertHelper( ptr->left , item ) ; else if ( item > ptr->info ) // GO RIGHT
InsertHelper( ptr->right , item ) ;} 40
![Page 41: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/41.jpg)
41
Find the node in the tree Delete the node from the tree Three cases:
1. Deleting a leaf2. Deleting a node with only one child3. Deleting a node with two children
Note: the tree must remain a binary tree and the search property must remain intact
Delete()
![Page 42: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/42.jpg)
template< class ItemType > void TreeType<ItemType> :: DeleteItem ( ItemType& item ){ DeleteHelper ( root, item ) ; }
template< class ItemType > void DeleteHelper ( TreeNode<ItemType>* ptr, ItemType& item){ if ( item < ptr->info ) // GO LEFT
DeleteHelper( ptr->left , item ) ; else if ( item > ptr->info ) // GO RIGHT
DeleteHelper( ptr->right , item ) ; else {
DeleteNode(ptr); // Node is found: call DeleteNode }}
42
![Page 43: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/43.jpg)
template< class ItemType > void DeleteNode ( TreeNode<ItemType>*& tree ){ ItemType data; TreeNode< ItemType>* tempPtr;
tempPtr = tree; if ( tree->left == NULL ) { tree = tree->right; delete tempPtr; } else if (tree->right == NULL ) { tree = tree->left; delete tempPtr; } else { // have two children GetPredecessor(tree->left, item); tree->info = item; DeleteHelper(tree->left, item); // Delete predecessor node (rec) }}
43
![Page 44: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/44.jpg)
template< class ItemType > void GetPredecessor( TreeNode<ItemType>* tree, ItemType& data )// Sets data to the info member of the rightmost node in tree{ while (tree->right != NULL) tree = tree->right;
data = tree->info;}
44
![Page 45: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/45.jpg)
45
Traverse a list:
-- forward
-- backward
Traverse a tree:
-- there are many ways!
PrintTree()
![Page 46: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/46.jpg)
46
Inorder Traversal: A E H J M T Y
‘J’
‘E’
‘A’ ‘H’
‘T’
‘M’ ‘Y’
tree
Print left subtree first Print right subtree last
Print second
![Page 47: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/47.jpg)
// INORDER TRAVERSALtemplate< class ItemType >void TreeType<ItemType> :: PrintTree ( ofstream& outFile ) const{
PrintHelper ( root, outFile ) ; }
template< class ItemType > void PrintHelper ( TreeNode<ItemType>* ptr, ofstream& outFile ){ if ( ptr != NULL ) {
PrintHelper( ptr->left , outFile ) ; // Print left subtree
outFile << ptr->info ;
PrintHelper( ptr->right, outFile ) ; // Print right subtree
}} 47
![Page 48: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/48.jpg)
48
Preorder Traversal: J E A H T M Y
‘J’
‘E’
‘A’ ‘H’
‘T’
‘M’ ‘Y’
tree
Print left subtree second Print right subtree last
Print first
![Page 49: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/49.jpg)
49
‘J’
‘E’
‘A’ ‘H’
‘T’
‘M’ ‘Y’
tree
Print left subtree first Print right subtree second
Print last
Postorder Traversal: A H E M Y T J
![Page 50: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/50.jpg)
template< class ItemType > TreeType<ItemType> :: ~TreeType ( ) // DESTRUCTOR{ DestroyHelper ( root ) ; }
template< class ItemType > void DestroyHelper ( TreeNode<ItemType>* ptr )// Post: All nodes of the tree pointed to by ptr are deallocated.
{ if ( ptr != NULL ) {
DestroyHelper ( ptr->left ) ;DestroyHelper ( ptr->right ) ;delete ptr ;
}}
50
![Page 51: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/51.jpg)
51
Read Text …
Iterative Insertion and Deletion
![Page 52: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/52.jpg)
52
Is the depth of recursion relatively shallow?
Yes. Is the recursive solution shorter or clearer than the
nonrecursive version?
Yes. Is the recursive version much less efficient than the
nonrecursive version?
No.
Recursion or Iteration?Assume: the tree is well balanced.
![Page 53: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/53.jpg)
53
Use a recursive solution when (Chpt. 7):
The depth of recursive calls is relatively “shallow” compared to the size of the problem.
The recursive version does about the same amount of work as the nonrecursive version.
The recursive version is shorter and simpler than the nonrecursive solution.
SHALLOW DEPTH EFFICIENCY CLARITY
![Page 54: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/54.jpg)
54
BST:
Quick random-access with the flexibility of a linked structure
Can be implemented elegantly and concisely using recursion
Takes up more memory space than a singly linked list
Algorithms are more complicated
Binary Search Trees (BSTs) vs. Linear Lists
![Page 55: 1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bfd61a28abf838cae0c0/html5/thumbnails/55.jpg)
55
End