Using c++  please include comments so I can learn how to do this.   Write a program that takes n elements as input and inserts them into a binary tree. Include a function to print out the elements in the tree.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Using c++  please include comments so I can learn how to do this.  

Write a program that takes n elements as input and inserts them into a binary tree. Include a function to print out the elements in the tree.

Expert Solution
Step 1

C++ Code:

#include <iostream>

#define MAX_ELEMS 100

using namespace std;

void MakeNode(struct bst * tree, int data)
{
    tree->data = data;
    tree->lindex = tree->rindex = -1;
}

void Insertleft( struct bst *tree, int data)
{
    MakeNode(tree, data);
}

void Insertright( struct bst * tree, int data)
{
    MakeNode(tree, data);
}

void Insert(struct bst * tree, int treeIndex, int data)
{
    int baseIndex = 0;
    
    while(baseIndex <= treeIndex)
    {
        if(data <= tree[baseIndex].data)
        {
            if(tree[baseIndex].lindex == -1)
            {
                tree[baseIndex].lindex = treeIndex;
                Insertleft(&tree[treeIndex], data);
                return;
            }
            else
            {
                baseIndex = tree[baseIndex].lindex;
                continue;
            }

        }
        else // data is > tree[baseIndex].data
        {
            if(tree[baseIndex].rindex == -1)
            {
                tree[baseIndex].rindex = treeIndex;
                Insertright(&tree[treeIndex], data);
                return;
            }
            else
            {
                baseIndex = tree[baseIndex].rindex;
                continue;
            }
        }
    }
}

void Intrav(struct bst * tree, int index)
{
    if(tree[index].lindex != -1)
        Intrav( tree, tree[index].lindex );
    cout<<"DataIn ="<<tree[index].data<<endl;
    if(tree[index].rindex != -1)
        Intrav( tree, tree[index].rindex );
}

void Pretrav(struct bst * tree, int index)
{
    cout<<"DataPre ="<<tree[index].data<<endl;
    if(tree[index].lindex != -1)
        Pretrav( tree, tree[index].lindex );
    if(tree[index].rindex != -1)
        Pretrav( tree, tree[index].rindex );
}

void Posttrav(struct bst * tree, int index)
{
    if(tree[index].lindex != -1)
        Posttrav( tree, tree[index].lindex );
    if(tree[index].rindex != -1)
        Posttrav( tree, tree[index].rindex );
    cout<<"DataPost ="<<tree[index].data<<endl;
}   

int main()
{
    struct bst tree[MAX_ELEMS];
    memset(tree, 0, sizeof(tree));
    int treeIndex = 0;

    MakeNode(&tree [treeIndex], 50);
    treeIndex++;
    Insert(tree, treeIndex, 10);
    treeIndex++;
    Insert(tree, treeIndex, 60);
    treeIndex++;
    Insert(tree, treeIndex, 25);
    treeIndex++;
    Insert(tree, treeIndex, 30);
    treeIndex++;
    Insert(tree, treeIndex, 92);
    treeIndex++;
    Insert(tree, treeIndex, 15);
    treeIndex++;
    Insert(tree, treeIndex, 67);
    
    Intrav(tree, 0);
    Pretrav(tree,0);
    Posttrav(tree, 0);

    return 0;
}
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Binomial Heap
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education