Sheet 8
Sheet 8
▪ Binary search tree is a special case of binary tree but with more constraints on adding and removing a
node.
The code in “BinarySearchTree” is very much the same as that in “BinaryLinkedTree” with few
exceptions:
• The traverse order is changed to in-order.
• addRoot, expandExternal and removeAboveExternal functions are now protected, instead of
public.
• Two public functions add and remove are added.
• Two public functions are added to class Position:
bool Position::hasLeftChild();
bool Position::hasRightChild();
You may take into account the assumption (of page 300 in your textbook) that external nodes of a
binary search tree are empty.
1) Ignore the assumption (of page 300 in your textbook) that external nodes of a binary search tree are
empty (they are non-empty).
2) Update void BinarySearchTree::add() and void BinarySearchTree::remove() first to comply with the
previous point.
3) The Euler Tour specified in the textbook assumes a proper binary tree, i.e., each node has either zero or
two children.
It is possible however for an internal node to have a single child in a non-proper binary tree.
In which case we must check first if an internal node has a left/right child before proceeding forward
with the traversal.
1. Implement Euler tour for computing the number of descendants of a node p in a tree.
2. Implement Euler tour for computing the max of descendants of a node p in a tree.
3. Implement Euler tour for computing the mean of descendants of a node p in a tree.
4. Implement Euler tour for computing the height of a node p in a tree.
5. The balance factor of an internal node v of a binary tree is the difference between the heights of the
right and left sub-trees of v. Show how to specialize the Euler tour traversal of Section 7.3.7 to print the
balance factors of all the nodes of a binary tree.
When you have the time, preferably after you have implemented all the functions above.
Ignore the assumption (of page 300 in your textbook) that external nodes of a binary search tree are empty
(they are non-empty).
It is easy to assign (x, y) coordinates for a node p of T using the two rules above.
You can use void draw_node(Elem data, int x, int y) in “simple_graphics.h” to draw a node given its (x, y)
coordinates and data.
If you want to clear the screen, simply include “windows.h” & perform this call system(“cls”);