Question
Data Structures C++ A node's level is the number of parent's nodes from the root to node. The root is always at level 0. The
Data Structures C++
A node's level is the number of parent's nodes from the root to node. The root is always at level 0. The root's children are a level 1 and so on. A level's width is the number of nodes at the level. A tree's width the greatest width of all of a tree's levels' width. Add iterative functions to the binaryTree class to compute a tree's width.
BINARYSEARCHTREE.H
//Header File Binary Search Tree
#ifndef H_binarySearchTree #define H_binarySearchTree #include
//************************************************************* // Author: D.S. Malik // // This class specifies the basic operations to implement a // binary search tree. //*************************************************************
using namespace std;
template
void insert(const elemType& insertItem); //Function to insert insertItem in the binary search tree. //Postcondition: If no node in the binary search tree has the // same info as insertItem, a node with the info insertItem // is created and inserted in the binary search tree.
void deleteNode(const elemType& deleteItem); //Function to delete deleteItem from the binary search tree //Postcondition: If a node with the same info as deleteItem // is found, it is deleted from the binary search tree
private: void deleteFromTree(binaryTreeNode
template
if (root == NULL) cerr << "Cannot search the empty tree." << endl; else { current = root;
while (current != NULL && !found) { if (current->info == searchItem) found = true; else if (current->info > searchItem) current = current->llink; else current = current->rlink; }//end while }//end else
return found; }//end search
template
newNode = new binaryTreeNode
if (root == NULL) root = newNode; else { current = root; while (current != NULL) { trailCurrent = current;
if (current->info == insertItem) { cerr << "The insert item is already in the list-"; cerr << "duplicates are not allowed." << insertItem << endl; return; } else if (current->info > insertItem) current = current->llink; else current = current->rlink; }//end while
if (trailCurrent->info > insertItem) trailCurrent->llink = newNode; else trailCurrent->rlink = newNode; } }//end insert
template
if (root == NULL) cout << "Cannot delete from the empty tree." << endl; else { current = root; trailCurrent = root;
while (current != NULL && !found) { if (current->info == deleteItem) found = true; else { trailCurrent = current;
if (current->info > deleteItem) current = current->llink; else current = current->rlink; } }//end while
if (current == NULL) cout << "The delete item is not in the tree." << endl; else if (found) { if (current == root) deleteFromTree(root); else if (trailCurrent->info > deleteItem) deleteFromTree(trailCurrent->llink); else deleteFromTree(trailCurrent->rlink); }//end if } }//end deleteNode
template
if (p == NULL) cerr << "Error: The node to be deleted is NULL." << endl; else if(p->llink == NULL && p->rlink == NULL) { temp = p; p = NULL; delete temp; } else if(p->llink == NULL) { temp = p; p = temp->rlink; delete temp; } else if(p->rlink == NULL) { temp = p; p = temp->llink; delete temp; } else { current = p->llink; trailCurrent = NULL;
while (current->rlink != NULL) { trailCurrent = current; current = current->rlink; }//end while
p->info = current->info;
if (trailCurrent == NULL) //current did not move; //current == p->llink; adjust p p->llink = current->llink; else trailCurrent->rlink = current->llink; delete current; }//end else }//end deleteFromTree
#endif
BINARYTREE.H
//Header File Binary Search Tree #ifndef H_binaryTree #define H_binaryTree
//************************************************************* // Author: D.S. Malik // // class binaryTreeType // This class specifies the basic operations to implement a // binary tree. //*************************************************************
#include
using namespace std;
//Definition of the node template
//Definition of the class template
int treeHeight() const; //Returns the height of the binary tree. int treeNodeCount() const; //Returns the number of nodes in the binary tree. int treeLeavesCount() const; //Returns the number of leaves in the binary tree. void destroyTree(); //Deallocates the memory space occupied by the binary tree. //Postcondition: root = NULL;
binaryTreeType(const binaryTreeType
binaryTreeType(); //default constructor
~binaryTreeType(); //destructor
//leavesCount();
protected: binaryTreeNode
private: void copyTree(binaryTreeNode
void destroy(binaryTreeNode
void inorder(binaryTreeNode
int height(binaryTreeNode
//Definition of member functions
template
template
template
template
template
template
template
template
template
template
template
template
//Overload the assignment operator template
if (otherTree.root == NULL) //otherTree is empty root = NULL; else copyTree(root, otherTree.root); }//end else
return *this; }
template
template
//copy constructor template
template
template
template
template
return 0; }
template
#endif
How exactly do I approach this problem?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started