Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

BST

Posted on 2007-11-17
12
Medium Priority
?
401 Views
Last Modified: 2010-04-01
How do i construct BST based on the following:
12, 11, 8, 16, 5, 7, 13, 9, 2

I have been thinking about this for quite some time. Inorder traversal is not suitable as it returns a list of ascending numbers. I have also tried preorder and postorder traversal....but it doesn't seem to work. Pls help.

I just need a diagram to illustrate the BST.
0
Comment
Question by:dandeliondream
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20306857
Have a look to this tutorial:
http://en.wikipedia.org/wiki/Binary_search_tree
0
 
LVL 3

Author Comment

by:dandeliondream
ID: 20306900
i think it shld be a preorder since postorder and inorder starts with 2. right? i have seen the tutorial.. :( not very useful pls help
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 200 total points
ID: 20306905
Depending on what you mean by "based" it could be
                   12  
           11             16
       8                13
   5      9
2   7
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 200 total points
ID: 20306928
I have not clear your question.
Traversal is done AFTER tree is created. Tree internal shape will depend how you have inserted the nodes into it.
In-order traversal will always give you an ordered result.
pre-order and post-order will give you "unordered" result but exact sequence will depend on internal shape.
0
 
LVL 3

Author Comment

by:dandeliondream
ID: 20306948
12, 11, 8, 16, 5, 7, 13, 9, 2 is the sequence from left to right after a traversal is done. It may be preorder, postorder or inorder traversal. Based on the sequence, how can i construct a BST?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20307196
You want to store the values 12, 11, 8, 16, 5, 7, 13, 9, 2 in a BST in that exact order ? Why use a BST then ? Why not just an array or a vector ?

There's no point. A BST's strength is that it makes searching of an ORDERED series a lot faster. You'd have to add a second index value that acts as key into the BST, and store the value as data. Sounds a bit overkill to me. Just take an array or vector.
0
 
LVL 3

Author Comment

by:dandeliondream
ID: 20307319
no that's not what i meant. I want a diagram like what mr. ozo has given me earlier. Construct a BST based on the list of numbers The numners are to be processed from left to right.
12, 11, 8, 16, 5, 7, 13, 9, 2

Not a program. Just a diagram.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20307322
Yes, but there's two orders :

1) the order you put the values into the BST
2) the order you get them out of the BST

Which are you referring to when you want to get this order : 12, 11, 8, 16, 5, 7, 13, 9, 2 ?
0
 
LVL 3

Author Comment

by:dandeliondream
ID: 20307390
the second one...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20307393
>> the second one...

Then I repeat : there's no point in using a BST - just use a simple array or vector.
0
 
LVL 3

Author Comment

by:dandeliondream
ID: 20307432
ok, thanks. what will the result be if it is the first one?
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 600 total points
ID: 20307438
>> ok, thanks. what will the result be if it is the first one?

A BST that looks like what ozo showed. You can of course balance the tree out, but you will not be able to get the values out the same order you put them in there (unless by chance).
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question