Title: CSI 1340 Introduction to Computer Science II
1CSI 1340Introduction to Computer Science II
- Chapter 8
- Binary Search Trees
2Jakes Pizza Shop
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
3A Tree Has a Root Node
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
ROOT NODE
4Leaf nodes have no children
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
LEAF NODES
5A Tree Has Levels
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
LEVEL 0
6Level One
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
LEVEL 1
7Level Two
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
LEVEL 2
8A Subtree
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
LEFT SUBTREE OF ROOT NODE
9Another Subtree
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
RIGHT SUBTREE OF ROOT NODE
10Binary 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.
11A Binary Tree
V
Q
L
T
A
E
K
S
12Not A Binary Tree
V
Q
L
T
A
E
K
S
13How many leaf nodes?
V
Q
L
T
A
E
K
S
14Name the nodes on level 3
V
Q
L
T
A
E
K
S
15How many descendants of Q?
V
Q
L
T
A
E
K
S
16How many ancestors of K?
17Implementing a Binary Tree with Pointers and
Dynamic Data
V
Q
L
T
A
E
K
S
18Each 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
19// 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
20TreeTypeltchargt CharBST
Private data root
RetrieveItem
PrintTree . . .
21A 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.
22A Binary Search Tree
8
3
9
6
12
1
4
13
23Shape 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.
24Inserting 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.
25Inserting 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.
26Inserting 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.
27Inserting 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.
28What binary search tree . . .
- is obtained by inserting
- the elements A E F J T in
that order?
29Binary search tree . . .
- obtained by inserting
- the elements A E F J T in
that order.
30Another 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
31Is F in the binary search tree?
J
T
E
A
V
M
H
P
32// 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 (OrderType order)
const . . . private
TreeNodeltItemTypegt root
33// 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 )
34// 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 )
35 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
36 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 )
37Inorder 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
38 templateltclass ItemTypegt void TreeTypeltItemTypegt
PrintTree(OrderType order) const // Calls
recursive function Print to print items in the
tree. switch (order) case
PREORDER PrintPreorder(root) break case
INORDER PrintInorder(root) break case
POSTORDER PrintPostorder(root) break defau
lt cout ltlt "Bad order" ltlt endl
39 templateltclass ItemTypegt void TreeTypeltItemTypegt
PrintInorder(TreeNodeltItemTypegt p) const
if (p ! NULL) PrintInorder(p-gtleft) //
Print left subtree. cout ltlt p-gtinfo ltlt
endl PrintInorder(p-gtright) // Print right
subtree.
40Preorder 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
41 templateltclass ItemTypegt void TreeTypeltItemTypegt
PrintPreorder(TreeNodeltItemTypegt p) const
if (p ! NULL) cout ltlt p-gtinfo ltlt
endl PrintPreorder(p-gtleft) // Print left
subtree. PrintPreorder(p-gtright) // Print
right subtree.
42Postorder 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
43 templateltclass ItemTypegt void TreeTypeltItemTypegt
PrintPostorder(TreeNodeltItemTypegt p) const
if (p ! NULL) PrintPostorder(p-gtleft) /
/ Print left subtree. PrintPostorder(p-gtright)
// Print right subtree. cout ltlt p-gtinfo ltlt
endl
44 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