1

I have to create and describe an algorithm for my university course that gets a BST tree T and creates new BST tree T' that satifies properties (and is as fast as possible):
1) T' has the same exact key values as T
2) T' is a Red-Black tree

So far I've had only one idea: randomize 0 or 1. In case of 0, get the max key node from left subtree of T and insert it into T', otherwise get the min key node from right subtree of T and insert it into T'. This is to ensure that Red-Black tree is at least somewhat balanced. The insertion would be any standard RB insertion.
Complexity of getting min/max is O(h), and since this needs to be repeated for each of the nodes in T, this would get quite high. I could also keep a pointer at the max node of left subtree and min node of the right subtree, which would solve the problem of traversing the whole height of the tree every time.
What do you think about this solution? I'm pretty sure it can be done better. Sorry if there is an obvious better solution, but I couldn't find answer to this on the internet, also it's only my 2nd semester at the university and I don't have much experience with programming.

qalis
  • 1,314
  • 1
  • 16
  • 44

1 Answers1

0

Unless you have some other constraints or information, the fastest way is to forget about the shape of the original BST.

Just put the keys in an ordered list, and build a complete binary tree from it, all in O(N) time.

Then, if there's a partially filled leaf level, then color those nodes red. The rest are black.

Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87
  • Could you please elaborate on how would you put the keys in an ordered list? I only know how to go from BST to doubly linked list with root in the middle. Also, is ordered list faster here than doubly linked list? Using your method, how would you build a complete binary tree from sorted list? By going though list, dividing it into 2 lists and making the middle element root, and repeating that process with linking new roots as children? – qalis Apr 22 '18 at 19:23
  • By "ordered list", I just mean a list in order. You can use an array, a linked list, a doubly linked list, etc. For example, count the keys, make an array of the appropriate size, and do an in-order traversal to fill out the array. There are more specific descriptions of how to create the tree from the array over here: https://stackoverflow.com/questions/34970804/building-red-black-tree-from-sorted-array-in-linear-time – Matt Timmermans Apr 22 '18 at 19:34
  • tree nodes themselves can work as a list, just have them all go through the right child. The original BST is already in sorted order so you just have to take the next node at each step. Once you have that straight chain it's easy to turn back into a balance tree (and as said if you have a partially filled bottom level thse nodes are red, all others are black). – SoronelHaetir Apr 23 '18 at 03:19
  • @SoronelHaetir yes, that's true, but you have to know how to do an iterative in-order traversal. – Matt Timmermans Apr 23 '18 at 12:33