is this possible that a node in the complete binary tree has just one child?
thanks
Can this be a complete binary tree?
23
/ \
12 15
/ \
9 11
/ \ \
10 5 13
OK, first to make the difference between a perfect and a complete binary tree. In a perfect binary tree every node has two children(if not a leaf) or no children(if a leaf). So a perfect binary tree of level N has totally 2^(N + 1) - 1 nodes. But if we talk about complete binary tree - this means every level, except the last is full, and the last level may not be full. Also in a complete binary tree, the last level nodes must be filled from left to right.
So if you talk about perfect binary tree, it is not possible. But if you mean the complete binary tree, it is possible to have only one child.
I would say it is possible:
*
/ \
/ \
* x
/ \ /
* * *
this is a
binary tree in which
every level, except possibly the last,
is completely filled, and all nodes
are as far left as possible
And node x has just one child.
Citing Wikipedia:
A complete binary tree is a binary
tree in which every level, except
possibly the last, is completely
filled, and all nodes are as far left
as possible.
Which means no.
From the other answers:
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
23
/ \
12 15
/ \
9 11 <- not the last level, but not completely filled!
/ \ \
10 5 13 <- last level: not completely filled, but that's okay
So this example tree is not complete, according to this definition.
Related
Does an balanced binary tree has an unique form?
For example, the node list [1,2,3,4,5]
It seems like following two forms are all qualified with the definition of balanced binary tree. Are they all correct?
2
/ \
1 4
/ \
3 5
3
/ \
1 4
\ \
2 5
No. There isn't. A balanced tree may have different order based on the order of operations made in order to get to it. Also, there are multiple ways to do a self balancing tree (Red-Black, AVL, Splay) - all result (usually) in different trees.
A simple counter example with two nodes on AVL tree:
insert(1)
insert(2)
Will result in no need for rebalancing, and the tree will be:
1
\
\
2
If you do it the other way around:
insert(2)
insert(1)
Again, no rebalancing is needed, but the result tree will be:
2
/
/
1
Both are valid AVL trees with the same elements, but as you can see - the form is not unique.
The structure of balanced binary search tree OR for that matter binary search tree is much dependent on the sequence of data you provide to it.
With AVL (self-balancing binary tree) you will yield a unique form; provided you have same sequence of data, that in your case is [1,2,3,4,5].
2
/ \
1 4
/ \
3 5
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))
My goal is to create a BST (Binary Search Tree) for compiler of pseudo-pascal code.
From what I know BST works like this: I have a root, which has some key - in dependency on MY GENERATED KEY (if it is greater or lesser than root key) I insert a node to the left or right from the root.
Obviously, if I use numbers for keys, the tree will go like:
1
\
2
\
3
What I want is a balanced BST, something like this:
1
/ \
2 3
... etc.
Which is obviously not gonna happen with those keys. How should keys for nodes look like when I want to achieve this structure? Is there a way to do this without remembering the last inserted key?
That also brings me to the question what "key" should be used for absolute root of the tree? So I can add keys to the left and to the right from it?
When you insert a node, you travel from root node down to find proper insertion point:
1 1 1
\ \
2 2
\
3
After insertion, when you return back to root, on the way you check that each left and right nodes are balanced enough. If not, you rotate them:
1 2
\ / \
2 1 3
\
3
This means that first node of deeper branch comes the new root node, and old root node will become child of this node.
So root node will change when necessary, and will always have the "most middle" value.
Apply the concept of AVL tree to balance the tree structure.
According to your requirement:
1
/ \
2 3
The above tree is constructed just considering a binary tree not a binary Search Tree.
However above mentioned tree can be constructed by top-to-down and from left-to-right fashion.
Recently I heard from professor in my university about such data structure as "matrix tree". I can understand what it is, but where is it useful?
I'll try to give short explanation for this structure:
We have a tree root - special node. Then we have left and right "children" (subtrees). Both are binary trees. And if there is no some numbers in the tree but we add their "descendants" then we add this missing numbers as parasitic (so subtrees is nearly full). In left tree all nodes are even numbers. Others are in right tree. And for N we can say that N = 2^L(2*Y - 1) where N - node value (even, in this case), L - level number and Y - position in level.
Example (even subtree):
8
/ \
4 12
/ \ /\
2 6 10 14
If we exclude, for example, 4, it become parasitic (special flag in node) and that's all.
So I'm self teaching AVL trees and I understand the basic idea behind it, but I just want to make sure my intuition of actually implementing it is valid:
I'll examine it with the left rotation-
So, the following situation is simple:
8
/ \
7 10
/
6
/
3
When we add the 3, the tree rebalances itself to:
8
/ \
6 10
/ \
3 7
But is the rotation based on the addition of the 3 or the imbalance of the subtree rooted at 7? Is it even based on the imbalance of the tree rooted at 8?
The following example is where things get a bit hairy, in my opinion:
9
/ \
7 10
/ \
6 8
/
3
So, in this case, the subtree at 7 is fine when the 3 is added, so that subtree doesn't need to rotate. However, the tree at 9 is imbalanced with the addition of 3, so we base the rotation at 9. We get:
7
/ \
6 9
/ / \
3 8 10
So in writing my code, which I will quite soon, would the following code, starting from small subtrees working up to bigger subtrees do the trick?
pseudocode:
function balanceTree(Node n){
if (n is not null){
balanceTree(n.rightchild);
balanceTree(n.leftchild);
}
if (abs(balanceFactor(n))>1){
rotateAsNeeded(n);// rotate based on balance factor
}
}
Thanks in advance!
The pseudocode that you've posted will correctly balance a tree. That said, it is too inefficient to be practical - notice that you're recursively exploring the entire tree trying to do rebalancing operations, which will make all insertions and deletions take O(n) time, eating away all the efficiency gains of having a balanced tree.
The idea behind AVL trees is that globally rebalancing the tree can be done by iteratively applying local rotations. In other words, when you do an insertion or deletion and need to do tree rotations, those rotations won't appear in random spots in the tree. They'll always appear along the access path you took when inserting or deleting the node.
For example, you were curious about inserting the value 3 into this tree:
9
/ \
7 10
/ \
6 8
Let's start off by writing out the difference in balance factors associated with each node (it's critical that AVL tree nodes store this information, since it's what makes it possible to do insertions and deletions efficiently):
9(+1)
/ \
7 (0) 10 (0)
/ \
6(0) 8(0)
So now let's see what happens when we insert 3. This places the 3 here:
9(+1?)
/ \
7 (0?) 10 (0)
/ \
6(0?) 8(0)
/
3(0)
Notice that I've marked all nodes on the access path with a ?, since we're no longer sure what their balance factors are. Since we inserted a new child for 6, this changes the balance factor for the 6 node to +1:
9(+1?)
/ \
7 (0?) 10 (0)
/ \
6(+1) 8(0)
/
3(0)
Similarly, the left subtree of 7 grew in height, so its balance factor should be incremented:
9(+1?)
/ \
7 (+1) 10 (0)
/ \
6(+1) 8(0)
/
3(0)
Finally, 9's left subtree grew by one, which gives this:
9(+2!)
/ \
7 (+1) 10 (0)
/ \
6(+1) 8(0)
/
3(0)
And here we find that 9 has a balance factor of +2, which means that we need to do a rotation. Consulting Wikipedia's great table of all AVL tree rotations, we can see that we're in the case where we have a balance factor of +2 where the left child has a balance factor of +1. This means that we do a right rotation and pull the 7 above the 9, as shown here:
7(0)
/ \
6(+1) 9(0)
/ / \
3(0) 8(0) 10 (0)
Et voilĂ ! The tree is now balanced.
Notice that when we did this fixup procedure, we didn't have to look over the entire tree. Instead, all we needed to do was look along the access path and check each node there. Typically, when implementing an AVL tree, your insertion procedure will do the following:
If the tree is null:
Insert the node with balance factor 0.
Return that the tree height has increased by 1.
Otherwise:
If the value to insert matches the current node, do nothing.
Otherwise, recursively insert the node into the proper subtree and get the amount that the tree height has changed by.
Update the balance factor of this node based on the amount that the subtree height changed.
If this mandates a series of rotations, perform them.
Return the resulting change in the height of this tree.
Since all these operations are local, the total work done is based purely on the length of the access path, which in this case is O(log n) because AVL trees are always balanced.
Hope this helps!
PS: Your initial example was this tree:
8
/ \
7 10
/
6
/
3
Note that this tree isn't actually a legal AVL tree, since the balance factor of the root node is +2. If you consistently maintain tree balance using the AVL algorithm, you will never encounter this case.