I saw an example of a binary tree and I am wondering if it's sorted or not.
Attachment 13654
I am dangled with the all nodes thing the link says.
Printable View
I saw an example of a binary tree and I am wondering if it's sorted or not.
Attachment 13654
I am dangled with the all nodes thing the link says.
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.
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?
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
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! :)
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