I saw an example of a binary tree and I am wondering if it's sorted or not.
I am dangled with the all nodes thing the link says.
I saw an example of a binary tree and I am wondering if it's sorted or not.
I am dangled with the all nodes thing the link says.
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
It is not a binary search tree. Consider the node with the key 3: every node in its left subtree should have a key less than 3, but 5 > 3.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
So my worries were correct. Thanks.
However, another question just got in my mind. With this structure of the tree, how would we place the values (1 till 7), so that the tree would be ordered?
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
Well, this is an ideal arrangement for a binary search tree with those values:
EDIT:Code:4 2 6 1 3 5 7
If by "this structure of the tree" you mean the alternating right/left children, then I suppose:
except that the binary search tree would have effectively been degraded to be a linked list with extra pointers.Code:1 7 2 6 3 5 4
Last edited by laserlight; 08-29-2014 at 09:18 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yes I agree. My question however is how the tree I posted in the picture would be sorted. I mean let's say that we erase the values and we place them as we want in the picture, in which way should we place the values so that the tree be ordered?
If I am not clear, let me know please.
EDIT:
Your edit is the answer!
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
I don't know why leading spaces on the first line are ignored by code (unless the view is changed to plain text), but to get around that issue, I use a dummy first line:
Code:....... 4 2 6 1 3 5 7