Order of insertion for worst case black height of a red black tree - binary-search-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

Related

Code to obtain the maximum value out of a list and the three consecutive values after the maximum value

I am writing a model to calculate the maximum production capacity for a machine in a year based on 15-min data. As the maximum capacity is not the sum of the required capacity for all 15-min over the year, I want to write a piece of code that determines the maximum value in the list and then adds this maximum value and the three next consecutive values after this maximum value to a new variable. An simplified example would be:
fifteen_min_capacity = [10, 12, 3, 4, 8, 12, 10, 9, 2, 10, 4, 3, 15, 8, 9, 3, 4, 10]
The piece of code I want to write would be able to determine the maximum capacity in this list (15) and then add this capacity plus the three consecutive ones (8,9,3) to a new variables:
hourly_capacity = 35
Does anyone now the code that would give this output?
I have tried using the max(), the sum() and a combination of both. However, I do not get a working code. Any help would be much appreciated!

How can I use IF and ELSE IF in looping and display 2 statements in GAMS?

I am a beginner level in this program. I try to improve this loop according to this condition. The details are as follows:
When CUTI(k) = CUTI(k)-4 then,
1)If the result shows this CUTI(k) value greater than 0, then print this CUTI(k) value.
2)If the result shows CUTI(k) value less than 0, then print this CUTI(k) value is added 12 with showing a word "*" after the number in display, e.g. 10*, 9*
I am not sure this loop is correct and enough to add this condition. Look forward to seeing your recoomendation. :)
set k /1*20/;
parameter
CUTI(k)/1 6, 2 2, 3 8, 4 5, 5 1, 6 3, 7 7, 8 8, 9 6, 10 8,11 1, 12 2, 13 4, 14 7,
15 5, 16 2, 17 8, 18 9, 19 2, 20 10/;
loop(k,
if(CUTI(k)-4 > 0,
CUTI(k) = CUTI(k)-4;
else
CUTI(k) = (CUTI(k)-4)+12 ;
)
);
display CUTI;
Your logic looks correct. However, instead of the loop/if/else you could simplify this to one assignment:
CUTI(k) = CUTI(k)-4+12$(CUTI(k)<=4);
However, modifying the display statement by adding a * to some elements is not possible. If you need to distinguish the cases in such a statement, you might assign the values to two different parameters and display them individually.

Is Hive QL IN bad for performance?

I have a Hive SQL like
select 5 in (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
When I explain it it tell me that hive will scan this one:
== Physical Plan ==
*(1) Project [true AS (5 IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20))
+- Scan OneRowRelation[]
Does that mean hive will perform a sequence scan instead of a set look up on this set (which cause a bad performance)?
The answer can be found in the implementation of
org.apache.hadoop.hive.ql.udf.generic.GenericUDFIn.
Simply put, it depends on the args in the parentheses. Specifically whether they are all constants.
In the case of all constants, a HashSet will be initialized once for each mapper respectively. And when evaluation is done at one row, just check if the value is in the set.
And if at least one of the arg is not a constant, that is, args depend on the row being evaluated, then a for loop that goes through all the args is executed for each row. So in this case, long argument list slows down the execution.
See the code here for more details: GenericUDFIn.
Have a nice day :)

Dask aggregate value into fixed range with start and end time?

In dask or even pandas how would you go about grouping an dask data frame that has a 3 columns of time / level / spread into a set of fixed ranges by time.
Time is only used to move one direction. Like a loop counting up. So the end result would be start time and end time with high of level, low of level, first value of level and last value of level over the fixed range? Example
12:00:00, 10, 1
12:00:01, 11, 1
12:00:02, 12, 1
12:00:03, 11, 1
12:00:04, 9, 1
12:00:05, 6, 1
12:00:06, 10, 1
12:00:07, 14, 1
12:00:08, 11, 1
12:00:09, 7, 1
12:00:10, 13, 1
12:00:11, 8, 1
For a fixed level range of (7). So level from start to end can not be more than 7 total distance from start to end for each bin of level. Just because first bin is only 8 difference in time and second is only 2 different in time, this dose not madder one the high to low madders that the total distance from high to low dose not go passed 7 the fixed bin size. The first bin could have been 5 not 8 for first bin and 200 for next bin not 2 in the example below. So the First few rows in dask would look something like this.
First Time, Last Time, High Level, Low Level, First Level, Last Level, Spread
12:00:00, 12:00:07, 13, 6, 10, 13, 1
12:00:07, 12:00:09, 14, 7, 13, 7, 1
12:00:09, X, 13, 7, X, X, X
How could this be aggregated in dask with a fix window of level moving forward in time binning each time level moves above X or equal too high/low with in X or below X?

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.