Вы находитесь на странице: 1из 5

This material is provided to the professors and students using Data Structures: A Pseudocode Approach Using C++.

All reprints in any form must cite this copyright.


Copyright(c) 2001 by Brooks/Cole
A division of Thomson Learning

Chapter 8 Search Trees


Exercises
1.

See Figure 8.1

3.

See Figure 8.3.

Figure 8.1 Solution to Chapter 8, Exercise 1.

Figure 8.3 Solution to Chapter 8, Exercise 3.


5.

See Figure 8.5.

Figure 8.5 Solution to Chapter 8, Exercise 5.

Page 8.1

7.

See Figure 8.7.

Figure 8.7 Solution to Chapter 8, Exercise 7.


9.
11.
13.

Left tree invalid (20 < 22)


Right tree valid.
25, 20, 18, 14, 19
25, 20, 18, 19, 14
See Figure 8.8.

Figure 8.8 Solution to Chapter 8, Exercise 13.


15.

See Figure 8.10.

Figure 8.10 Solution to Chapter 8, Exercise 15.


17.

See Figure 8.12.

Figure 8.12 Solution to Chapter 8, Exercise 17.

Page 8.2

19.

See Figure 8.14.

Figure 8.14 Solution to Chapter 8, Exercise 19.


Problems
21.

algorithm searchBST (val root


<node pointer>
val target <key type>)
This algorithm searches for a node in a binary search tree.
Pre
root is a pointer to a binary tree.
target is key of the node being searched for
Post
target has been searched for
Return pointer to the target node if found
NULL if not found
1 searchPtr = root
2 loop (searchPtr not null
AND searchPtr->data.key not equal target)
1 if (target < searchPtr->data.key)
1 searchPtr = searchPtr->left
2 else
1 searchPtr = searchPtr->right
3 end if
3 end loop
4 return searchPtr
end searchBST

23.

/* ===================== insertBST ====================


This function inserts a node into a BST without
using recursion.
Pre
root is pointer to root node of a BST
pNew is a pointer to the new node
Post
new node has been inserted into the tree
Return true if successful,
false if data already in tree
*/
bool insertBST (NODE *&root,
NODE *pNew)
{
// Local Definitions
NODE *pWalk;
NODE *parent;
// Statements
if (root == NULL)
root = pNew;
else
{
pWalk = root;
while (pWalk != NULL)
{
parent = pWalk;

Page 8.3

}
25.

if (pNew->data < pWalk->data)


pWalk = pWalk->left;
else if (pNew->data > pWalk->data)
pWalk = pWalk->right;
else
{
// Data already in tree -- return error
cerr << "\n\a**Data already in tree\n";
return false;
} // else
} // while
if (pNew->data < parent->data)
parent->left = pNew;
else
parent->right = pNew;
} // else
return true;
// insertBST

/* =================== deleteBST ======================


This function deletes a node from a BST.
Pre
root is pointer to a binary tree or subtree
Post
node has been deleted and memory freed
Return true if successful,
false if the node wasn't found
*/
bool deleteBST (NODE *&root,
int
dltKey)
{
// Local Definitions
int success;
NODE *dltPtr;
// Statements
if (root == NULL)
success = 0;
else if (dltKey < root->data)
success = deleteBST (root->left, dltKey);
else if (dltKey > root->data)
success = deleteBST (root->right, dltKey);
else
{
if (root->left == NULL)
{
dltPtr = root;
root
= root->right;
delete (dltPtr);
success = 1;
} /* if
else if (root->right == NULL)
{
dltPtr = root;
root
= root->left;
free (dltPtr);
success = 1;
} /* else if
else

Page 8.4

dltPtr = root->left;
while (dltPtr->right != NULL)
dltPtr = dltPtr->right;
root->data = dltPtr->data;
success
= deleteBST (root->left,
dltPtr->data);
} // else
} // else
return success;
// dleteBST

Page 8.5

Вам также может понравиться