12287 - Linked-list-based binary search trees   

Description

A binary search tree (BST) is a binary tree, whose internal nodes each store a key and each have two sub-trees, commonly denoted left and right. The tree additionally satisfies the property: the key in each node must be greater than all keys stored in the left sub-tree, and smaller than all keys in the right sub-tree.

 

Let’s see how the BST data structure can be realized in C++ using a linked structure. We define three classes and use polymorphism as follows:

 

function.h

main.cpp

 

where

  1. Class BST serves as the abstract base class for realizing polymorphism
  2. List_ BST provide an approach to implementing the BST data structure

 

REQUIREMENTS:

  1. Implement the insert and createLeaf member functions of the List_ BST class.

Note:

1.      This problem involves three files.

  • function.h: Class definitions.
  • function.cpp: Member-function definitions.
  • main.cpp: A driver program to test your class implementation.

You will be provided with main.cpp and function.h, and asked to implement function.cpp.

 

2.      For OJ submission:

       Step 1. Submit only your function.cpp into the submission block.

       Step 2. Check the results and debug your program if necessary.

 

Input

There are three kinds of commands:

  • “I A”: insert a node with int value A(A>0) into the BST. If the key already exists, you simply do nothing.
  • “P”: show the current content of the BST.
  • “H”: print the BST’s height. Remember if the BST is empty, the height is 0 and you should print “0”.

Each command is followed by a new line character.

Input terminated by EOF.

Output

The output shows the result of each command.

Sample Input  Download

Sample Output  Download

Partial Judge Code

12287.cpp

Partial Judge Header

12287.h

Tags




Discuss