Professional Documents
Culture Documents
root
c
NULL NULL
NULL NULL
NULL NULL
cur
cur
root
c
NULL NULL
cur
NULL
cur
NULL NULL
NULL NULL
cur
cur
cur
root
c
NULL NULL
NULL
cur
NULL NULL
NULL NULL
cur
cur
root
c
NULL NULL
cur
NULL NULL
NULL NULL
cur
cur
root
c
NULL NULL
NULL NULL
NULL NULL
cur
cur
cur
root
c
NULL NULL
NULL NULL
NULL NULL
cur
Output:
a bdec
cur
NULL NULL
NULL NULL
root
c
NULL NULL
root
NULL NULL
NULL NULL
cur
////Process
in left sub-tree.
Processnodes
the current
node.
cout
<< cur->value;right);
// Process
thenodes
current
node.
InOrder(cur->
// Process
in left
sub-tree.
}
InOrder(cur-> right); // Process nodes in left sub-tree.
Output:
b
b
NULL NULL
////Process
in left sub-tree.
Processnodes
the current
node.
cout
<< cur->value;right);
// Process
thenodes
current
node.
InOrder(cur->
// Process
in left
sub-tree.
}
InOrder(cur-> right); // Process nodes in left sub-tree.
root
cur
cur
c
NULL NULL
Output:
bac
cur
root
NULL NULL
NULL NULL
PostOrder(cur->
right); // Process the
nodes
in right
sub-tree.
cout << cur->value;
current
node.
}
cout << cur->value;
// Process the current node.
Output:
b
cur
b
NULL NULL
PostOrder(cur->
right); // Process the
nodes
in right
sub-tree.
cout << cur->value;
current
node.
}
cout << cur->value;
// Process the current node.
root
cur
c
NULL NULL
Output:
bc a
In-order
root
#2
#15
#8
#17
#9
d
NULL
#6
NULL
#7
NULL NULL
#11
#12
#15
#6
#4
c
#16
#8
d
NULL
#3
NULL
#5
#18
#10
NULL NULL
#9
#17
NULL NULL
#12
#2
#18
#10
#19
#13
#7
#16
NULL NULL
#13
#4
#5
root
#1
#19
#14
#3
#14
#11
1. Traverse left
2. Process current node
3. Traverse right
Level-order
root
#1
#14
#2
#5
#15
#7
d
NULL
#3
NULL
#4
root
#10
#9
1. Traverse left
2. Traverse right
3. Process current node
#3
NULL NULL
#16
NULL NULL
#8
#2
#17
NULL NULL
#11
#6
#18
#13
#12
#1
#4
d
NULL
NULL
#5
NULL NULL
Big-Oh of Traversals?
Question: Whatre the big-ohs of each of our traversals?
Answer: Well, since a traversal must visit each node
exactly once
and since there are n nodes in a tree
the big-oh for any of the traversals is
O(n)
Traversal Challenge
RULES
The class will split into
left and right teams
One student from each
team will come up to the
board
Each student can either
write one new item or
fix a single error in
their teammates
solution
Then the next two people
come up, etc.
The team that completes
their program first wins!
Larry
Fran
Ronda
NULL
Danny
Jack
NULL NULL
NULL NULL
Tom
NULL
Sam
NULL NULL
Expression Evaluation
We can represent arithmetic expressions using a
binary tree.
ptr
Expression Evaluation
Heres our evaluation function. We start by passing in a
pointer to the root of the tree.
cu
r
(5+6)*(3-1)
Expression Evaluation
Heres our evaluation function. We start by passing in a
pointer to the root of the tree.
cu
r
(5+6)*(3-1)
Expression Evaluation
Heres our evaluation function. We start by passing in a
pointer to the root of the tree.
1. If the current node is a
1. If
the current
node
is a
number,
return
its value.
number, return its value.
2. Recursively evaluate the left
2. Recursively
evaluate
left
Result
= 5the
subtree and
get the
result.
subtree and get the result.
3. Recursively evaluate the right
3. Recursively
evaluate
right
Result
= 6the
subtree and
get the
result.
subtree and get the result.
4. Apply the operator in the
4. Apply
thenode
operator
in left
the and
current
to the
current
node toreturn
the left
right results;
theand
right
results; return the
result.
result.
(5+6)*(3-1)
ptr
cur
cur
5+6 = 11
Expression Evaluation
Heres our evaluation function. We start by passing in a
pointer to the root of the tree.
1. If the current node is a
1. If
the current
node
is a
number,
return
its value.
number, return its value.
2. Recursively evaluate the left
2. Recursively
evaluate
left
Result
= 3the
subtree Result
and
get=the
11 result.
subtree and get the result.
3. Recursively evaluate the right
3. Recursively
evaluate
right
Result
= 1the
subtree and
get the
result.
subtree and get the result.
4. Apply the operator in the
4. Apply
thenode
operator
in left
the and
current
to the
current
node toreturn
the left
right results;
theand
right
results; return the
result.
result.
(5+6)*(3-1)
ptr
cur
r
u
c
3-1 = 2
Expression Evaluation
Heres our evaluation function. We start by passing in a
pointer to the root of the tree.
The result is 22.
1. If the current node is a
number, return its value.
2. Recursively evaluate the left
Result = 11
subtree and get the result.
(5+6)*(3-1)
ptr
cur
11*2=22
3-1
=2
Expression Evaluation
Heres our evaluation function. We start by passing in a
pointer to the root of the tree.
Larry
Fran
Ronda
NULL
Danny
Jack
NULL NULL
NULL NULL
Tom
NULL
Sam
NULL NULL
root
Larry
Fran
Ronda
NULL
Danny
Jack
NULL NULL
NULL NULL
Tom
NULL
Sam
NULL NULL
Larry
NULL
Fran
NULL
Fran
Ronda
NULL NULL
Danny
NULL
Danny
Nick
NULL NULL
NULL NULL
Manny
Alex
Amy
NULL NULL
NULL NULL
Nick
NULL
Maddy
NULL NULL
Searching a BST
Input: A value V to search for
Output: TRUE if found, FALSE otherwise
Start at the root of the tree
Keep going until we hit the NULL pointer
If V is equal to current nodes value, then found!
If V is less than current nodes value, go left
If V is greater than current nodes value, go right
If we hit a NULL pointer, not found.
Gary
Larry??
Gary==
<==
> Fran??
Larry??
Fran??
Fran??
Gary??
Larry
Fran
Ronda
NULL NULL
Barry
Gary
NULL NULL
NULL NULL
Searching a BST
Start at the root of the tree
Keep going until we hit the NULL pointer
If V is equal to current nodes value, then found!
If V is less than current nodes value, go left
If V is greater than current nodes value, go right
If we hit a NULL pointer, not found.
Show how to search for:
1. Khang
2. Dale
3. Sam
Searching a BST
Here are two different BST search algorithms in C++,
one recursive and one iterative:
pRoot
14 ==
13??
< 17??
13??
17??
14??
ptr->
13
pRoot
ptr->
bool Search(int V, Node *ptr)
{
bool
V, Node *ptr)
if Search(int
(ptr == NULL)
{
return(false); // nope
if
(ptr
else
if ==
(V NULL)
== ptr->value)
return(false);
// found!!!
nope
return(true); //
else
ptr->value)
else if
if (V
(V ==
< ptr->value)
return(true);
//
found!!!
true
return(Search(V,ptr->left));
else
else if (V < ptr->value)
return(Search(V,ptr->left));
return(Search(V,ptr->right));
} else
return(Search(V,ptr->right));
}
7
NULL
13
ptr->
17
true
3
NULL NULL
true
14
19
NULL NULL
NULL NULL
void main(void)
{
bool bFnd;
bFnd = Search(14,pRoot);
}
pRoot
true
ptr->
13
true
17
7
bool Search(int V, Node *ptr)
{
if (ptr == NULL)
return(false); // nope
else if (V == ptr->value)
return(true); // found!!!
else if (V < ptr->value)
return(Search(V,ptr->left));
else
true
return(Search(V,ptr->right));
}
NULL
true
3
NULL NULL
14
19
NULL NULL
NULL NULL
void main(void)
{
bool bFnd;
bFnd = Search(14,pRoot);
true
}
50%
eliminated!
50% eliminated!
50%
eliminated!
50%
eliminated!
Right! N steps
Question:
If there are 4 billion nodes in a BST, how
many steps will it take to perform a search?
Just 32!
WOW!
Now thats PIMP!
Carly
(nothing to do...)
if (m_root == NULL)
binary tree, we Tree
use a class.
node
However
lets
add
a
constructor to
{ m_root = struct
new Node(value);
to hold ourreturn;
items.}
If our tree is
our
Node
so
we
can
easily
create
a
class
BinarySearchTree
Node
*cur = m_root;
empty,
new
one!
{struct
for (;;)
Node
Startallocate
traversing
a
And our constructor initializes that
public:
{ {
down
newfrom
nodethe
and
root pointer to NULL
if (value == cur->value) return;
Node(const std::string &myVal)
If our
value
is
root
of
the
tree.
point
the
when
we
create
a
new
tree.
if
(value
<
cur->value)
BinarySearchTree()
{ {
If
already
the
value
the
to
for(;;)
isinthe
(This indicates the tree is empty)
root
pointer
{ valueif= (cur->left
myVal;
!= NULL)
tree,
insert
then
is
less
were
same
as
an a
If
there
is
to
it
then
m_root
=
NULL;
cur
=
cur->left;
Now
lets
see
our
complete
left = right = NULL;
than
done
the
-current
just
infinite
loop.
to our
left,
done!
}}
else
insertion function in C++. nodewere
{
void
insert(const
std::string
value;std::string &value)
return;
{Node }
*left,*right;
}; }
else if (value > cur->value)
}
Our BST class has a single
{
private:
};
if (cur->right != NULL)
member variable the root
cur = cur->right; pointer to the tree.
else
{
Node *m_root;
nodes
return.
value,
Otherwise
advance
to weve
that
then
goand
left.
found
the
node
proper
spot for
continue.
our new value!
Add
ourvalue
valuewe
as
If the
the
child of
wantleft
to insert
is
greater
than the
the current
current
nodes
node.
value, then
traverse/insert
to the right.
if (m_root == NULL)
{ m_root = new Node(value);
m_root NULL
return; }
Larry
if (value == cur->value)
if (value < cur->value)
{
NULL NULL
return;
if (cur->left != NULL)
cur = cur->left;
else
{
if (cur->right != NULL)
cur = cur->right;
else
void main(void)
{
BinarySearchTree bst;
bst.insert(Larry);
...
bst.insert(Phil);
}
if (m_root == NULL)
{ m_root = new Node(value);
m_root
return; }
cur
Phil == Ronda??
Larry??
if (value
cur->value)
Phil <==Larry??
Ronda??
if (cur->right != NULL)
cur = cur->right;
else
Ronda
NULL NULL
Barry
Fran
if (cur->left != NULL)
cur = cur->left;
else
{
return;
Larry
NULL NULL
Phil
NULL NULL
void main(void)
{
BinarySearchTree bst;
bst.insert(Larry);
...
bst.insert(Phil);
}
Groovy Baby!
Barry
return(pRoot->value);
NULL
NULL
Phil
return(pRoot->value);
NULL NULL
if (pRoot->left == NULL)
return(pRoot->value);
if (pRoot->right == NULL)
return(pRoot->value);
return(GetMin(pRoot->left));
return(GetMax(pRoot->right));
}
cur
cur
cur
danny cur
bill
NULL NULL
InOrder(cur->left);
// Process
nodes in left sub-tree.
all the items in the
tree?
cout << cur->value;
root
jane
waa
NULL NULL
frank
NULL NULL
Output:
bill
danny
frank
jane
waa
cur
delete cur;
cur = NULL
void FreeTree(Node *cur)
{ FreeTree(Node *cur)
void
if (cur == NULL)
{ FreeTree(Node
void
*cur)
if (curreturn;
== NULL)
{ FreeTree(Node
void
*cur)
return;
if (cur
== NULL)
{
FreeTree(cur->left);
return;
if (curFreeTree(cur->left);
== NULL)
FreeTree (cur-> right);
return;
FreeTree(cur->left);
FreeTree (cur-> right);
FreeTree(cur->left);
delete(cur->
cur; right);
FreeTree
}
delete
cur; right);
FreeTree
(cur->
}
delete
cur;
}
delete
cur;
}
cur-> Fran
Ronda
NULL NULL
cur-> Barry
NULL NULL
Gabby
NULL NULL
Ronda
NULL NULL
cur-> Barry
NULL NULL
Gabby
NULL NULL
Ronda
NULL NULL
cur-> Gabby
NULL NULL
Ronda
NULL NULL
FreeTree(cur->left);
FreeTree (cur-> right);
FreeTree (cur-> right);
delete cur;
}
delete
cur;
Big-oh Alert!
And so on
So whats the big-Oh of freeing
all the items in the tree?
Its still O(n) since we have
to visit all n items.
Appendix Slides