Binary Search Trees: Assigned Problems #2
1)
A complete binary
tree is a special case of a binary tree, in which all the levels, except
perhaps the last, are full; while on the last level, any missing nodes are to
the right of all the nodes that are present. Write a function to output if an
input binary search tree is complete or not.
2)
The cost of a path in a tree is sum of the values of the nodes
participating in that path. Write a method that returns the cost of the most
expensive path from the root to a leaf node.
3)
Build a binary search tree, T. Your program has to output the
length of the longest path in T. For an example, click here. In this example,
the longest paths are darkened for Tree_1 (length = 8) and Tree_2 (length = 9).
As you can see, the longest path need not include the root.
4)
Write a program that outputs all structurally distinct binary search
trees of N (a positive integer > 0) nodes. Two trees are considered
structurally distinct if they have different number of nodes or if their left
and/or right sub trees are of different shapes. The values in the nodes are of
no concern. Your program will take an integer input, N (> 0). Use the OutputTree
method to display the
trees.
Permutation Generator:
· Algorithm : Generating permutations
in lexicographic order
5)
(Source: Java Textbook (Deitel & Deitel, #17.22, Pages, 842 - 843)
In this exercise, we discuss
deleting items from binary search trees. The deletion algorithm is not as straightforward
as the insertion algorithm. Three cases are encountered when deleting an
item—the item is contained in a leaf node (i.e., it has no children), the item
is contained in a node that has one child or the item is contained in a node
that has two children:
1)
If the item to be deleted is
contained in a leaf node, the node is deleted and the reference in the parent
node is set to null.
2)
If the item to be deleted is
contained in a node with one child, the reference in the parent node is set to
reference the child node and the node containing the data item is deleted. This
causes the child node to take the place of the deleted node in the tree.
3)
The last case is the most
difficult. When a node with two children is deleted, another node in the tree
must take its place. However, the reference in the parent node cannot simply be
assigned to reference one of the children of the node to be deleted. In most
cases, the resulting binary search tree would not embody the following
characteristic of binary search trees (with no duplicate values):The values in
any left subtree are less than the value in the parent node, and the values in
any right subtree are greater than the value in the parent node.
Which node is
used as a replacement node to maintain this characteristic? It is either the
node containing the largest value in the tree less than the value in the node
being deleted, or the node containing the smallest value in the tree greater
than the value in the node being deleted. Let us consider the former scenario:
in a binary search tree, the largest value less than a parent’s value is
located in the left subtree of the parent node and is guaranteed to be
contained in the rightmost node of the subtree. This node is located by walking
down the left subtree to the right until the reference to the right child of
the current node is null. We are now referencing the replacement node, which is
either a leaf node or a node with one child to its left. If the replacement
node is a leaf node, the steps to perform the deletion are as follows:
a) Store the
reference to the node to be deleted in a temporary reference variable.
b) Set the reference
in the parent of the node being deleted to reference the replacement node.
c) Set the
reference in the parent of the replacement node to null.
d) Set the
reference to the right subtree in the replacement node to reference the right
subtree of the node to be deleted.
e) Set the
reference to the left subtree in the replacement node to reference the left
subtree of the node to be deleted.
The deletion
steps for a replacement node with a left child are similar to those for a
replacement node with no children, but the algorithm also must move the child
into the replacement node’s position in the tree. If the replacement node is a
node with a left child, the steps to perform the deletion are as follows:
a) Store the
reference to the node to be deleted in a temporary reference variable.
b) Set the
reference in the parent of the node being deleted to reference the replacement
node.
c) Set the
reference in the parent of the replacement node to reference the left child of
the replacement node.
d) Set the
reference to the right subtree in the replacement node to reference the right
subtree of the node to be deleted.
e) Set the
reference to the left subtree in the replacement node to reference the left
subtree of the node to be deleted.
Write method
deleteNode, which takes as its argument the value to delete. Method deleteNode
should locate in the tree the node containing the value to delete and use the
algorithms discussed here to delete the node. If the value is not found in the
tree, the method should print a message that indicates whether the value is
deleted.