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

Implementing your own Tree ADT! Use the tree.h file which has all the methods to

ID: 3836753 • Letter: I

Question

Implementing your own Tree ADT!

Use the tree.h file which has all the methods to implement but do modify the tree.h file.

Use the tree_main.cpp file to test the programs functionality.

tree.h

#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.

// 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

tree_main.cpp

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

int main()
{
   Tree t;

   t.push(4);
   t.push(2);
   t.push(1);
   t.push(3);
   t.push(6);
   t.push(5);

   cout<<"t is : "<<endl;
   t.print();


   Tree t3(t);
   t3.push(7);

   cout<<"t is : "<<endl;
   t.print();
   cout<<"t3 is : "<<endl;
   t3.print();

   Tree t2;
   t2.push(2);
   t2.push(1);
   t2.push(3);

   t2 = t;

   t2.push(8);
   t2.push(9);
   t2.push(11);

   cout<<"t2 is : "<<endl;
   t2.print();
   cout<<"t is : "<<endl;
   t.print();

   t2.delete(1);
   t2.delete(5);

   cout<<"t2 is : "<<endl;
   t2.print();
  
   TreeNode *node = t.find(5);
   cout << "found: " << node->value << endl;

   node = t.find(100000);
   cout << "t.find(100000): " << node << endl;
}

Explanation / Answer

import java.util.Scanner;

/* category AVLNode */
category AVLNode
builder */
public AVLNode()

/* creator */
public AVLNode(int n)
  
}

/* category AVLTree */
category AVLTree
personal AVLNode root;   

/* creator */
public AVLTree()

/* perform to envision if tree is empty */
public Boolean isEmpty()
come back root == null;
}
/* build the tree logically empty */
public void makeEmpty()

/* perform to insert information */
public void insert(int data)

/* perform to induce height of node */
non-public int height(AVLNode t )
come back t == null ? -1 : t.height;
}
/* perform to GHB of left/right node */
non-public int max(int lhs, int rhs)
come back lhs &gt; rhs ? lhs : rhs;
}
/* perform to insert information recursively */
non-public AVLNode insert(int x, AVLNode t)
a pair of )
if( x &lt; t.left.data )
t = rotateWithLeftChild( t );
else
t = doubleWithLeftChild( t );
}
else if( x &gt; t.data )
two )
if( x &gt; t.right.data)
t = rotateWithRightChild( t );
else
t = doubleWithRightChild( t );
}
else
; // Duplicate; do nothing
t.height = max( height( t.left ), height( t.right ) ) + 1;
return t;
}
/* Rotate binary tree node with left kid */   
non-public AVLNode rotateWithLeftChild(AVLNode k2)
Dapsang.left;
k2.left = k1.right;
k1.right = k2;
k2.height = max( height( Mount Godwin Austen.left ), height( k2.right ) ) + 1;
k1.height = max( height( k1.left ), k2.height ) + 1;
return k1;
}

/* Rotate binary tree node with right kid */
non-public AVLNode rotateWithRightChild(AVLNode k1)
{
AVLNode Mount Godwin Austen = k1.right;
k1.right = k2.left;
k2.left = k1;
k1.height = max( height( k1.left ), height( k1.right ) ) + 1;
k2.height = max( height( Mount Godwin Austen.right ), k1.height ) + 1;
return k2;
}
/**
* Double rotate binary tree node: 1st left kid
* with its right kid; then node k3 with new left child */
non-public AVLNode doubleWithLeftChild(AVLNode k3)
come back rotateWithLeftChild( k3 );
}
/**
* Double rotate binary tree node: 1st right kid
* with its left kid; then node k1 with new right child */
non-public AVLNode doubleWithRightChild(AVLNode k1)
come rotateWithRightChild( k1 );
}
/* Functions to count range of nodes */
public int countNodes()
come back countNodes(root);
}
non-public int countNodes(AVLNode r)

}
/* Functions to look for a part */
public Boolean search(int val)
come back search(root, val);
}
non-public Boolean search(AVLNode r, int val)
{
Boolean found = false;
whereas ((r != null) &amp;&amp; !found)

found = search(r, val);
}
come found;
}
/* perform for inorder traversal */
public void inorder()

non-public void inorder(AVLNode r)

non-public void preorder(AVLNode r)

non-public void postorder(AVLNode r)
making object of AVLTree */
AVLTree avlt = new AVLTree();

System.out.println("AVLTree Tree Test ");
char ch;
/* Perform tree operations */
do