Data Structures with C Using STL - PowerPoint PPT Presentation

1 / 69
About This Presentation
Title:

Data Structures with C Using STL

Description:

... Binary Search Tree (BST) is . . . 9/14/09 ... Inserting E' into the BST. J' E' 9/14 ... Inserting A' into the BST. J' E' F' T' A' 9/14/09. Chapter 10. 42. is ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 70
Provided by: ruthu
Category:
Tags: stl | bst | data | structures | using

less

Transcript and Presenter's Notes

Title: Data Structures with C Using STL


1
Data Structures with CUsing STL
  • Chapter Ten
  • Trees

2
Als Pizza Shop
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna
Ethan Frank
Gail
3
A Tree Has a Root Node
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna
Ethan Frank
Gail
ROOT NODE
4
A Parent-Child Relationship
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna
Ethan Frank
Gail
Parent
Children
5
Descendents of the Root
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna
Ethan Frank
Gail
6
Ancestors of Ethan
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna
Ethan Frank
Gail
7
Leaf nodes have no children
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna
Ethan Frank
Gail
LEAF NODES
8
A Tree Has Levels
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna
Ethan Frank
Gail
LEVEL 0
9
Level One
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna
Ethan Frank
Gail
LEVEL 1
10
Level Two
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna
Ethan Frank
Gail
LEVEL 2
11
A Subtree
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna Ethan
Frank Gail
LEFT SUBTREE OF ROOT NODE
12
Another Subtree
RIGHT SUBTREE OF ROOT NODE
Owner Al Manager
Chef Bob
Chris Waitress
Waiter Cook
Helper Donna Ethan
Frank Gail
13
Depth 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
14
What is the Depth of this tree?
L
F
Q
K
R
D
G
S
15
How many descendants of Q?

V
Q
L
T
A
E
K
S
16
How many ancestors of K?

V
Q
L
T
A
E
K
S
17
Binary 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.

18
Complete 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
19
Full 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
20
Degenerate Tree

V
Binary tree where each node has at most one child
Q
T
K
21
Inorder 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
22
Preorder 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
23
Postorder 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
24
Breadth 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'
25
What 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
26
What 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
27
What 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
28
What 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
29
Implementing a Binary Tree with Pointers and
Dynamic Data

V
L
Q
T
A
E
K
S
30
Dynamic Allocation with Trees
  • Node for a binary tree
  • left data right

31
TreeNode 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)

32
A 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)

33
A 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)

34
A 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
  • . . .

35
Implementing 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)

36
A 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.

37
Shape 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
38
Inserting 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
39
Inserting 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
40
Inserting 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
41
Inserting 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
42
What binary search tree . . .
  • is obtained by inserting
  • the elements A E F J T in
    that order?

A
43
Another 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
44
Resulting binary search tree

J
T
E
A
V
M
H
Z
D
P
K
L
B
Q
S
45
How 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.

46
Is 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 )
52
Deleting a node from BST
  • 3 Cases
  • Deleting a leaf node
  • Deleting a node with only one child
  • Deleting a node with two children

53
Deleting a node with no children DeleteItem
(S)
J
T
E
A
V
M
H
Z
D
P
K
L
B
Q
S
54
Deleting a node with only one child DeleteItem
(A)
J
T
E
A
V
M
H
Z
D
P
K
L
B
Q
S
55
Deleting a node with two children DeleteItem
(T)
J
T
E
A
V
M
H
Z
D
P
K
L
B
Q
S
56
DeleteItem (T)Step 1 Locate inorder
predecessor

J
T
E
A
V
M
H
Z
D
P
K
L
B
Q
S
57
DeleteItem (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
58
DeleteItem (T)Step 3 Delete predecessor

J
S
E
A
V
M
H
Z
D
P
K
L
B
Q
S
Free Node
59
DeleteItem (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()
63
A 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.

64
A Two-Level Binary Expression
treePtr
-
8
5
INORDER TRAVERSAL 8 - 5 PREORDER
TRAVERSAL - 8 5 POSTORDER TRAVERSAL 8
5 -
65
Levels 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.

66
A Binary Expression Tree


3

2
4
What value does this tree evaluate to?
67
A Binary Expression Tree


3

2
4
What infix, prefix, postfix expressions does it
represent?
68
Evaluate this binary expression tree


-
/
3
5

8
2
4
What infix, prefix, postfix expressions does it
represent?
69
InfoNode 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
70
Each 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
Write a Comment
User Comments (0)
About PowerShow.com