Binary search tree, Data Structure & Algorithms

Assignment Help:

Objectives

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

Background

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.

Description

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.


Related Discussions:- Binary search tree

Data structure for representing numbers, Your first task will be to come up...

Your first task will be to come up with an appropriate data structure for representing numbers of arbitrary potential length in base 215. You will have to deal with large negative

Complexity classes, Complexity classes All decision problems fall in se...

Complexity classes All decision problems fall in sets of comparable complexity, called as complexity classes. The complexity class P is the set of decision problems which ca

Write a program for linear search, In the book the following methods are pr...

In the book the following methods are presented: static void selectionSort(Comparable[] list) static void insertionSort(Comparable[] list) static boolean linearSearch(Comparable

Operation of algorithm, Operation of Algorithm The following sequence o...

Operation of Algorithm The following sequence of diagrams shows the operation of Dijkstra's Algorithm. The bold vertices show the vertex to which shortest path has been find ou

Two-dimensional array, Two-dimensional array is shown in memory in followin...

Two-dimensional array is shown in memory in following two ways:  1.  Row major representation: To achieve this linear representation, the first row of the array is stored in th

Representation of max-heap sequentially, Q. How do we represent a max-heap ...

Q. How do we represent a max-heap sequentially? Explain by taking a valid   example.         Ans: A max heap is also called as a descending heap, of size n is an almos

Train reorganising, A freight train from Melbourne is approaching Sydney, c...

A freight train from Melbourne is approaching Sydney, carrying n cars of cargos. The cargos are to be delivered to n different cities in the metropolitan area of Sydney - one car f

Sorting algorithm is best if the list is already sorted, Which sorting algo...

Which sorting algorithm is best if the list is already sorted? Why? Insertion sort as there is no movement of data if the list is already sorted and complexity is of the order

Explain b tree (binary tree), B Tree Unlike a binary-tree, every node o...

B Tree Unlike a binary-tree, every node of a B-tree may have a variable number of keys and children. The keys are stored in non-decreasing order. Every key has an associated ch

Algorithmss, calculate gpa using an algorithm

calculate gpa using an algorithm

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd