Title: Data Structures with C Using STL
1Data Structures with CUsing STL
2Als Pizza Shop
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna
Ethan Frank
Gail
3A Tree Has a Root Node
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna
Ethan Frank
Gail
ROOT NODE
4A Parent-Child Relationship
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna
Ethan Frank
Gail
Parent
Children
5Descendents of the Root
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna
Ethan Frank
Gail
6Ancestors of Ethan
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna
Ethan Frank
Gail
7Leaf nodes have no children
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna
Ethan Frank
Gail
LEAF NODES
8A Tree Has Levels
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna
Ethan Frank
Gail
LEVEL 0
9Level One
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna
Ethan Frank
Gail
LEVEL 1
10Level Two
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna
Ethan Frank
Gail
LEVEL 2
11A Subtree
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna Ethan
Frank Gail
LEFT SUBTREE OF ROOT NODE
12Another Subtree
RIGHT SUBTREE OF ROOT NODE
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna Ethan
Frank Gail
13Depth of the Tree is 5
J
1
T
E
2
A
V
M
H
3
Z
D
P
K
4
L
B
Q
5
S
14What is the Depth of this tree?
L
F
Q
K
R
D
G
S
15How many descendants of Q?
V
Q
L
T
A
E
K
S
16How many ancestors of K?
V
Q
L
T
A
E
K
S
17Binary 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.
18Complete Binary Tree
L
H
R
M
T
K
D
Leaf nodes at lowest two levels and all leaf
nodes at last level are to the left.
E
B
J
19Full Binary Tree
All leaf nodes are only at lowest level.
M
R
H
P
T
J
D
W
E
B
J
K
N
Q
S
20Degenerate Tree
V
Binary tree where each node has at most one child
Q
T
K
21Inorder Traversal A E H J M T Y
Print second
tree
J
T
E
A
H
M
Y
Print left subtree first
Print right subtree last
22Preorder Traversal J E A H T M Y
Print first
tree
J
T
E
A
H
M
Y
Print left subtree second
Print right subtree last
23Postorder Traversal A H E M Y T J
Print last
tree
'J'
'E'
'T'
'H'
'A'
'M'
'Y'
Print left subtree first
Print right subtree second
24Breadth First Scan J E T A H M Y
Print level 0 first from left to right Then level
1 from left to right Then level 2 from left to
right, ...
tree
'J'
'T'
'E'
'H'
'A'
'M'
'Y'
25What is the PreOrder Scan for this tree?
J E A E B T M K L P Q S V Z
J
T
E
A
V
M
H
Z
D
P
K
L
B
Q
S
26What is the InOrder Scan for this tree?
A B D E H J K L M P Q S V Z
J
T
E
A
V
M
H
Z
D
P
K
L
B
Q
S
27What is the BreadthFirst Scan for this tree?
J E T A H M V D K P Z B L Q S
J
T
E
A
V
M
H
Z
D
P
K
L
B
Q
S
28What is the PostOrder Scan for this tree?
B D A H E L K S Q P M Z V T J
J
T
E
A
V
M
H
Z
D
P
K
L
B
Q
S
29Implementing a Binary Tree with Pointers and
Dynamic Data
V
L
Q
T
A
E
K
S
30Dynamic Allocation with Trees
- Node for a binary tree
- left data right
31TreeNode class
- template ltclass Tgt
- class TreeNode
- private TreeNodeltTgt left
- TreeNodeltTgt right
- T data
- public // constructor
- TreeNode(const T item, TreeNodeltTgt
lptrNULL, - TreeNodeltTgt
rptrNULL) - // access methods
- TreeNodeltTgt Left(void)const
- TreeNodeltTgt Right(void)const
- T Data(void)const
- void SetLeft(TreeNodeltTgt lptr)
- void SetRight(TreeNodeltTgt rptr)
-
32A Binary Tree Class
- include TreeNode.h
- template ltclass Tgt
- class BinTree
- private // constructor
- TreeNodeltTgt root
- // allocates a treenode setting its
fields and returns its address - TreeNodeltTgt GetTreeNode(const T item,
- TreeNodeltTgt lptr NULL,
- TreeNodeltTgt rptr NULL)
- // destroys a treenode
- void FreeTreeNode(TreeNodeltTgt ptr)
-
33A Binary Tree Class continued
- // private recursive traversal functions
- void PreOrder(TreeNodeltTgt tptr, void
visit(const Titem))const - void PostOrder(TreeNodeltTgt tptr, void
visit(const T item))const - void InOrder(TreeNodeltTgt tptr, void
visit(const T item))const - void BreadthFirst(TreeNodeltTgt tptr), void
visit(const T item))const - public // default constructor - creates empty
tree - BinTree(void)
- // copy constructor
- BinTree(const BinTreeltTgt BT)
- // overloaded assignment operator
- BinTreeltTgt operator(const BinTreeltTgt
BT) - // destructor
- BinTree(void)
34A Binary Tree class continued
- void PreOrder(void visit(const Titem))const
- void PostOrder(void visit(const T
item))const - void InOrder(void visit(const T item))const
- void BreadthFirst(void visit(const T
item))const - . . .
-
35Implementing PreOrder Traversal
- template classltTgt
- void TreeNodeltTgtPreOrder(TreeNodeltTgt tptr,
- void visit(const Titem))const
- if (tptr ! NULL)
- visit(tptr-gtdata)
- PreOrder(tptr-gtLeft(),visit)
- PreOrder(tptr-gtRight()visit)
-
-
- template classltTgt
- void TreeNodeltTgtPreOrder(void visit(const
Titem))const - PreOrder(root,visit)
36A 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.
37Shape of a binary search tree . . .
- Depends on its key values and their order of
insertion. - Let's build the binary search tree of elements
J E F T A in that order. - The first value to be inserted is put into the
root node.
J
38Inserting 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.
J
E
39Inserting 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.
J
E
F
40Inserting 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.
J
T
E
F
41Inserting 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.
J
T
E
F
A
42What binary search tree . . .
- is obtained by inserting
- the elements A E F J T in
that order?
A
43Another binary search tree
J
T
E
A
H
M
P
K
Add nodes containing these values in this
order D B L Q S
V Z
44Resulting binary search tree
J
T
E
A
V
M
H
Z
D
P
K
L
B
Q
S
45How do we handle duplicate keys?
- If we use the lt operator with comparison
insertValue lt TreeNode.data, to search for the
appropriate leaf. - Duplicates will be placed on the right subtree
- An inorder scan will print the duplicate values
in the order they were added.
46Is F in the binary search tree?
J
T
E
A
V
M
H
Z
D
P
K
L
B
Q
S
47// BINARY SEARCH TREE SPECIFICATION templatelt
class T gt class BSTree public
BSTree ( ) // constructor
BSTree ( ) // destructor bool
IsEmpty ( ) const bool IsFull ( )
const int NumberOfNodes ( ) const
void InsertItem ( T item ) void
DeleteItem (T item ) void
RetrieveItem ( T item, bool found ) void
PrintTree (ofstream outFile) const .
. . private TreeNodeltTgt root
48// SPECIFICATION (continued) // - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - //
RECURSIVE PARTNERS OF MEMBER FUNCTIONS
templatelt class T gt void PrintHelper (
TreeNodeltTgt ptr, ofstream outFile )
templatelt class T gt void InsertHelper (
TreeNodeltTgt ptr, T item )
templatelt class T gt void RetrieveHelper (
TreeNodeltTgt ptr, T item, bool found )
templatelt class T gt void DestroyHelper (
TreeNodeltTgt ptr )
49// BINARY SEARCH TREE IMPLEMENTATION // OF
MEMBER FUNCTIONS AND THEIR HELPER
FUNCTIONS templatelt class T gt TreeTypeltTgt
TreeType ( ) // constructor root
NULL templatelt class T gt bool
TreeTypeltTgt IsEmpty( ) const return
( root NULL )
50 templatelt class T gt void TreeTypeltTgt
RetrieveItem (T item, bool found )
RetrieveHelper ( root, item, found )
templatelt class Tgt void RetrieveHelper (
TreeNodeltTgt ptr, T item, bool found)
if ( ptr NULL ) found false
else if ( item lt ptr-gtData()) // GO
LEFT RetrieveHelper( ptr-gtLeft() , item, found
) else if ( item gt ptr-gtData()) //
GO RIGHT RetrieveHelper( ptr-gtRight() , item,
found ) else item
ptr-gtData() found true
51 templatelt class T gt void TreeTypeltTgt
InsertItem (const T item ) InsertHelper (
root, item ) templatelt class T gt void
InsertHelper ( TreeNodeltTgt ptr, const T
item ) if ( ptr NULL ) ptr
GetTreeNode(item) // INSERT item HERE AS LEAF
else if ( item lt ptr-gtData()) // GO
LEFT InsertHelper( ptr-gtLeft() , item )
else if ( item gt ptr-gtData()) // GO
RIGHT InsertHelper( ptr-gtRight , item )
52Deleting a node from BST
- 3 Cases
- Deleting a leaf node
- Deleting a node with only one child
- Deleting a node with two children
53Deleting a node with no children DeleteItem
(S)
J
T
E
A
V
M
H
Z
D
P
K
L
B
Q
S
54Deleting a node with only one child DeleteItem
(A)
J
T
E
A
V
M
H
Z
D
P
K
L
B
Q
S
55Deleting a node with two children DeleteItem
(T)
J
T
E
A
V
M
H
Z
D
P
K
L
B
Q
S
56DeleteItem (T)Step 1 Locate inorder
predecessor
J
T
E
A
V
M
H
Z
D
P
K
L
B
Q
S
57DeleteItem (T)Step 2 Copy info from
predecessor into node.
J
T 'S'
E
A
V
M
H
Z
D
P
K
L
B
Q
S
58DeleteItem (T)Step 3 Delete predecessor
J
S
E
A
V
M
H
Z
D
P
K
L
B
Q
S
Free Node
59DeleteItem (T)Step 3 Delete predecessor
J
S
E
A
V
M
H
Z
D
P
K
L
B
Q
60 templatelt class T gt void TreeTypeltTgt
DeleteItem ( const T item )
DeleteHelper ( root, item ) templatelt
class T gt void DeleteHelper ( TreeNodeltTgt
ptr, T item ) // Deletes item from tree. //
Post item is not n tree. if ( item lt
ptr-gtData() ) DeleteHelper(ptr-gtLeft(),
item) // LOOK IN LEFT SUBTREE else if
( item gt ptr-gtData() )
DeleteHelper(ptr-gtRight(), item) // LOOK
IN RIGHT SUBTREE else
DeleteNode(ptr) // NODE FOUND DELETE
IT
61 templatelt class T gt void DeleteNode(TreeNodelt
Tgt tree) // Deletes the node pointed to by
tree. T info TreeNodeltTgt tempPtr
tempPtr tree if (tree-gtLeft() NULL)
// one or no children, delete node
tree tree-gtRight() delete
tempPtr else if (tree-gtRight()
NULL) // only left child tree
tree-gtLeft() delete tempPtr
else // two children
GetPredecessor(tree, info)
tree-gtData() info
DeleteHelper(tree-gtLeft(), data)
62 templatelt class T gt void GetPredecessor
(TreeNodeltTgt tree, T info) // Sets data to
the info member of the right-most node of the
left sub-tree. tree tree-gtLeft()
while(tree-gtRight() ! NULL) tree
tree-gtRight() info tree-gtData()
63A Binary Expression Tree is . . .
- A special kind of binary tree in which
- 1. Each leaf node contains a single operand,
- 2. Each nonleaf node contains a single binary
operator, and - 3. The left and right subtrees of an operator
node represent subexpressions that must be
evaluated before applying the operator at the
root of the subtree.
64A Two-Level Binary Expression
treePtr
-
8
5
INORDER TRAVERSAL 8 - 5 PREORDER
TRAVERSAL - 8 5 POSTORDER TRAVERSAL 8
5 -
65Levels Indicate Precedence
- When a binary expression tree is used to
represent an expression, the levels of the nodes
in the tree indicate their relative precedence of
evaluation. - Operations at higher levels of the tree are
evaluated later than those below them. The
operation at the root is always the last
operation performed.
66A Binary Expression Tree
3
2
4
What value does this tree evaluate to?
67A Binary Expression Tree
3
2
4
What infix, prefix, postfix expressions does it
represent?
68Evaluate this binary expression tree
-
/
3
5
8
2
4
What infix, prefix, postfix expressions does it
represent?
69InfoNode has 2 forms
enum OpType OPERATOR, OPERAND struct
InfoNode OpType whichType
union // ANONYMOUS union char
operation int operand
OPERATOR
OPERAND 7
. whichType . operation
. whichType . operand
70Each node contains two pointers
struct TreeNode InfoNode info
// Data member TreeNode left //
Pointer to left child TreeNode right
// Pointer to right child
NULL 6000
OPERAND 7
. whichType . operand
. left . info
. right