Inserting into Red-Black Tree - red-black-tree

purely conceptually, how would I insert 21 into this red-black tree?

You find the point of insertion. Starting with root
Compare 21 with 25 => it is less => follow to left child
Compare 21 with 15 => it is more => follow to right child
Compare 21 with 22 => it is less => follow to left child
Compare 21 with 20 => it is more => there is no right child so you insert.
Insert the node as a RED node. Now there is a Red Child-Red Parent violation
Parent is 20. Grandparent is 22, Uncle is 24.
When Parent and Uncle are both RED you fix the violation by flipping the colours of Parent, Grandparent & Uncle.
Now consider the Grandparent as the new node to fix (22). It's Parent is 15, also RED, a new violation.
For new violation: Node is 22, Parent is 15, Grandparent is 15, Uncle is 35.
When Parent is RED and Uncle is BLACK, you fix by rotation.
You see if the Node is on the inside of the tree and rotate outwardly about the parent 15, leftwards. 22 becomes the new left child of 25, colours don't change.
Now a rotation rightwards at the root of tree 25, plus swapping the colours of the root and its right child fixes the tree.
You should have

Related

BST Construction with multiple choices

I encountered the following question:
The Height of a BST constructed from the following list of values 10 , 5 , 4 , 3 , 2 , 1 , 0 , 9 , 13 , 11 , 12 , 16 , 20 , 30 , 40 , 14 will be:
A) 5
B) 6
C) 7
D) 8
Now to certain point I can construct the Tree with no problem because there aren't many choices to insert the values so from the first value 10 to the eleventh value 12 my tree would go like this:
10
/ \
5 13
/ \ /
4 9 11
/ \
3 12
/
2
/
1
/
0
But after that, now I have to add the value 16 and I have two choices to make it the right child of the value 12 or to make it the right child of the value 13 and the height would differ according to this choice, so what's the right approach here?
A binary search tree is a binary tree (that is, every node has at most 2 children) where for each node in the tree, all nodes in the left subtree rooted at that node are less than the root of the subtree and all nodes in the right subtree rooted at that node are greater than the root of the subtree.
Your walkthrough so far assumes a "naive" insertion algorithm that makes no attempt at balancing the tree and inserts the nodes in sequence by recursively comparing against each node from the root. Under typical circumstances, you would not want to build such an unbalanced tree because the performance of operations devolves into O(n) time instead of the optimal O(log(n)) time, which defeats the purpose of the BST structure.
Making 16 the right child of 12 would be an invalid choice because 16 as a left child of 13 violates the BST property. By definition, every insertion must go in one location, so there are no choices to be made, at least following this naive algorithm.
Following through with your approach, it should be clear that the final height (longest root-to-leaf path) will be 7 in any case due to the unbalanced left branch.
However, if the question is to find the height of an optimal tree, then the correct answer is 5. The below illustrates a complete tree (that is, every level is full except the bottom level, and that level has all elements to the left) that can be built from the given data:
11
+---------------+
4 16
+------+ +-------+
2 9 14 30
+---+ +---+ +---+ +---+
1 3 5 10 12 13 20 40
+--
0
Take a look at self-balancing binary search tree data structures for more information on algorithms that can build such a tree.

Is this an AVL tree?

Is the following an AVL tree?
10
5 15
7 12 17
8
5-7-8 being the long branch
In an AVL tree, the heights of the two child subtrees of any node differ by at most one. Is this satisfied here?
No, it is not, because the subtrees of node 5 differ by more than 1 in height. The left subtree is empty (its height is 0) and the right subtree is 7-8 (so its height is 2).

pre-order and "tree_insert" on a BST

if I have a BST (call it - T) and run PRE-ORDER on it,
how can I show/prove that running the function "tree_insert" on the sequence I got from the pre-order, I get exactly the same tree-T (I started with) back?
Thanks,
For example, let's say you have 3 elements.
When you insert, the first element you insert will be taken as the root, then the next element(whether it is lower or larger) will placed accordingly to the left or right. Pre-order traversal means it will visit root first, then recursively visit the left child, then recursively visit the right child. So after pre-order will display root, the smaller element, and then the larger element. Now try inserting those 3 elements again. You will receive the same tree. (The first element inserted will be the root. Then the smaller element will again go to the left, and the larger element will automatically go to the right). You can model this with 6 different scenarios with 3 elements.
Scenario 1: Elements to insert = {1, 2, 3}
root = 1, right child = 2, right-most = 3
When doing preOrder, 1 is visited first. No left child so 2 is visited next. 2 has no left child so 3 is visited next. (1, 2, 3)
Scenario 2: Elements to insert = {2, 1, 3}
root = 2, left child = 1, right child = 3
When doing preOrder, 2 is visited first. Left child 1 so it's visited next. Right child is 3, so it's visited next. (2, 1, 3)
Scenario 3: Elements to insert = {3, 1, 2}
root = 3, left child = 1, right child of left child(with height of 1) = 2
When doing preOrder, 3 is visited first. Left child is 1 so it's visited next. No left child of 1, so 3 is visited next. (3, 1, 2)
There are 3 more scenarios which you can write out and check for yourself.
For a given pre order traversal of a tree, there can be more than one BSTs formed. A unique BST can be generated if you have INORDER traversal too alongside the pre order traversal.

Inserting into a BST

I want to create a visualization of a BST, but every example I find online stops after inserting only 7 or less values. Let's say I'm doing the following sequence:
insert(5),insert(7),insert(9),insert(8),insert(3),insert(2),insert(4),insert(6),insert(10).
Up until insert(6), I end up with:
My main question is: where do I go from here? Do I add on to my left-most leaf or do I add on to my "lowest" leaf?
Also: according to wikipedia the code for an insertion is:
void insert(Node* node, int value) {
if (value < node->key) {
if (node->leftChild == NULL)
node->leftChild = new Node(value);
else
insert(node->leftChild, value);
} else {
if(node->rightChild == NULL)
node->rightChild = new Node(value);
else
insert(node->rightChild, value);
}
}
But according to this, once I'm at 8 and I get insert(3), it would add 3 to the left of 8, as it would compare 3 with the node 9, see that the less-than spot is already taken by 8, then rerun the insertion with 8 being the node compared to, and place the 3 as the left child of 8. But this would just create kind of a list.
Thanks.
What seems to mislead you is that on every insert, you must start from the root (which, in your case, is 5). So, let's take the graph above and try to insert 3 following the algorithm you pasted:
3 < 5, we go left and we meet 3
3 == 3, we go right (here it's the same, the code above says "go right") and we meet 4
3 < 4, we go left. Since 4 has no left child, 3 becomes its left child.
Try to build your tree from zero using the algorithm above. Incidentally, you won't find many examples with n > 10 nodes, because they tend to be exceedingly long without any benefit for the reader.

Order of insertion for worst case black height of a red black tree

Lets say we are dealing with the keys 1-15. To get the worst case performance of a regular BST, you would insert the keys in ascending or descending order as follows:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
Then the BST would essentially become a linked list.
For best case of a BST you would insert the keys in the following order, they are arranged in such a way that the next key inserted is half of the total range to be inserted, so the first is 15/2 = 8, then 8/2 = 4 etc...
8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15
Then the BST would be a well balanced tree with optimal height 3.
The best case for a red black tree can also be constructed with the best case from a BST. But how do we construct the worst case for a red black tree? Is it the same as the worst case for a BST? Is there a specific pattern that will yield the worst case?
You are looking for a skinny tree, right? This can be produced by inserting [1 ... , 2^(n+1)-2] in reverse order.
You won't be able to. A Red-Black Tree keeps itself "bushy", so it would rotate to fix the imbalance. The length of your above worst case for a Red-Black Tree is limited to two elements, but that's still not a "bad" case; it's what's expected, as lg(2) = 1, and you have 1 layer past the root with two elements. As soon as you add the third element, you get this:
B B
\ / \
R => R R
\
R