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

Generate a single sorted list of all n elements, Q. Assume that we have sep...

Q. Assume that we have separated n elements in to m sorted lists. Explain how to generate a single sorted list of all n elements in time O (n log m )?

Define game trees, Game trees An interesting application of trees is th...

Game trees An interesting application of trees is the playing of games such as tie-tac-toe, chess, nim, kalam, chess, go etc. We can picture the sequence of possible moves by m

State about the simple types - built-in types, State about the Simple types...

State about the Simple types - Built-In Types Values of the carrier set are atomic, that is, they can't be divided into parts. Common illustrations of simple types are inte

Rules for abstract data type-tree, null(nil) = true                     // ...

null(nil) = true                     // nil refer for empty tree null(fork(e, T, T'))= false   //  e : element , T and T are two sub tree leaf(fork(e, nil, nil)) = true leaf(

Column major representation, Column Major Representation In memory th...

Column Major Representation In memory the second method of representing two-dimensional array is the column major representation. Under this illustration, the first column of

Which is the most suitable data type, Problem 1. You are asked to store...

Problem 1. You are asked to store Names of all 100 students of class A in your Learning Centre. Which data type will you use? What is its syntax? Explaining the data typ

Define abstract data type & column major ordering for arrays, Q1. Define th...

Q1. Define the following terms: (i) Abstract data type. (ii) Column major ordering for arrays. (iii)  Row major ordering for arrays. Q2. Explain the following: (i) A

Implementation of dequeue, Dequeue (a double ended queue) is an abstract da...

Dequeue (a double ended queue) is an abstract data type alike to queue, where insertion and deletion of elements are allowed at both of the ends. Like a linear queue & a circular q

Logic circuits, the voltage wave forms are applied at the inputs of an EX-O...

the voltage wave forms are applied at the inputs of an EX-OR gate. determine the output wave form

Relationship between shortest path distances of modified, a) Given a digrap...

a) Given a digraph G = (V,E), prove that if we add a constant k to the length of every arc coming out from the root node r, the shortest path tree remains the same. Do this by usin

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