Question

In: Computer Science

C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary...

C++ tree program (please do NOT use struct Node, use classes)

Program 1 Implement a Binary tree using an array

Program 2 Implement a tree using linked list - pointer Binary Tree

Program 3 - Convert program 1 to a template

(include screenshots please of output)

Solutions

Expert Solution

Eg: the location of node 3 in the array is 3. So it’s left child will be placed at 2*3 = 6.

Its right child will be at the location 2*3 +1 = 7. As we can see in the array, children of 3 which are 6 and 7 are placed at location 6 and 7 in the array.

Used structure to declare a single node and then using a class, we develop a linked list of nodes.

#include<iostream>

using namespace std;

struct bintree_node{

    bintree_node *left;

    bintree_node *right;

    int data;

} ;

class bst{

    bintree_node *root;

    public:

    bst(){

        root=NULL;

    }

    int isempty() {

        return(root==NULL);

    }

    void insert(int item);

    void displayBinTree();

    void printBinTree(bintree_node *);

     

};

void bst::insert(int item){

    bintree_node *p=new bintree_node;

    bintree_node *parent;

    p->data=item;

    p->left=NULL;

    p->right=NULL;

    parent=NULL;

    if(isempty())

        root=p;

    else{

        bintree_node *ptr;

        ptr=root;

        while(ptr!=NULL){

            parent=ptr;

            if(item>ptr->data)       

                ptr=ptr->right;

            else

                ptr=ptr->left;

        }  

        if(item<parent->data)

            parent->left=p;

        else

            parent->right=p;

    }

}

void bst::displayBinTree(){

    printBinTree(root);

}

void bst::printBinTree(bintree_node *ptr){

    if(ptr!=NULL){

        printBinTree(ptr->left);

        cout<<" "<<ptr->data<<"     ";

        printBinTree(ptr->right);

    }

}

int main(){

    bst b;

    b.insert(20);

    b.insert(10);

    b.insert(5);

    b.insert(15);

    b.insert(40);

    b.insert(45);

    b.insert(30);

    cout<<"Binary tree created: "<<endl;

    b.displayBinTree();

}

Output:

Binary tree created:

5       10       15       20       30       40       45

PROGRAM 2:

struct TreeNode {
              int item;         // The data in this node.
              TreeNode *left;   // Pointer to the left subtree.
              TreeNode *right;  // Pointer to the right subtree.
           }
int countNodes( TreeNode *root ) {
           // Count the nodes in the binary tree to which
           // root points, and return the answer.
        if ( root == NULL )
           return 0;  // The tree is empty.  It contains no nodes.
        else {
           int count = 1;   // Start by counting the root.
           count += countNodes(root->left);  // Add the number of nodes
                                            //     in the left subtree.
           count += countNodes(root->right); // Add the number of nodes
                                            //    in the right subtree.
           return count;  // Return the total.
        }
     } // end countNodes()
 void preorderPrint( TreeNode *root ) {
           // Print all the items in the tree to which root points.
           // The item in the root is printed first, followed by the
           // items in the left subtree and then the items in the
           // right subtree.
        if ( root != NULL ) {  // (Otherwise, there's nothing to print.)
           cout << root->item << " ";      // Print the root item.
           preorderPrint( root->left );    // Print items in left subtree.
           preorderPrint( root->right );   // Print items in right subtree.
        }
     } // end preorderPrint()
void postorderPrint( TreeNode *root ) {
           // Print all the items in the tree to which root points.
           // The items in the left subtree are printed first, followed 
           // by the items in the right subtree and then the item in the
           // root node.
        if ( root != NULL ) {  // (Otherwise, there's nothing to print.)
           postorderPrint( root->left );    // Print items in left subtree.
           postorderPrint( root->right );   // Print items in right subtree.
           cout << root->item << " ";       // Print the root item.
        }
     } // end postorderPrint()


     void inorderPrint( TreeNode *root ) {
           // Print all the items in the tree to which root points.
           // The items in the left subtree are printed first, followed 
           // by the item in the root node, followed by the items in
           // the right subtree.
        if ( root != NULL ) {  // (Otherwise, there's nothing to print.)
           inorderPrint( root->left );    // Print items in left subtree.
           cout << root->item << " ";     // Print the root item.
           inorderPrint( root->right );   // Print items in right subtree.
        }
     } // end inorderPrint()

output:

 preorderPrint outputs:   1  2  4  5  3  6
       
 postorderPrint outputs:  4  5  2  6  3  1
       
 inorderPrint outputs:    4  2  5  1  3  6

Template:

class T

{

    T(void);

    T(G *pGIn, const unsigned long s, char nIn);

    ~T(void);

    // Member functions

    public:

    bool Expand(const unsigned long newS);

    void Empty(void); private:G *pG;

    char n;

    unsigned long s;

    int f;

    TEntry *p;

};

class TEntry
{// Constructors
    public:
    TEntry();
    TEntry(int l);
// Member functions
    public:
    void Relocate(int delta);
private:// Data members
    int k;TEntry *p;

};

class TEntry
{// Constructors
    public:
    TEntry();
    TEntry(int l);// Member functions
    public:
    void Relocate(int delta);
private: // Data members
    int k;
    TEntry *p;
};
T::T()
{
    p=NULL; s=0; pG=NULL;
    Empty();
    return;
}
T::T(G *pGIn, const unsigned long m, char nIn)
{
    pG=pG; n=nIn;
    return;
}
T::~T(void)
{
    if(p!=NULL)
        delete[] p;
    return;
}
bool T::Expand(const unsigned long newS)
{
    T *pBefore=p;
    p=(T *)_realloc_dbg(p, newS*sizeof(T), _NORMAL_BLOCK,__FILE__,__LINE__);
    s=newS;
    return p!=NULL;
}
void T::Empty()
{
    f=0;
    return;

}

T::T()
{
}
T::T(int i){

k=i;

}

void T::Relocate(int delta){

k+=delta;

return;

}


Related Solutions

C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary...
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree...
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree using an array    Program 2 Implement a tree using linked list - pointer Binary Tree    Program 3 - Convert program 1 to a template
Consider the following struct that represents a node within a binary tree: struct Node { int...
Consider the following struct that represents a node within a binary tree: struct Node { int data; // Data of interest Node *left // Link to left subtree (nullptr if none) Node *right ; // Link to right subtree (nullptr if none) }; Complete the following function that computes the number of elements in a binary tree: // Counts the number of elements in the binary tree to which t points. // Returns the number of elements. int size(Node *t)...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions...
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions below. a) int getSmallest(Node * r); // return smallest value in the BST with root r. Assume r not null. b) int getSecondSmallest(Node * r); // return 2nd smallest value in BST with root r. Assume r not null and r has a nonnull left or right child. c) void removeSecondSmallest(Node * r); // remove 2nd smallest value in BST with root r. Assume...
Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition. If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode *find(int item) { //implement code here return nullptr; } int main() {    root = new...
In C++, Implement the heapafication operation. Do not re-implement the binary tree class. Simply create a...
In C++, Implement the heapafication operation. Do not re-implement the binary tree class. Simply create a funcntion that takes in a binary tree and heapafies it. Meaning it takes in a pointer to a binary tree and converts it into a heap. (You may choose max or min heap).
c++ Binary Search Tree: find parent of a node please insert comment to explain what the...
c++ Binary Search Tree: find parent of a node please insert comment to explain what the code do. Use struct Node{ int data; Node* left; Node* right};
Implement a function to remove a leaf node from a binary search tree. Using the following...
Implement a function to remove a leaf node from a binary search tree. Using the following class and function definition: class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode * remove_leaf(int item, bool &removed) { // implement } The remove function will return the node with the item if it's present in the tree. If the node is a leaf node and is removed by the function,...
Please do this code with python. Thank you! struct Node {     int data;     struct...
Please do this code with python. Thank you! struct Node {     int data;     struct Node* left;     struct Node* right; }; // Node creation struct Node* newNode(int data) {     struct Node* nn         = new Node;     nn->data = data;     nn->left = NULL;     nn->right = NULL;     return nn; } // Function to insert data in BST struct Node* insert(struct Node* root, int data) {   if (root == NULL)         return newNode(data);     else {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT