This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes When do i say that a binary search tree is unique?! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When do i say that a binary search tree is unique?!" Watch "When do i say that a binary search tree is unique?!" New topic
Author

When do i say that a binary search tree is unique?!

Nadine Ernest
Greenhorn

Joined: Dec 10, 2008
Posts: 27
Hi,
given the following:

90,50,20,5,25,75,66,80,150,95,92,111,175,166,200.

i constructed the binary tree and the traversal was preorder traversal : root, left, right


and then Is the tree unique?!

yes , the tree is unique,

i dont understand why?!


What Doesn't Kill You, Makes You Stronger!
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Nadine Ernest wrote:Hi,
given the following:

90,50,20,5,25,75,66,80,150,95,92,111,175,166,200.

i constructed the binary tree and the traversal was preorder traversal : root, left, right


and then Is the tree unique?!

yes , the tree is unique,

i dont understand why?!


Because it's a binary search tree. With a regular binary tree, the order of the elements doesn't matter, so the pre-order traversal of [3,2,4] could be:



But with a binary search tree, you start with 3, and the insert 2 to the left (because it's less than 3) and insert 4 into the right (because it's more than 3), resulting in:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44590
    
  34
Please explain what you mean by the tree being unique.
Nadine Ernest
Greenhorn

Joined: Dec 10, 2008
Posts: 27
Hi campbell ritchie, i actually asked the question to a friend of mine!

she told me that the tree is unique meaning that with the preorder and inorder traversal the tree has JUST ONE SHAPE!!

not many!

thanks guys for your help!!

so actually yeah when you draw this tree it is unique!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When do i say that a binary search tree is unique?!
 
It's not a secret anymore!