Modify this code to do an Insert function for an AVL tree in

Modify this code to do an Insert function for an AVL tree, instead of a B-Tree .

Please compile in Microsoft Visual studio, as it is the IDE that is required for my course.

Here is the code for the header file and the Source file.

Soure File :

#include <iostream>
#include \"bTree.h\"

using namespace std;

int main()
{
bTree<int, 5> treeRoot;
   
int num;

cout << \"Enter numbers ending with -999\" << endl;

cin >> num;

while (num != -999)
{
        treeRoot.insert(num);
  cin >> num;
}

    cout << \"Inorder traversal data: \";
    treeRoot.inOrder();
cout << endl;

return 0;

Header File:

#ifndef H_bTree
#define H_bTree

#include <iostream>
#include <cassert>

using namespace std;

//*************************************************************
// Author: D.S. Malik
//
// class bTree
// This class specifies the basic operations to implement a
// B-tree.
//*************************************************************

template <class recType, int bTreeOrder>
struct bTreeNode
{
    int recCount;
    recType list[bTreeOrder - 1];
    bTreeNode *children[bTreeOrder];
};

template <class recType, int bTreeOrder>
class bTree
{
public:
    bool search(const recType& searchItem);
      //Function to determine if searchItem is in the B-tree.
      //Postcondition: Returns true if searchItem is found in the
      //    B-tree; otherwise, returns false.

    void insert(const recType& insertItem);
      //Function to insert insertItem in the B-tree.
      //Postcondition: If insertItem is not in the the B-tree, it
      //    is inserted in the B-tree.

    void inOrder();
      //Function to do an inorder traversal of the B-tree.

    bTree();
  //constructor

    //Add additional members as needed

protected:
    bTreeNode<recType, bTreeOrder> *root;

private:
    void searchNode(bTreeNode<recType, bTreeOrder> *current,
                    const recType& item,
                    bool& found, int& location);
    void insertBTree(bTreeNode<recType, bTreeOrder> *current,
                    const recType& insertItem,
                    recType& median,
                    bTreeNode<recType, bTreeOrder>* &rightChild,
                    bool& isTaller);
    void insertNode(bTreeNode<recType, bTreeOrder> *current,
                    const recType& insertItem,
                    bTreeNode<recType, bTreeOrder>* &rightChild,
                    int insertPosition);
    void splitNode (bTreeNode<recType, bTreeOrder> *current,
                    const recType& insertItem,
                    bTreeNode<recType, bTreeOrder>* rightChild,
                    int insertPosition,
                    bTreeNode<recType, bTreeOrder>* &rightNode,
                    recType &median);
    void recInorder(bTreeNode<recType, bTreeOrder> *current);
};

template <class recType, int bTreeOrder>
bTree<recType, bTreeOrder>::bTree()
{
    root = NULL;
} //end constructor

template <class recType, int bTreeOrder>
bool bTree<recType, bTreeOrder>::search(const recType& searchItem)
{
    bool found = false;
    int location;

    bTreeNode<recType, bTreeOrder> *current;

    current = root;

    while (current != NULL && !found)
    {
        searchNode(current, item, found, location);

        if (!found)
            current = current->children[location];
    }

    return found;
} //end search

template <class recType, int bTreeOrder>
void bTree<recType, bTreeOrder>::searchNode
                   (bTreeNode<recType, bTreeOrder> *current,
                    const recType& item,
                    bool& found, int& location)
{
    location = 0;

    while (location < current->recCount
           && item > current->list[location])
        location++;

    if (location < current->recCount
           && item == current->list[location])
        found = true;
    else
        found = false;
} //end searchNode

template <class recType, int bTreeOrder>
void bTree<recType, bTreeOrder>::insert(const recType& insertItem)
{
    bool isTaller = false;
    recType median;

    bTreeNode<recType, bTreeOrder> *rightChild;

    insertBTree(root, insertItem, median,
                rightChild, isTaller);

    if (isTaller) //the tree is initially empty or the root
                  //was split by the function insertBTree
    {
        bTreeNode<recType, bTreeOrder> *tempRoot;
        tempRoot = new bTreeNode<recType, bTreeOrder>;
        tempRoot->recCount = 1;
        tempRoot->list[0] = median;
        tempRoot->children[0] = root;
        tempRoot->children[1] = rightChild;

        root = tempRoot;
    }
} //insert

template <class recType, int bTreeOrder>
void bTree<recType, bTreeOrder>::insertBTree
                   (bTreeNode<recType, bTreeOrder> *current,
                    const recType& insertItem,
                    recType& median,
                    bTreeNode<recType, bTreeOrder>* &rightChild,
                    bool& isTaller)
{
    int position;
    isTaller = false;

    if (current == NULL)
    {
        median = insertItem;
        rightChild = NULL;
        isTaller = true;
    }
    else
    {
        bool found;

        searchNode(current, insertItem, found, position);

        if (found)
            cout << \"Cannot insert duplicate record.\" << endl;
        else
        {
            recType newMedian;

            bTreeNode<recType, bTreeOrder> *newChild;

            insertBTree(current->children[position], insertItem,
                        newMedian, newChild, isTaller);

            if (isTaller)
            {
                if (current->recCount < bTreeOrder - 1)
                {
                    isTaller = false;
                    insertNode(current, newMedian,
                               newChild, position);
                }
                else
                    splitNode(current, newMedian, newChild,
                              position, rightChild, median);
            }
        }
    }
} //insertBTree

template <class recType, int bTreeOrder>
void bTree<recType, bTreeOrder>::insertNode
                   (bTreeNode<recType, bTreeOrder> *current,
                    const recType& insertItem,
                    bTreeNode<recType, bTreeOrder>* &rightChild,
                    int insertPosition)
{
    int index;

    for (index = current->recCount; index > insertPosition;
                                    index--)
    {
        current->list[index] = current->list[index - 1];
        current->children[index + 1] = current->children[index];
    }

    current->list[index] = insertItem;
    current->children[index + 1] = rightChild;
    current->recCount++;
} //end insertNode

template <class recType, int bTreeOrder>
void bTree<recType, bTreeOrder>::splitNode
                   (bTreeNode<recType, bTreeOrder> *current,
                    const recType& insertItem,
                    bTreeNode<recType, bTreeOrder>* rightChild,
                    int insertPosition,
                    bTreeNode<recType, bTreeOrder>* &rightNode,
                    recType &median)
{
    rightNode = new bTreeNode<recType, bTreeOrder>;

    int mid = (bTreeOrder - 1) / 2;

    if (insertPosition <= mid) //new item goes in the first
                                  //half of the node
    {
        int index = 0;
        int i = mid;

        while (i < bTreeOrder - 1)
        {
            rightNode->list[index] = current->list[i];
            rightNode->children[index + 1] =
                                current->children[i + 1];
            index++;
            i++;
        }

        current->recCount = mid;
        insertNode(current, insertItem, rightChild,
                   insertPosition);
        (current->recCount)--;

        median = current->list[current->recCount];

        rightNode->recCount = index;
        rightNode->children[0] =
                  current->children[current->recCount + 1];
    }
    else //new item goes in the second half of the node
    {
        int i = mid + 1;
        int index = 0;

        while (i < bTreeOrder - 1)
        {
            rightNode->list[index] = current->list[i];
            rightNode->children[index + 1] =
                                  current->children[i + 1];
            index++;
            i++;
        }

        current->recCount = mid;
        rightNode->recCount = index;

        median = current->list[mid];
        insertNode(rightNode, insertItem, rightChild,
                   insertPosition - mid - 1);
        rightNode->children[0] =
                  current->children[current->recCount + 1];
    }
} //splitNode

template <class recType, int bTreeOrder>
void bTree<recType, bTreeOrder>::inOrder()
{
    recInorder(root);
} // end inOrder

template <class recType, int bTreeOrder>
void bTree<recType, bTreeOrder>::recInorder
                     (bTreeNode<recType, bTreeOrder> *current)
{
    if (current != NULL)
    {
        recInorder(current->children[0]);

        for (int i = 0; i < current->recCount; i++)
        {
            cout << current->list[i] << \" \";

            recInorder(current->children[i + 1]);
        }
    }
} //end recInorder

#endif

Solution

In a binary tree the balance issue of a node N is described to be the height distinction

BalanceFactor(N) := –peak(LeftSubtree(N)) + height(RightSubtree(N)) [6]
of its infant subtrees. A binary tree is defined to be an AVL tree if the invariant

BalanceFactor(N) –1,zero,+1
holds for each node N in the tree.

A node N with BalanceFactor(N) < 0 is called \"left-heavy\", one with BalanceFactor(N) > 0 is known as \"proper-heavy\", and one with BalanceFactor(N) = 0 is once in a while clearly called \"balanced\".

statement
inside the sequel, because there\'s a one-to-one correspondence among nodes and the subtrees rooted via them, we every so often depart it to the context whether the call of an object stands for the node or the subtree.

properties[edit]
balance factors can be saved up to date through knowing the preceding balance elements and the exchange in top – it is not important to recognize absolutely the height. for containing the AVL balance statistics, two bits in keeping with node are enough.[7]

the height h of an AVL tree with n nodes lies within the c programming language:[8]

log2(n+1) h < c log2(n+2)+b
with the golden ratio := (1+5) 2 1.618, c := 1 log2 1.44, and b := c2 log2 5 – 2 –zero.328. that is due to the fact an AVL tree of top h includes at the least Fh+2 – 1 nodes where Fh is the Fibonacci collection with the seed values F1 = 1, F2 = 1.

Modify this code to do an Insert function for an AVL tree, instead of a B-Tree . Please compile in Microsoft Visual studio, as it is the IDE that is required fo
Modify this code to do an Insert function for an AVL tree, instead of a B-Tree . Please compile in Microsoft Visual studio, as it is the IDE that is required fo
Modify this code to do an Insert function for an AVL tree, instead of a B-Tree . Please compile in Microsoft Visual studio, as it is the IDE that is required fo
Modify this code to do an Insert function for an AVL tree, instead of a B-Tree . Please compile in Microsoft Visual studio, as it is the IDE that is required fo
Modify this code to do an Insert function for an AVL tree, instead of a B-Tree . Please compile in Microsoft Visual studio, as it is the IDE that is required fo
Modify this code to do an Insert function for an AVL tree, instead of a B-Tree . Please compile in Microsoft Visual studio, as it is the IDE that is required fo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site