Question

3. (Gaddis Exercises 20.4) Tree Height Write a recursive member function for the BinaryTree class that...

3. (Gaddis Exercises 20.4) Tree Height

Write a recursive member function for the BinaryTree class that returns the height of the tree. The height of the tree is the number of levels it contains. Demonstrate the function in a driver program.

CPP FILE CODE:

#include "BinaryTree.h"
#include <iostream>
using namespace std;

BinaryTree::BinaryTree()
{
        root = NULL;
}

BinaryTree::~BinaryTree()
{
        destroy(root);
}

bool BinaryTree::search(int data)
{
        return search(data, root);
}

void BinaryTree::insert(int data)
{
        insert(data, root);
}

void BinaryTree::traverseInOrder()
{
        traverseInOrder(root);
}

void BinaryTree::traversePreOrder()
{
        traversePreOrder(root);
}

void BinaryTree::traversePostOrder()
{
        traversePostOrder(root);
}

void BinaryTree::destroy(Node *tempNode)
{
        if (tempNode)
        {
                destroy(tempNode->left);
                destroy(tempNode->right);
                delete tempNode;
        }
}

bool BinaryTree::search(int data, Node *tempNode)
{
        if (!tempNode)
                return false;
        if (data == tempNode->data)
                return true;
        else if (data < tempNode->data)
                return search(data, tempNode->left);
        else
                return search(data, tempNode->right);
}

void BinaryTree::insert(int data, Node* &tempNode)
{
        if (!tempNode)
        {
                Node *newNode = new Node;
                newNode->data = data;
                newNode->left = NULL;
                newNode->right = NULL;
                tempNode = newNode;
        }
        else if (data < tempNode->data)
                insert(data, tempNode->left);
        else
                insert(data, tempNode->right);
}

void BinaryTree::traverseInOrder(Node *tempNode)
{
        if (tempNode)
        {
                traverseInOrder(tempNode->left); // left
                cout << tempNode->data << endl; // root
                traverseInOrder(tempNode->right);; // right
        }
}

void BinaryTree::traversePreOrder(Node *tempNode)
{
        if (tempNode)
        {
                cout << tempNode->data << endl; // root
                traversePreOrder(tempNode->left); // left
                traversePreOrder(tempNode->right);; // right
        }
}

void BinaryTree::traversePostOrder(Node *tempNode)
{
        if (tempNode)
        {
                traversePostOrder(tempNode->left); // left
                traversePostOrder(tempNode->right);; // right
                cout << tempNode->data << endl; // root
        }
}

HEADER FILE CODE:

struct Node
{
        int data;
        Node *left;
        Node *right;
};

class BinaryTree
{
private:
        Node *root;
        void destroy(Node*);
        bool search(int, Node*);
        void insert(int, Node*&);
        void traverseInOrder(Node*);
        void traversePreOrder(Node*);
        void traversePostOrder(Node*);
public:
        BinaryTree();
        ~BinaryTree();
        bool search(int);
        void insert(int);
        void traverseInOrder();
        void traversePreOrder();
        void traversePostOrder();
};
0 0
Add a comment Improve this question Transcribed image text
Answer #1

BinaryTree.h

#ifndef BINARYTREE_H_

#define BINARYTREE_H_

struct Node

{

int data;

Node *left;

Node *right;

};

class BinaryTree

{

private:

Node *root;

void destroy(Node*);

bool search(int, Node*);

void insert(int, Node*&);

void traverseInOrder(Node*);

void traversePreOrder(Node*);

void traversePostOrder(Node*);

int countNodes(Node*);

int maxDepth(Node*);

public:

BinaryTree();

~BinaryTree();

bool search(int);

void insert(int);

void traverseInOrder();

int countNodes();

void traversePreOrder();

void traversePostOrder();

int maxDepth();

};

#endif /* BINARYTREE_H_ */

main.cpp

#include "BinaryTree.h"

#include <iostream>

using namespace std;

BinaryTree::BinaryTree()

{

root = NULL;

}

BinaryTree::~BinaryTree()

{

destroy(root);

}

bool BinaryTree::search(int data)

{

return search(data, root);

}

void BinaryTree::insert(int data)

{

insert(data, root);

}

void BinaryTree::traverseInOrder()

{

traverseInOrder(root);

}

void BinaryTree::traversePreOrder()

{

traversePreOrder(root);

}

void BinaryTree::traversePostOrder()

{

traversePostOrder(root);

}

void BinaryTree::destroy(Node *tempNode)

{

if (tempNode)

{

destroy(tempNode->left);

destroy(tempNode->right);

delete tempNode;

}

}

bool BinaryTree::search(int data, Node *tempNode)

{

if (!tempNode)

return false;

if (data == tempNode->data)

return true;

else if (data < tempNode->data)

return search(data, tempNode->left);

else

return search(data, tempNode->right);

}

void BinaryTree::insert(int data, Node* &tempNode)

{

if (!tempNode)

{

Node *newNode = new Node;

newNode->data = data;

newNode->left = NULL;

newNode->right = NULL;

tempNode = newNode;

}

else if (data < tempNode->data)

insert(data, tempNode->left);

else

insert(data, tempNode->right);

}

int BinaryTree::countNodes()

{

return countNodes(root);

}

int BinaryTree::countNodes(Node *tempNode)

{

if (tempNode)

{

return 1+countNodes(tempNode->left)+

countNodes(tempNode->right);

}

return 0;

}

void BinaryTree::traverseInOrder(Node *tempNode)

{

if (tempNode)

{

traverseInOrder(tempNode->left); // left

cout << tempNode->data << endl; // root

traverseInOrder(tempNode->right);; // right

}

}

void BinaryTree::traversePreOrder(Node *tempNode)

{

if (tempNode)

{

cout << tempNode->data << endl; // root

traversePreOrder(tempNode->left); // left

traversePreOrder(tempNode->right);; // right

}

}

void BinaryTree::traversePostOrder(Node *tempNode)

{

if (tempNode)

{

traversePostOrder(tempNode->left); // left

traversePostOrder(tempNode->right);; // right

cout << tempNode->data << endl; // root

}

}

int BinaryTree::maxDepth(){

return maxDepth(root);

}

int BinaryTree::maxDepth(Node* node)

{

if (node==NULL)

return 0;

else

{

int lDepth = maxDepth(node->left);

int rDepth = maxDepth(node->right);

if (lDepth > rDepth)

return(lDepth+1);

else return(rDepth+1);

}

}

int main(){

BinaryTree tree;

tree.insert(1);

tree.insert(2);

tree.insert(3);

tree.insert(4);

tree.insert(5);

tree.insert(1);

tree.insert(3);

tree.insert(2);

tree.traverseInOrder();

cout<<"Total number of nodes : "<<tree.countNodes();

cout<<"\nTotal max depth is : "<<tree.maxDepth();

return 0;

}

ー ]JEE Outline ⓔ Make Target El Task List E Console X <terminated> CPP Workspace.exe [C/C++ Application] C:\Users ︿ 1 3 3 4 Total number of nodes 8 Total max depth is : 5

Add a comment
Know the answer?
Add Answer to:
3. (Gaddis Exercises 20.4) Tree Height Write a recursive member function for the BinaryTree class that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\...

    I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\ package Tree; public class BinaryTree { private TreeNode root; // head of the list    //constructor - create an empty binary tree public BinaryTree() { root = null;    }    //isEmpty() - return true if tree is empty, false otherwise public boolean isEmpty() { return (root == null); }    //deleteTree() - remove all items from tree public void deleteList() { root =...

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • Hi there, I am working on a binary search tree code in c++. The program must...

    Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...

  • 1. Write a function in Tree class which returns true if and only if the tree...

    1. Write a function in Tree class which returns true if and only if the tree satisfies the binary search tree property. The function’s header line is public boolean isValidBST() And in the attached code, you just need to finish the function after the comment: “//Instructor hint: please write your code here:” Make sure you execute your code, and the result in the main function after calling your function should be same as the prompt message I write. Clearly you...

  • Lab 6 Help

    I need help with this lab assignment. I use C++, thank you. Derive a MinHeap class from the BST class of your Lab 4 code.Define/Override only the Search/Insert/Delete methods in the new class.Write a new main for this lab which:Will only be a test program for the MinHeap.Declares and creates a MinHeap object as necessary.Declares and creates the Dollar objects as necessary.Inserts the 20 Dollar objects specified in Lab 4 in the same order.Performs all 4 traversals after the inserting...

  • Take the following code for a binary source tree and make it include the following operations....

    Take the following code for a binary source tree and make it include the following operations. bool replace(const Comparable & item, const Comparable & replacementItem); int getNumberOfNodes() const; (a) The replace method searches for the node that contains item in a binary search tree, if it is found, it replaces item with replacementItem. The binary tree should remain as a binary search tree after the replacement is done. Add the replace operation to the BinarySearchTree class. Test your replace using...

  • Language: C++ ○ For this question I task you with creating an Iterative Search function that...

    Language: C++ ○ For this question I task you with creating an Iterative Search function that works with the existing binary search tree of type string. This function will be inputted a variable of type string and will search for it within the existing and pre-declared binary tree. ○ The function appears just above the main, and is simply a placeholder. It can be altered as you wish. ○ Within the Main I have a simple program to prompt for...

  • Having code issues wth my C++ program. My program checks if two binary trees are similar...

    Having code issues wth my C++ program. My program checks if two binary trees are similar and if they're not they return false. My program is return true with different binary trees. Could use some help thanks #include <iostream> #include <string> using namespace std; //Struct of Nodes struct BinarySearchTree { int data; BinarySearchTree *left; BinarySearchTree *right; }; // Inserting nodes into BST BinarySearchTree* insert( BinarySearchTree* node, int val) { if (node == NULL) { BinarySearchTree *newNode = new BinarySearchTree(); newNode->data...

  • Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>...

    Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>       T data       TreeNode<T> left       TreeNode<T> right Since this TreeNode is a generic Template, use any data file we've used this quarter to store the data in the BinaryTree. To do this will likely require writing a compare function or operator. Hint: Think LEFT if the index is calculate (2n+1) and RIGHT if index is (2n+2). Source code: #include<iostream> using namespace std;...

  • Need help for three BinaryTree class public class BinaryTree { //Implements a Binary Tree of Strings...

    Need help for three BinaryTree class public class BinaryTree { //Implements a Binary Tree of Strings    private class Node {        private Node left;        private String data;        private Node right;        private Node parent; // reference to the parent node        // the parent is null for the root node        private Node(Node L, String d, Node r, Node p) {            left = L;            data...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT