0% found this document useful (0 votes)
50 views2 pages

Analysis of Algorithms: Solutions 5: Problem 1

1) The document contains 4 problems related to binary search trees. 2) Problem 1 asks to draw 5 binary search trees with heights of 2, 3, 4, 5, and 6 and 7 nodes labeled 1-7. 3) Problem 2 gives a recursive version of a tree insert procedure.

Uploaded by

Grant Lin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views2 pages

Analysis of Algorithms: Solutions 5: Problem 1

1) The document contains 4 problems related to binary search trees. 2) Problem 1 asks to draw 5 binary search trees with heights of 2, 3, 4, 5, and 6 and 7 nodes labeled 1-7. 3) Problem 2 gives a recursive version of a tree insert procedure.

Uploaded by

Grant Lin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PS, PDF, TXT or read online on Scribd
You are on page 1/ 2

Analysis of Algorithms: Solutions 5

X
X X X
X X X
X X X
X X X X
X X X X X
X X X X X X
X X X X X X X X
-----------------------------
1 2 3 4 5 6 7 8 9 10
grades

Problem 1
Draw ve binary search trees, with heights two, three, four, ve, and six. Each tree should
have seven nodes, and the keys of these nodes should be 1, 2, 3, 4, 5, 6, and 7.

4 5 6 7

2 6 4 7 5 7 6

1 3 5 7 1 6 1 1

4 2 2 2

3 6 3 4 5

1 5 7 3 3

2 4

1
Problem 2
Give a recursive version of the Tree-Insert procedure.
Recursive-Insert(T ; z )
if root[T ] = nil
then parent[z] nil
root[T ] z

else
Add-Node(root[T ]; z )
Add-Node(x; z )
if key[ ] key[ ]
z < x

then if left-child[ ] 6= nil


x

then Add-Node(left-child[ ] )
x ;z

else parent[ ] z x

left-child[x] z
else if right-child[x] 6= nil
then Add-Node(right-child[x]; z)
else parent[z] x
right-child[x] z
Problem 3
Give a nonrecursive version of Inorder-Tree-Walk.
Iterative-Tree-Walk(T )
x Tree-Minimum(root[T ])
while x 6= nil
do print key[ x ]
x Tree-Successor(x)
Problem 4
Write a procedure Inorder-Range-Walk(x, min, max) that recursively prints all nodes
between min and max in the subtree rooted at x. It should be more ecient than Inorder-
Tree-Walk; thus, it should not traverse the whole subtree.
Inorder-Range-Walk(x, min, max)
if x 6= nil
then if min  key[ ] x

then Inorder-Range-Walk(left-child[ ], min, max)


x

if min  key[ ]  max x

then print key[ ] x

if key[ ]  max
x

then Inorder-Range-Walk(right-child[ ], min, max) x

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy