This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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

 
Nadine Ernest
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?!
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Marshal
Posts: 62875
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain what you mean by the tree being unique.
 
Nadine Ernest
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!