0% found this document useful (0 votes)
5 views3 pages

Sheet 8

The document outlines the implementation and modifications required for a Binary Search Tree (BST), detailing specific functions to be added or updated, such as add, remove, and various traversal methods. It emphasizes the need to ignore certain assumptions from the textbook regarding external nodes and provides guidelines for implementing an Euler tour for various calculations related to tree nodes. Additionally, it includes instructions for visualizing the tree structure using coordinates and drawing functions.

Uploaded by

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

Sheet 8

The document outlines the implementation and modifications required for a Binary Search Tree (BST), detailing specific functions to be added or updated, such as add, remove, and various traversal methods. It emphasizes the need to ignore certain assumptions from the textbook regarding external nodes and provides guidelines for implementing an Euler tour for various calculations related to tree nodes. Additionally, it includes instructions for visualizing the tree structure using coordinates and drawing functions.

Uploaded by

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

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.

Implement the two comparison operators below:

bool BinarySerachTree::Position::operator== (const Position) const;


bool BinarySerachTree::Position::operator!= (const Position) const;

Complete the added functions in the BinarySearchTree class.

▪ Draw the tree resulting from executing the main code.


1. Implement add & remove.
2. Implement a function that returns the position of the maximum element in the tree.
3. Implement a function that returns the Position of the minimum element in the tree.
4. Write a function called exist that takes a value and returns true if the value is in the tree.
5. Implement a function that takes a Position p and returns the position of the (In-order Predecessor)
largest element less than the value in p stored in the tree.
6. Implement a function that takes a Position p and returns the position of the (In-order Successor)
smallest element larger than the value in p stored in the tree.
7. Implement a function that returns the count of elements stored (number of nodes) in the tree.
8. Implement operator [ ] that takes an int index and returns (not by reference) the Elem of that index.
Consider index 0 for the smallest element and other elements are in ascending order.
9. Update 9 but for negative index. Consider index -1 for the largest element and other elements are in
descending order.
10. Implement a mean function that returns the mean of Elements stored in the tree.
Euler Tour

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).

Update void BinarySearchTree::add() and void BinarySearchTree::remove() first.

Textbook (page 301 – Using Inorder Traversal for Tree Drawing).

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”);

You might also like