Possible Binary Trees and Binary SEARCH tree with 3 nodes A,B,C below .
Is it correct?
Close, it's a good attempt.
However, number 3 is not a valid sorted tree since it has A coming after B (BAC). I think instead you should have chosen the final one on the page
A
\
C
/
B
There's also the mirror of that one:
C
/
A
\
B
In terms of search tree, number two is the one you want, since all the others have a differential height greater than one.
Related
The picture below is the case of different binary trees that can be made with 3 nodes.
But why is the following case not included in the number of cases?
Is it the same case as the third case from the left in the picture above? If so, I think the parent-child relationship will be different.
I'd appreciate it if you could tell me how to determine how binary trees are equal to or different from each other.
You mean 3 nodes. Your case is not included because the cases all use A as the root node, so it is easier to demonstrate the different possible combinations using always the same elements in the same order, i.e. A as root, then B -> C on top and symmetrically C -> B at the bottom.
Using B or C as the root, you could achieve the same number of variations. This number can be computed using the following formula:
In this case, n=3, so F(0)*F(2) + F(1)*F(1) + F(2)*F(0) = 2 + 1 + 2 = 5
F(0) = 1 (empty is considered one variation)
F(1) = 1
F(2) = 2
So in your picture, only one row is actually relevant if that is supposed to be a binary search tree. Also note that there is a difference between a binary tree and a binary search tree. From first article below:
As we know, the BST is an ordered data structure that allows no
duplicate values. However, Binary Tree allows values to be repeated
twice or more. Furthermore, Binary Tree is unordered.
References
https://www.baeldung.com/cs/calculate-number-different-bst
https://en.wikipedia.org/wiki/Binary_tree#Using_graph_theory_concepts
https://encyclopediaofmath.org/wiki/Binary_tree
TL;DR My ultimate problem is to find the two nodes on a proper binary tree (i.e. itself has at least two nodes) that one is only greater than input, and the other only less than input. (cont. under the line)
To implement that, I personally asserted that literally if you draw a tree (decently), you see horizontally the one on the right is by all means greater than any one on its left.
In other words, quoting from Wikipedia (Binary search tree):
The key in each node must be greater than all keys stored in the left sub-tree, and smaller than all keys in the right sub-tree.
And it seems to only guaranteed to be true locally. With a figure like this:
A
/ \
B C
/ \ / \
D E F G
/ \
H I
(Letters have no order, i.e. just for structure)
By locally I mean when we talk about node E, it's guaranteed that D (with F and G) is smaller than E, but what about C, F, G compared to E, is that also guaranteed?
This seems quite intuitive (that F,C,G are all greater than E), but I don't find anyway to prove that, so is there any counterexample or theoretical proof? Any existed theorems or suggestions?
EDIT: I finally find this equivalent to why is in-order traversal of a binary search tree has a non-decreasing order.
This seems quite intuitive (that F,C,G are all greater than E), but I don't find anyway to prove that, so is there any counterexample or theoretical proof? Any existed theorems or suggestions?
F > A — definition of BST ("key in each node must be … smaller than all keys in the right sub-tree")
E < A — definition of BST ("key in each node must be greater than all keys stored in the left sub-tree")
E < A < F — transitivity
And so on for C and G
Imagine, you have same tree without E:
A
/ \
B C
/ / \
D F G
/ \
H I
Now you insert this E that greater than B. What if E greater than A also? In this case it will be inserted to right subtree of A, well? But while E is in right subtree of B, it less than A:
A
/ \
B C
/ \ / \
D E F G
/ \
H I
You need to differentiate between a "binary tree" and a "binary search tree" to be precise about it.
A binary search tree has the property you're looking for; all nodes in the left branch are smaller than all nodes in the right branch. If it's weren't the case, you couldn't use the search method usually associated -- that is, look at a node, and if you're looking for a smaller node, go left, if you're looking for a larger node, go right. I think both a basic BST, plus balanced trees like AVL and red-black, all observe this same property.
There are other binary tree data structures which aren't "Binary Search Trees" -- for example, a min-heap and max-heap are both binary trees, but the 'left is smaller than right' constraint is not met all the way through the tree. Heaps are used to find the smallest or largest element in a set, so you only normally reason about the node at the top of the tree.
As to a proof, I guess there is this; if you accept that the search algorithm works, then we can show that this property must hold. For instance, in this tree;
a
/ \
b n
/ \
c d
then let's say you wanted to prove that d is smaller than n, or any child. Imagine you were searching for d and you were at a. Obviously, you compare a to d and find that d is smaller -- that's how you know to traverse left, to b. So right there we've got to have faith that the entire left tree (b and under) must all be smaller than a. The same argument for the right hand side and numbers greater than a.
So left-children(a) < a < right-children(a)
In terrible pseudo-proof...
This question has already been asked here: http://stackoverflow.com/questions/14092710/equality-of-two-binary-search-trees-constructed-from-unordered-arrays.
However, the solution that I thought of is fairly simple but hasn't been mentioned there.
The problem statement is: Given two arrays which represent a sequence of keys. Imagine we make a Binary Search Tree (BST) from each array. We need to tell whether two BSTs will be identical or not without actually constructing the tree.
Something that I thought of is, just sort the two arrays. If they are identical, then their inorder traversal will also be identical and hence if the two arrays are identical, then they definitely will lead to the same BST. Am I wrong in assuming that if the inorder traversal of two binary search trees is same, then trees will be same too?
Unless I'm misunderstanding what you mean by "inorder traversal", this won't work. Not only are inorder traversals of BSTs not unique, in fact the inorder traversal of every BST on the same set of elements is the same. Here's a small example:
4
/\
2/ \
/\ \
1 3 5
2
/\
/ \4
/ /\
1 3 5
Both trees have the inorder traversal 1, 2, 3, 4, 5. So your approach will report "IDENTICAL" even though the trees are different.
Your approach is actually wrong in the other direction too. If the two BSTs have the same structure but different elements, then you should report "IDENTICAL", but of course their sorted lists ( = inorder traversals) are different -- so in this case you will report "DIFFERENT".
Only inorder traversal can not define a unique BST. You have to get another pre/post order traversal to reconstruct the very BST.
Given an arbitrary set of values V and building a tree by inserting values left to right, what does it mean if I'm asked if my orderings of these values (to construct a minimum height and maximum height tree) are unique?
I've read on the internet it must follow a Hamiltonian Path, but we never learned this. And I'm also not quite sure what a Hamiltonian Path is.
Is there a proof that an ordering I choose is a unique ordering?
I believe (though I'm not fully positive) that the question is asking you whether there are multiple different orders into which you could insert the values into the BST that would produce the same tree.
For example, consider this tree:
1
/ \
0 2
There are two orders in which you could add the values into this tree to produce this result: 1, 0, 2 and 1, 2, 0.
On the other hand, this tree can only be formed in one way:
1
\
2
Namely, you have to insert 1 first, then 2.
Hope this helps!
For example,
consider the following tree's, check whether they exist in the list of BST.
5
/ \
4 6
/ \
1 3
3
/ \
2 4
How to approach to this problem?
Sort the list according to the root (if roots are same then left node etc). For each query tree do a binary search.
This works if the number of queries is comparable to number of elements in the list. Complexity: ( (n+m)logn) where m is the number of queries and n is the number of elements in the list.
If the number of queries is small, brute-force searching is efficient.
I'll put it up as an answer so people can make variations if they'd like.
A naive approach would be to just scan through the list, compare each node and once you see a difference in the two trees you're comparing, just go on to the next one in the list. => O(N) where N is the total number of nodes.
The answer to this question was put all the trees of the list in hash table so that there is constant time search for a tree.