예제 #1
0
        public void BackBoneToBST()
        {
            TheTree T = new TheTree();    //a temporary tree

            convert(0, NodeCount - 1, T); //converting our right-only tree
            root = T.root;                //setting the temporary tree as the main one
        }
예제 #2
0
        public void convert(int start, int end, TheTree T)            //searching and adding an element
        {
            if (start > end)
            {
                return;
            }
            int  mid     = (start + end) / 2;        //calculating the middle element
            Node current = root;
            int  i       = 0;

            while (i < mid)
            {
                current = current.right;                 //moving to the calculated element
                i++;
            }
            T.Insert(T.root, current.value);       //inserting the value of the calculated element into the tree
            convert(start, mid - 1, T);            //doing the same for the left half
            convert(mid + 1, end, T);              //and for the right half
        }
예제 #3
0
    // Use this for initialization
    void Start()
    {
        TheTree myTree = new TheTree();

        myTree.textOut = textUI;         //first tree

        for (int i = 0; i < inputs.Length; i++)
        {
            myTree.Insert(myTree.root, inputs [i]);              //adding elements from the array into the list
        }

        myTree.PrintTree(myTree.root);             //printing the tree
        myTree.SearchTree(myTree.root, searchVal); //searching

        myTree.textOut = textUI2;                  //more space to print the tree

        textUI2.text += string.Format("Deleting {0} \n", deleteVal);
        myTree.Delete(myTree.root, null, deleteVal);
        myTree.PrintTree(myTree.root);

        TheTree newTree = new TheTree();

        newTree.textOut   = textUI3;       //second tree
        newTree.NodeCount = inputs2.Length;
        for (int j = 0; j < inputs2.Length; j++)
        {
            newTree.Insert(newTree.root, inputs2 [j]);              //adding elements from the array into the list
        }
        newTree.PrintTree(newTree.root);

        newTree.textOut = textUI4;
        textUI4.text   += "Balance:\n";
        newTree.MakeBackbone();         //making a right-only tree
        newTree.BackBoneToBST();        //converting to a balanced tree
        newTree.PrintTree(newTree.root);
    }