473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Insert items into a Binary Search Tree in ascending order

Today in class, my professor posed an interesting question for us to think about before our next class. If you insert items that are in ascending order into a binary search tree, then you will build the worst possible tree since it will have the largest possible height. The question is:

How would you change the tree building process so that the tree is nearly balanced ?

Just something to think about, if anyone has any ideas that would be great!! Thanks in advance!!!
Apr 16 '08 #1
7 3367
Ganon11
3,652 Expert 2GB
What if you tried to keep the height of the subtrees of each node equal?
Apr 16 '08 #2
What if you tried to keep the height of the subtrees of each node equal?
I'm not real sure what you are getting at.....wouldn't the height of each subtree already be 1, since every parent node would have 1 child node on the right side?
Apr 16 '08 #3
Ganon11
3,652 Expert 2GB
What is the height of a tree? Isn't it the number of levels (i.e. a perfectly balanced binary tree with 7 nodes has height 3 (level 1 has the root, level 2 has its 2 children, and level 3 has those children's 4 children))? So then, the height of a subtree is 1 + max(height(leftChild), height(rightChild)).

If the two heights were equal or close to equal, that means that subtree is balanced.
Apr 16 '08 #4
yes i understand what you are saying, and that was the question that my professor posed to us.

How would you change the tree building process so that the tree is nearly balnaced?

The one thought that I am having is rotating the nodes after they are inserted, more like an AVL tree would do.

Does any one know of any pseudo-code to implement a rotation? After thinking about it more I think rotation would be the best option, but im not sure what the best way to perform the rotation would be.
Apr 16 '08 #5
Ganon11
3,652 Expert 2GB
In the following diagrams, X3 is the node where the tree is unbalanced, and X1 and X2 are other nodes we will be moving around. A, B, C, and D are all subtrees whose structure doesn't change.

Expand|Select|Wrap|Line Numbers
  1.              (Some tree...)
  2.             /
  3.            X1
  4.           /  \
  5.         X2    A
  6.        /  \
  7.      X3    B
  8.     /  \
  9.    D    C
would become...

Expand|Select|Wrap|Line Numbers
  1.              (Some tree...)
  2.             /
  3.           X2
  4.          /  \
  5.        X3     X1
  6.       /  \   /  \
  7.      D    C B    A
in one type of rotation.

There are 3 other types - try writing down diagrams and figuring out where each node/subtree would go.
Apr 16 '08 #6
Yes I know how to make all of the necessary rotations on paper like you suggested. But lets say we wanted to implent it. How would we do that?

Would this work?

Expand|Select|Wrap|Line Numbers
  1. Insert item
  2. Check height to see if the tree is balanced
  3. If it is unbalanced, call function to perform necessary swaps
The only thing that I am unclear about would be how to know which type of rotation is to be used? How would you check to see if it is a right rotation, left rotation, or one of the other 2 types?
Apr 16 '08 #7
Ganon11
3,652 Expert 2GB
You would have to check the specific values of the heights where the tree was unbalanced.

Let's say that, in the above example, height(X3) = 6, height (B) = 4, height(X2) = 7, height(X1) = 8. So the tree is unbalanced at X2 (because height(X2->left) is significantly different than height(X2->right)). So you go to the parent of X2, namely X1, and check if X2 is a left child of X1 or a right child. A single rotation occurs when you have a bigger subtree in the left child of a left child, or the right child of a right child. In this case, X2 is the left child of X1, so you want to check if X3 is a left child of X2 - wait, we already determined this (from our comparison of the height() of each subtree).

If the heights had been reversed (height(X3) = 4, height(B) = 6), you would have needed to perform a double rotation, which you should know how to do.

The actual rotations are done with some simple manipulation of pointers - say, making X1 point to X3 instead of X2 or something.
Apr 16 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
2
by: maltchev | last post by:
i need to insert data from an xml file into sql server table. the xml file contains only one record. how to insert the data? how to map the names of the fields in the xml file and the table?...
15
by: Foodbank | last post by:
Hi all, I'm trying to do a binary search and collect some stats from a text file in order to compare the processing times of this program (binary searching) versus an old program using linked...
10
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
4
by: mathon | last post by:
Hello, im currently implementing a binary search tree means, that a greater number than root will be added as right child and a less number as left child. My insert function looks currently like...
6
by: fdmfdmfdm | last post by:
This might not be the best place to post this topic, but I assume most of the experts in C shall know this. This is an interview question. My answer is: hash table gives you O(1) searching but...
18
by: Hunk | last post by:
Would like some advice on the fillowing I have a sorted list of items on which i require to search and retrieve the said item and also modify an item based on its identity. I think an Map stl...
8
by: n.noumia | last post by:
- with 10 nodes, give one example of an unbalanced binary tree and one example of a balanced binary tree - what is the advantage of having a balanced binary tree over an unbalanced tree? - number...
11
by: Defected | last post by:
Hi, How i can create a Binary Search Tree with a class ? thanks
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.