简体   繁体   中英

Find the best root for Binary Search Tree

I am trying to find the best root for my binary search tree. ie this is my binary search tree.

         42
       /    \
      /      \
     25      53
    /  \    /  \
  23    41 49   55
 /  \  / \ / \ /  \

then I save them in an array in the form of inOrder walk.

| 23 | 25 | 41 | 42 | 49 | 53 | 55 |.........
...........↑...........↑...........↑...........

so the ones pointing the arrows are the nodes that I'll try to see which one is the best root 41, 42, 53.(the array can be bigger and I'll take the odd indexes of the array with a given Depth of the tree).

So i have to try every single odd index as my new root and compare each height with the previous one and like that I can determine which one is my best height. ie if I decided 25 is my new root I can have a tree like this

          25                            25
            \                             \
             \                             \
             42          or                 53
               \                            /
                53                         42
                  \                       /

so for each I check and compare the height with the previous node in the array and i return the node that it will give me the best node. So far i tried this:

void rebalance(){

    //this is the size for the array and NumDepth is defined at the constructor

    int size = (pow (2,(numDepth +1) )-1);
    Node* Array2 [size];
    int i = 0;
    int bestH = 0;
    Node* temp;

    for (int i=0; i < size; i++){
            Array2[i]= new Node();
            Array2[i]= NULL;
    }
    //this function will be the one creates the inOrder walk and saves my nodes inside the array
    storeInOrder(rootBST, Array2, i, numDepth);

    temp = shortestBST(Array, rootBST, bestH, height);


}

Node* shortestBST(Node *root[], Node* root, int &bestHeigth, int sizeA) {
//root is my main tree basically 

//this is how i know that i have to use the odd numbers in the array

    for(int i= 1; i< sizeA; i+=2){

       //inside here I am supposed to do a recursion to check every node inside the array to check the node that is the best
      //they gave me a hint saying that i can point the nodes in the array to the ones in my main tree to create the tree with the new testing root in order to check if that node can create a best tree but i don't know how to do that using recursion
//each of my nodes hold a key, a height and a size of a subtree , left to point to the left and a right to point to the right

    }




}

Node::Node() {

    sizeSub=0;
    height=1;
    key=0;
    left=NULL;
    right=NULL;
}

Since you're starting with the numbers in sorted order, finding the root node is pretty trivial: the ideal root is the median of the numbers to be inserted.

This makes it fairly trivial to do the insertion recursively: find the median, insert it as the root, then insert the left sub-array as the left child and the right sub-array as the right child.

There's a similar question and an algorithm that looks like just what is being asked of here:

  1. Using right-rotation operations, turn the tree into a linked list (aka backbone or vine)
  2. Rotate every second node of the backbone about its parent to turn the backbone into a perfectly balanced BST.

http://www.geekviewpoint.com/java/bst/dsw_algorithm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM