Binary search tree, Data Structure & Algorithms


The purpose of this project is to give you significant exposure to Binary Search Trees (BST), tree traversals, and recursive code.


An arbitrary BST is seldom balanced. The left and right subtrees of a node may have different heights or contain different numbers of nodes, potentially leading to O( N ) performance for operations such as insert, find, and remove. There are several techniques for improving performance and insuring O( lg N ) performance by "balancing" the tree. Some of these will be discussed in class.


In this project, you will explore balancing BST based on the weights of its subtrees. Here we define the weight of a BST to be the number of nodes in that tree. A node in a BST is weight-balanced if the weights of its left and right subtrees differ by no more than 1. A weight-balanced BST is a BST in which every node is weight-balanced. An important property of weight-balanced BST is that the value at any node, X, is a median of the values at all nodes in the subtree rooted at X.

How You're Program Works

Your program is invoked with two command line arguments. The first argument is the name of a file of integers (separated by whitespace) to read and insert into your BST. The second argument is the level to which your BSTs should be printed. Recall that the root is at level zero. For example

unix> ant run -Dargs="integers.dat 4"

Your program performs the following steps

  • Read the integers found in the file specified on the command line and insert them into an initially empty BST, let's call it T, ignoring duplicates.
  • Print the number of integers read from the file (including duplicates).
  • Print the number of nodes in T, the height and median value of T and then print the contents of T in level-order up to the level specified on the command line.
  • Weight-balance T according to the (admittedly inefficient) algorithm below.

    Weight Balance tree T

       find the median of T

       create a new BST, T', with a single node (the root) whose value is the median of T

       retrieve and insert elements of all nodes of T except the median into T'.

       replace T with T'           // T' has a weight-balanced root

       call this procedure to balance the left and right subtrees of T

Print the number of nodes in the weight-balanced tree, the height and median of the weight-balance tree and the contents of the tree in level-order up to the level specified on the command line.

Your Tasks

Design and implement a BST tree class which supports the required operations for this project. You are free to write your own BST from scratch or use some or all of the author's code as a starting point.

Project Requirements, Notes and Hints

    (R) Level-order printing

If the tree's height is less than the specified number of levels to print, then print the entire tree.

Tree nodes must be printed as ordered triples of values in the format ( x, y, z ), where x is the value found in the node's parent (print -1 for the root's parent), y is the value found in the node being printed and z is the weight of the tree rooted at that node.

Your level-order tree print must start with a label on a new line for each level, and print 4 nodes per line if there are more than 4 nodes at a given level.

The format for printing trees is shown in the sample output below.

(N) A level-order traversal requires use of a queue. Elements in the queue should contain appropriate data to print the required information.

(N) You are free to use any classes provided by the Java 6 API.

(N) The median of a set of values is the value "in the middle". If there are an even number of values, then there are two values "in the middle". In this project you should use the smaller of the two as the median.

(N) The algorithm given to weight-balance the tree is not the only possible algorithm, but we ask you to use this one so that your project output matches ours.

(H) Test your code with small files first, using non-random data then move to larger, more complex files.

(H) Some methods are better implemented as recursive functions, others as iterative functions. Choose your implementation carefully.

(H) By convention and for ease of coding, define the height of an empty tree as -1.

(H) Consider adding a new data member to each node which is the weight of the tree rooted at that node. The weight will make it easier to find the median and must be printed with each node. New nodes start with weight = 1. Nodes visited while finding the insertion point for a new node have their weight incremented if the integer being inserted is not a duplicate.

(H) Use the weight in the tree nodes described above to help find the median value. The median may be found with either a recursive or iterative algorithm.

Posted Date: 2/20/2013 2:08:24 AM | Location : United States

Related Discussions:- Binary search tree, Assignment Help, Ask Question on Binary search tree, Get Answer, Expert's Help, Binary search tree Discussions

Write discussion on Binary search tree
Your posts are moderated
Related Questions
Q. Convert the given infix expression into the postfix expression (also Show the steps) A ∗ (B + D)/ E - F(G + H / k ) Ans. Steps showing Infix to Post fix

write an algorithm for multiplication of two sparse matrices using Linked Lists

Define about the class invariant A class invariant may not be true during execution of a public operation though it should be true between executions of public operations. For

Deletion Algorithm for dequeue Step 1: [check for underflow]   If front = 0 and rear = 0   Output "underflow" and return Step 2: [delete element at front end]   If front

Write an algorithm for multiplication of two sparse matrices using Linked Lists.

Write down any four applications of queues.            Application of Queue (i)  Queue is used in time sharing system in which programs with the similar priority form a queu

Midsquare Method :- this operates in 2 steps. In the first step the square of the key value K is taken. In the 2nd step, the hash value is obtained by deleting digits from ends of

write an algorithm and draw a flowchart to calculate the perimeter and area of a circle

Time Complexity:- The time complexity of an algorithm is the amount of time it requires to run to completion. Some of the reasons for studying time complexity are:- We may be in