Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

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.

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M9523094

Have any Question?


Related Questions in Computer Engineering

Question summary using an organization of your

Question: Summary: Using an organization of your choice: Develop a Complete Disaster Recovery Plan to be submitted to the executive board of your company. Please note that this is a formal writing, all references (peer-r ...

Research the web for an example of a startup using a cloud

Research the Web for an example of a startup using a cloud infrastructure. What were the main reasons for choosing a cloud infrastructure? What alternatives did the startup have? Answer should be at least 1 page long dou ...

Question in a sliding window protocol assume that the

Question : In a Sliding Window Protocol, assume that the senders and receivers window sizes are 3 packets each and a total of 6 packets are sent numbered 1-6. Draw time line diagrams corresponding to each of the followin ...

You are given 3 unknown cylinders each massing exactly

You are given 3 unknown cylinders each massing exactly 25.00g. Your choices of metal have the following densities: Zn 7.14 g/mL, Fe 7.87 g/mL and Ni 8.91 g/mL. Each unknown is placed into a graduated cylinder filled with ...

Question sometimes when we visit super markets we may share

Question : Sometimes when we visit super markets, we may share our personal details to get (for example) "Loyalty cards" and other benefits. Do you think sharing such details is a good approach? Give arguments for both c ...

Ellen is an anthropologist who has been working at olduvai

Ellen is an anthropologist who has been working at Olduvai Gorge in Tanzania for the past six months. She has been conducting research on the Internet. She finds a Web site with an article that proposes a revolutionary t ...

Legacy industries inc has a historic pe multiple of 15 a

Legacy Industries Inc. has a historic P/E multiple of 15, a current EPS of $2.50 projected to grow by 4% in the coming year, and a forward looking P/E multiple of 18. With this information please estimate the current pri ...

Consider the market for small business loans in the context

Consider the market for small business loans. In the context of this market. How adverse selection impact lenders. How does adverse selection impact borrowers? In the context of this market provide 2 things that a lender ...

For a small computer design with 8-instructions suppose the

For a small computer design with 8-instructions. Suppose the processor is modified such that it can is 21-bit processor and can address 2^16 Memory locations: 1. What is the word size? 2. How that will affect the size of ...

What are the implications of the shift from medical care to

What are the implications of the shift from medical care to the focus on overall health conditions?

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As