Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone help me implement these functions in this header file:: #ifndef _TRE

ID: 3839084 • Letter: C

Question

Can someone help me implement these functions in this header file::

#ifndef _TREE_H

#define _TREE_H

#include <cstdlib> // necessary in order to use NULL

class TreeNode

{

public:

   TreeNode() : left(NULL), right(NULL) {}

   TreeNode* left;

   TreeNode* right;

   int value;

};

class Tree

{

public:

   // Default constructor

   Tree();

   // Copy constructor

   Tree(const Tree& other);

   //Destructor

   ~Tree();

   // overloaded Assignment Operator

   Tree& operator=(const Tree& other);

   // Similar to insert function we discussed earlier

   // creates node and inserts it at appropriate position.

   void push(int value);

   // Returns the address of the node containing the value.

   TreeNode* find(int value) const;

   // Print the tree data

   void print() const;

   // Deletes the node with value in the tree and deallocates its memory.

   void deleteNode(int value);

  

private:

   // Root of the tree.

   TreeNode* start;

   //copyOther

// you should implement and use this helper function inside your

// copy constructor and overloadedAssignment operator.

   void copyOther(const Tree& other);

   // clear

// you should implement and use this function inside your

// destructor to delete all the nodes and free memory

   void clear();

   // pushFrom

// Recursively push a single element into a tree.

// Use it in your push function.

   void pushFrom(TreeNode* startingPoint, TreeNode* nodeToPush);

  

   // findFrom

// Recursively find a single element in a tree.

   TreeNode* findFrom(TreeNode* startingPoint, int value) const;

   // printFrom

//

// Recursively print the values in a tree. Use

// pre-order traversal.

//

// If a tree looks like this:

//

// 6

// /

// /

// 5 8

// / /

// / /

// 0 7 9

//

// then it should be printed like this:

//

// 6

// 5

// 0

// 8

// 7

// 9

//

// Helper function that you should use inside your print function

   void printFrom(TreeNode* startintPoint, int numSpaces) const;

   // copyFrom

// Recursively copy another tree's nodes. Use

// pre-order traversal. Use this in CopyOther function.

   void copyFrom(TreeNode* startintPoint);

   // deleteFrom

// should implement and use in the delete function.

// Deletes the node with the value specified in the below function.

   void deleteFrom(TreeNode* startintPoint, int value);

   // clearFrom

// Recursively delete nodes. Use post-order traversal.

   // Use it in clear function.

   void clearFrom(TreeNode* startingPoint);

};

#endif

Explanation / Answer

Answer

Code for few of the functions are as below"

//Default constructor
Tree()
{
   this.value = TreeNode.value;
   this.left = TreeNode.left;
   this.right = TreeNode.right;
}

//Destructor

~Tree()
{
   delete TreeNode.left;
delete TreeNode.right;

// this will delete any other resources allocated by the node.
}


// This method mainly calls insertRec()
void push(int value)
{
start = insertRec(start, value);
}

/* A recursive function to creates node and inserts it at appropriate position*/
TreeNode insertRec(TreeNode root, int key) {

/* If the tree is empty, return a new node */
if (root == null) {
root = new Node(key);
return root;
}

/* Otherwise, recur down the tree */
if (key < root.key)
root.left = insertRec(root.left, key);
else if (key > root.key)
root.right = insertRec(root.right, key);

/* return the (unchanged) node pointer */
return root;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote