I'm working through a past exam paper for my advanced programming course and I've gotten stuck at this question
What property must the values in a binary search tree satisfy? How many different binary search trees are there containing the three values 1 2 3? Explain your answer.
I can answer the first part easily enough but the second bit, about the number of possible trees has me stumped. My first instinct is to say that there is only a single tree possible, with 2 as the root because the definition says so, but this question is work 8 marks out of a total of 100 for the entire paper, so I can only assume that it's a trick question, and there's a more subtle explanation, but there's nothing in the lecture notes that explains this. Does anyone know who to answer this question?
The question doesn't say that the tree is balanced, so think about whether 1 or 3 can be at the root node.
Try to think about all possible binary trees with these three nodes. How many of those trees fulfill the property of binary search tree?
I think that a trick is that a tree can be a degenerate one (effectively, a linked list of elements):
1
\
2
\
3
And variations thereof.
Also, are these trees considered to be identical ?
2 2
/ \ / \
3 1 1 3
If I remember correctly, the root of the tree does not have to be the "middle element". Thus there are a few more combinations of trees:
2
1 3
or
1
2
3
or
1
3
2
or
3
2
1
or
3
1
2
Maybe I forget a few, but I think you'll get the idea. Just for my notation: Newline meets get down in the tree, right and left of the upperline showes whether it is right or left of its parent node ;)
Related
Lets consider that, the following is the resultant of the Level Order Traversal of the Binary Tree.
Ex: 1,2,3,4,5,6,7,8
But, I got a question like, with the given list of data, how to compute the total number of levels in the binary tree.
I thought some thing like, Sqrt(8) and doing the Math.Round to it, will yield the result.
But I doubt that, I am wrong.
May I know, what is the perfect to do that.
Thanks in advance...
In the general case, a binary tree with n nodes will have at least 1 + floor(log_2(n)) levels. For example, you can fit 7 nodes on 3 levels, but 8 nodes will take at least 4 levels no matter what.
Also in the general case, the upper limit is n levels in the case of a degenerate binary tree (which looks like a linked list hanging down from the root). Consider your example, where the level-order traversal (also known as breadth-first traversal) is 1 2 3 4 5 6 7 8. The following cases are possible, along with everything in between:
1 1
/ \ \
/ \ 2
2 3 \
/ \ / \ 3
4 5 6 7 \
/ 4
8 \
5
(4 levels) \
6
\
7
\
8
(8 levels)
There are particular types of binary trees for which you can put stronger constraints on the upper limit. For complete or full binary trees, the number of levels is always 1 + floor(log_2(n)), because the shape of the tree depends only on n.
If you label the nodes with an index in breadth-first order, you can compute the level without any traversal in O(1) time. So if you are doing multiple queries, you can do an O(N) BFT and have each query answered in O(1) time.
The formula for the level is:
level = floor(log(index + 1))
Where the log is to the base 2
This link help you How can I calculate the level of a node in a perfect binary tree from its depth-first order index?
The height of a complete binary tree is up to O(logN).
Where N is the number of nodes you fill in the tree.
Note that Big O notation is required due to the fact that the actual height could vary by some addition or scaling factor.
https://www.cs.cmu.edu/~adamchik/15-121/lectures/Trees/trees.html
Level index value either can starts from 0 or 1.
If you are counting the level index starting from 0 (i.e., Root at Level 0) then
#no.of levels = floor(log_2(n))
If you are counting the level index starting from 1 (i.e., Root at Level 1) then
#no.of levels = 1 + floor(log_2(n))
I had thought I understood BSTs.
That was until my Professor came along.
Let's say I have a BST:
2
/ \
1 3
Now if I were to insert 4, my tree would look like this:
2
/ \
1 3
\
4
but my Professor's tree would end up like this:
2
/ \
1 4
/ \
3 4
Basically, he finds where the new node should be placed and places it there. He then changes the value of the new node's parent to the new node's value and makes the left child of the parent what the original parent node used to be.
I have looked around online but can't find anyone doing this.
What kind of insertion technique is this? Am I missing something?
I don't think it would make a difference but this was specifically for AVL trees.
I think he is trying to keep the tree strictly binary ,i.e., each node has 0 or exactly 2 children.
As i understand, in the example, the first and second 4s are added in their places. A left rotation (required for balancing the avl tree) brings the final shape.
Your insertion of key "4" into a BST is completely right, I can assure you. This is the orthodox and the simplest way to insert elements into a BST.
I think that you've misunderstood your professor, because your third illustration is incorrect at least because of the fact that in BST no duplicates are allowed, and you have element "4" occurring twice.
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.
If in-order traversal of two binrary trees (not binary search trees) are the same, does it guarantee that the two trees are the same?
if answer is no, then what about both in-order and pre-order traversal are the same?
Definitely not. The two trees
b
/ \
a d
/ \
c e
and
d
/ \
b e
/ \
a c
both have an inorder traversal of a b c d e. They are, in fact, rotations, an operation which preserves inorder traversal.
NO, and its seen with this simple example
3 2
2 1 3
1 0
0
Both have same inorder traversals [0,1,2,3]
But if inorder and preorder traversals are same then the trees are equal.
I'm thinking "no."
It's possible for two trees to be balanced differently, but have the same "order" of node values. For instance, if, of the set
1,2,3,4,5,6,7
You build a tree:
4
2 6
1 3 5 7
in-order traversal will yield 1,2,3,4,5,6,7.
however, if you choose a different root node (if the list is not sorted correctly beforehand)
5
4 6
2 7
1 3
These two trees will give the same in-order traversal result, but are (clearly) not identical.
or even
7
6
5
4
3
2
1
et cetera.
This is also related to a problem with BSP (binary space partition) trees, typically used in game development collision detection and visibility determination.
The BSP stores triangles in a tree. Each node contains a triangle or facet. The left node contains all children that are "behind" the facet, while the right child contains everything that is in "front." Recurse as expected.
If you pick the left-most facet in the scene as your root, the right child will then own every other facet. If you make a bad decision for the right child, the same thing will happen. It's perfectly possible for one to build a BSP compiler that, through idiotic analysis, builds a "tree" that is actually a list (as in my last example above). The problem is to partition the data set so that each node divides the remaining list as equally as possible. This is one of the reasons that BSPs are typically generated at compile time, as building one for a very complex geometry can take hours to find the/an optimal solution.
Inorder and any one of pre-order or post-order, uniquely define a tree structure. Nothing less.
One thing you can do is use level order
5
4 6
2 7
1 3
lvel order- 5 4 6 2 N N 7 1 3 N N N N N N
First of all, sorry for the title. Someone please propose a better one, I really didn't know how to express my question properly.
Basically, I'm just looking for the name of a data structure where the elements look like this (ignore the dots):
......5
....3...2
..4...1...6
9...2...3...1
I first thought it could be a certain kind of "tree", but, as wikipedia says:
A tree is [...] an acyclic connected graph where each node has zero or more children nodes and at most one parent node
Since there can be more than one parent by node in the data structure I'm looking for, it's probably not a tree.
So, here's my question:
What is the name of a data structure that can represent data with the following links between the elements? (/ and \ being the links, again, ignore the dots):
......5
...../..\
....3...2
.../..\./..\
..4...1...6
../.\./..\./..\
9...2...3...1
I think it isn't totally wrong to call it a Tree, although "Digraph" (directed graph) would be a more proper term.
First of all, sorry for the title.
Someone please propose a better one, I
really didn't know how to express my
question properly.
The title is fine, I LOL'd hard when I opened the question. I am going to start calling them "Bowling Pins" now :)
5
3 2
4 1 6
9 2 3 1
The most popular thing I reckon, which was laid out like this, is Pascal's triangle. It's the structure used to calculate binomial coefficients; each node is the sum of its parents:
http://info.ee.surrey.ac.uk/Personal/L.Wood/publications/MSc-thesis/fig36.gif.
Usuallly, when it comes to implementing such algorithms (such class is commonly referred to as "dynamic programming"), this "structure" is usually represented as a simple two-dimensional array. See here, for example:
n\k 0 1 2 3 4
------------------
0 1 0 0 0 0
1 1 1 0 0 0
2 1 2 1 0 0
3 1 3 3 1 0
4 1 4 6 4 1
5 1 5 10 10 5
6 1 6 15 20 15
I think, that there's no formal name for such a structure, but in dynamic programming such stuff is just... arrays.
But from now on, as NullUserException suggests I'm totally calling it "bowling pins" :-)
A "dag", or directed acyclic graph, is a graph with directed edge in which there may be multiple paths to a node, and some nodes may have both incoming and outgoing edges, but there is no way to leave any node and return to it (there are no cycles). Note that in a finite DAG at least one node must have nothing but outgoing edges and at least one must have nothing but incoming edges. Were that not the case, it would be possible to move continuously through the graph without ever reaching a dead end (since every node would have an exit), and without visiting any node twice (since the graph is acyclic). If there are only a finite number of nodes, that's obviously impossible.
Since there can be more than one parent by node in the data structure I'm looking for, it's probably not a tree.
What you're looking for is probably a graph. A tree is a special case of a graph where each node has exactly one parent. (except the root which has none)