Title: Nell Dale
1C Plus Data Structures
Nell Dale Chapter 8 Binary Search
Trees Modified from the slides by Sylvia
Sorkin, Community College of Baltimore County -
Essex Campus
2Binary search
- for an element in a sorted list stored
sequentially - in an array O(Log2N)
- in a linked list ? (midpoint ?)
3Goals of this chapter
- Introduce some basic tree vocabulary
- Develop algorithms
- Implement operations needed to use a binary
search tree
4Jakes Pizza Shop
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
5 Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
6 Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
7A Tree Has Levels
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
LEVEL 0
Level of a node its distance from the root
8 Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
9Level Two
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
LEVEL 2
10A binary tree with N nodes
- Maximum number of levels N
- Minimum number of levels logN 1
- e.g., N8, 16, , 1000
11The height of a tree
- the maximum level in a tree
-- a critical factor in determining how
efficiently we can search for elements
12A Subtree
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
LEFT SUBTREE OF ROOT NODE
13Another Subtree
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
RIGHT SUBTREE OF ROOT NODE
14Binary Tree
- 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.
15A Binary Tree
V
Q
L
T
A
E
K
S
16A Binary Tree ?
V
Q
L
T
A
E
K
S
17How many leaf nodes?
V
Q
L
T
A
E
K
S
18How many descendants of Q?
V
Q
L
T
A
E
K
S
19How many ancestors of K?
20Implementing a Binary Tree with Pointers and
Dynamic Data
V
Q
L
T
A
E
K
S
21Each node contains two pointers
templatelt class ItemType gt struct TreeNode
ItemType info // Data member
TreeNodeltItemTypegt left // Pointer to
left child TreeNodeltItemTypegt right //
Pointer to right child
NULL A 6000
. left . info . right
22// BINARY SEARCH TREE SPECIFICATION templatelt
class ItemType gt 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 TreeNodeltItemTypegt
root
22
23TreeTypeltchargt CharBST
Private data root
RetrieveItem
PrintTree . . .
24A Binary Tree
V
Q
L
T
A
E
K
S
Search for S?
25A Binary Search Tree (BST) is . . .
- 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.
26Shape of a binary search tree . . .
- 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.
27Inserting E into the BST
- 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.
28Inserting F into the BST
- 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.
29Inserting T into the BST
- 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.
30Inserting A into the BST
- 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.
31What binary search tree . . .
- is obtained by inserting
- the elements A E F J T in
that order?
32Binary search tree . . .
- obtained by inserting
- the elements A E F J T in
that order.
A degenerate tree!
33Another binary search tree
T
E
A
H
M
P
K
Add nodes containing these values in this
order D B L Q S
V Z
34Is F in the binary search tree?
J
T
E
A
V
M
H
P
35// BINARY SEARCH TREE SPECIFICATION templatelt
class ItemType gt 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
TreeNodeltItemTypegt root
35
36// SPECIFICATION (continued) // - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - //
RECURSIVE PARTNERS OF MEMBER FUNCTIONS
templatelt class ItemType gt void PrintHelper
( TreeNodeltItemTypegt ptr, ofstream
outFile ) templatelt class ItemType
gt void InsertHelper ( TreeNodeltItemTypegt
ptr, ItemType item )
templatelt class ItemType gt void
RetrieveHelper ( TreeNodeltItemTypegt ptr,
ItemType item, bool found )
templatelt class ItemType gt void
DestroyHelper ( TreeNodeltItemTypegt ptr )
36
37// BINARY SEARCH TREE IMPLEMENTATION // OF
MEMBER FUNCTIONS AND THEIR HELPER FUNCTIONS //
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - templatelt class ItemType gt
TreeTypeltItemTypegt TreeType ( ) //
constructor root NULL // - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - -
- templatelt class ItemType gt bool
TreeTypeltItemTypegt IsEmpty( ) const
return ( root NULL )
37
38 templatelt class ItemType gt void
TreeTypeltItemTypegt RetrieveItem ( ItemType
item, bool found
) RetrieveHelper ( root, item, found )
templatelt class ItemType gt void
RetrieveHelper ( TreeNodeltItemTypegt ptr,
ItemType item, bool
found) if ( ptr NULL ) found
false else if ( item lt ptr-gtinfo
) // GO LEFT RetrieveHelper( ptr-gtleft ,
item, found ) else if ( item gt
ptr-gtinfo ) // GO RIGHT RetrieveHelper(
ptr-gtright , item, found ) else
// item ptr-gtinfo found true
38
39 templatelt class ItemType, class KF gt void
TreeTypeltItemType, KFgt RetrieveItem (
ItemType item, KF key, bool found
) // Searches for an element with the same key as
items key. // If found, store it into item
RetrieveHelper ( root, item, key, found )
templatelt class ItemType gt void
RetrieveHelper ( TreeNodeltItemType,KFgt ptr,
ItemType item, KF key, bool
found) if ( ptr NULL ) found
false else if ( key lt ptr-gtinfo.key()
) // GO LEFT RetrieveHelper( ptr-gtleft , item,
key, found ) else if ( key gt
ptr-gtinfo.key() ) // GO RIGHT RetrieveHelper(
ptr-gtright , item, key, found ) else
item ptr-gtinfo found true
39
40 templatelt class ItemType gt void
TreeTypeltItemTypegt InsertItem ( ItemType item
) InsertHelper ( root, item )
templatelt class ItemType gt void
InsertHelper ( TreeNodeltItemTypegt ptr,
ItemType item ) if ( ptr NULL )
// INSERT item HERE AS LEAF ptr
new TreeNodeltItemTypegt ptr-gtright NULL
ptr-gtleft NULL ptr-gtinfo item
else if ( item lt ptr-gtinfo ) // GO
LEFT InsertHelper( ptr-gtleft , item )
else if ( item gt ptr-gtinfo ) // GO
RIGHT InsertHelper( ptr-gtright , item )
40
41Delete()
- Find the node in the tree
- Delete the node from the tree
- Three cases
- 1. Deleting a leaf
- 2. Deleting a node with only one child
- 3. Deleting a node with two children
- Note the tree must remain a binary tree and
the search property must remain intact
42 templatelt class ItemType gt void
TreeTypeltItemTypegt DeleteItem ( ItemType
item ) DeleteHelper ( root, item )
templatelt class ItemType gt void
DeleteHelper ( TreeNodeltItemTypegt ptr, ItemType
item) if ( item lt ptr-gtinfo ) //
GO LEFT DeleteHelper( ptr-gtleft , item )
else if ( item gt ptr-gtinfo ) // GO
RIGHT DeleteHelper( ptr-gtright , item )
else DeleteNode(ptr) // Node is
found call DeleteNode
42
43 templatelt class ItemType gt void DeleteNode (
TreeNodeltItemTypegt tree ) ItemType data
TreeNodelt ItemTypegt tempPtr tempPtr tree
if ( tree-gtleft NULL ) tree
tree-gtright delete tempPtr else if
(tree-gtright NULL ) tree
tree-gtleft delete tempPtr else
// have two children GetPredecessor(tree-
gtleft, item) tree-gtinfo item
DeleteHelper(tree-gtleft, item) // Delete
predecessor node (rec)
43
44 templatelt class ItemType gt void
GetPredecessor( TreeNodeltItemTypegt tree,
ItemType data ) // Sets data to the info member
of the rightmost node in tree while
(tree-gtright ! NULL) tree
tree-gtright data tree-gtinfo
44
45PrintTree()
- Traverse a list
- -- forward
- -- backward
- Traverse a tree
- -- there are many ways!
46Inorder Traversal A E H J M T Y
Print second
tree
T
E
A
H
M
Y
Print left subtree first
Print right subtree last
47 // INORDER TRAVERSAL templatelt class
ItemType gt void TreeTypeltItemTypegt PrintTree
( ofstream outFile ) const PrintHelper (
root, outFile ) templatelt class
ItemType gt void PrintHelper (
TreeNodeltItemTypegt ptr, ofstream outFile )
if ( ptr ! NULL )
PrintHelper( ptr-gtleft , outFile ) // Print
left subtree outFile ltlt ptr-gtinfo
PrintHelper( ptr-gtright, outFile ) //
Print right subtree
47
48Preorder Traversal J E A H T M Y
Print first
tree
T
E
A
H
M
Y
Print left subtree second
Print right subtree last
49Postorder Traversal A H E M Y T J
Print last
tree
T
E
A
H
M
Y
Print left subtree first
Print right subtree second
50 templatelt class ItemType gt TreeTypeltItemTypegt
TreeType ( ) // DESTRUCTOR
DestroyHelper ( root ) templatelt class
ItemType gt void DestroyHelper (
TreeNodeltItemTypegt ptr ) // Post All nodes of
the tree pointed to by ptr are deallocated.
if ( ptr ! NULL )
DestroyHelper ( ptr-gtleft ) DestroyHelper (
ptr-gtright ) delete ptr
50
51Iterative Insertion and Deletion
52Recursion or Iteration?
Assume the tree is well balanced.
- 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.
53Use 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
54Binary Search Trees (BSTs) vs. Linear Lists
- 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
55End