-1

So I am not really sure how the Template Class and the ItemType are interacting here. Should I just be changing root->data to the value? I keep getting a seg fault when doing that. So I figured I would have to create a new node and then assign that to the new root node?

Here's what I have so far. Thank for any help :)

The part I am having trouble with is adding a new Node. Specifically I have trouble with the line in the first if statement. I am not sure how to do it.

I thought it would be as simple as

root->data = value;

.....but its not

Main.cpp is below...

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


int main (void)
{

    TreeType <int> tree;
        srand(100); 
    cout<<"\nINSERTING\n";
    for (int i=0;i<15;i++)
    {
        int number=rand()%100;
        cout<<"Atttempting to insert "<<number<<endl;
        tree.Insert(number);
    }
    cout<<"\nPRINT ALL\n";
    tree.Print();
    cout<<endl;

    int array[16]={40,23,1,5,28,84,81,80,97,90,47,56,70,71,79,90};  
    for(int i=0;i<16;i++)
    {
        if (tree.DeleteItem(array[i]))
            cout<<"Attempting to delete "<<array[i]<<endl;
    }



    cout<<"\nPRINT ALL\n";
    tree.Print();


    return 0;
}

Treend.h is below...

#ifndef TREEND_H
#define TREEND_H
template <class NODETYPE>
class TreeNode{

public:
    TreeNode(const NODETYPE &value);
    TreeNode(){right=0;left=0;}
    TreeNode<NODETYPE> *left, *right;
    NODETYPE data;


};
template<class NODETYPE>
TreeNode<NODETYPE>::TreeNode(const NODETYPE &value)
{
    left = 0;
    right = 0;
    data = value;
}


#endif

tree.h is below...

#ifndef TREE
#define TREE
#include <fstream>
#include <iostream>
using namespace std;
#include "Treend.h"
template<class ItemType>
class TreeType 
{

   public:
      TreeType();
      TreeType (const TreeType &original){CopyTree(root, original.root);}
      void operator=(TreeType &orginalTree);
      bool Search(ItemType &value);
      void Insert(ItemType value);
      void Print();
     ~TreeType();
      bool isEmpty() {return root==0;}
      bool DeleteItem(ItemType value);
      bool PrintOne(ItemType value);
      void Update(ItemType value, ItemType &newvalue);


   private:
      TreeNode<ItemType>* root;
      void CopyTree(TreeNode<ItemType>*& copy, const TreeNode<ItemType>* original);

      bool BinarySearch(TreeNode<ItemType>* root, ItemType &value);
      void InsertItem(TreeNode<ItemType>*&  root, ItemType value);
      void PrintTree(TreeNode<ItemType>* root);
      void Destroy(TreeNode<ItemType>*& root); 
      bool PrintOneNode(TreeNode<ItemType>* root, ItemType value);
      bool UpdateTree(TreeNode<ItemType>*& root, ItemType value, ItemType newvalue);
      bool Delete(TreeNode<ItemType>*& root, ItemType);
      void FindMin(TreeNode<ItemType>* root, ItemType &data);

};

#include "tree.cpp"
#endif

Unfinished tree.cpp below... (working on insert now)

#include "tree.h"
#ifndef TREE_CPP
#define TREE_CPP

template<class ItemType>
void TreeType<ItemType>::operator=(TreeType<ItemType> &original)
{
    if(original == this)
        return; //ignore assigning self to self
    Destroy(root);
    CopyTree(root, original.root);
    cout<<"";
}


template<class ItemType>
void TreeType<ItemType>::CopyTree(TreeNode<ItemType>*& copy, const TreeNode<ItemType>* original)
{
    if (original == NULL)
        copy = NULL;
    else 
    {
        copy = new TreeNode<ItemType>;
        copy-> data = original->data;
        CopyTree(copy->left, original->left);
        CopyTree(copy->right, original->right);
    }
}

template<class ItemType>
bool TreeType<ItemType>::DeleteItem(ItemType item)
{
   return Delete(root, item);
}


template<class ItemType>
void TreeType <ItemType>::FindMin(TreeNode <ItemType>* root, ItemType &data)
{
   while(root->left != 0)
   {

          root = root->left;
   }
   data = root->data;
}


template<class ItemType>
TreeType<ItemType>::~TreeType()
{
   Destroy(root);
}


template <class ItemType>
void TreeType<ItemType>::Destroy(TreeNode<ItemType>*& root)
{

}


template <class ItemType>
bool TreeType<ItemType>::BinarySearch(TreeNode<ItemType>* root, ItemType &value)
{
   if(root == 0)
   {
       cout<<value<<" not found\n";
      return false;
   }
   else if(root->data == value)
   {
      value = root->data;
      return true;
   }
   else if (root->data <= value)
      return(BinarySearch(root->right, value));
   else
      return(BinarySearch(root->left, value));

}
template <class ItemType>
TreeType<ItemType>::TreeType()
{
   root = 0;
}
template <class ItemType>
bool TreeType<ItemType>::Search(ItemType &value)
{
   return(BinarySearch(root, value));
}
template<class ItemType>
void TreeType<ItemType>::Insert(ItemType item)
{
   InsertItem(root, item);
}

//changed the ItemType from TreeNode

template<class ItemType>
void TreeType<ItemType>::InsertItem(TreeNode<ItemType>*& root, ItemType value)
{ 
   // no nodes exist yet.
   if (root->data == 0) {
      root = new TreeNode<ItemType>(value);
   } else if (root->data > value) { 
      InsertItem(root->left, value); 
   } else if (root->data < value) { 
      InsertItem(root->right, value);
   }
}


template<class ItemType>
void TreeType<ItemType>::Print()
{
   PrintTree(root);
}


template <class ItemType>
void TreeType<ItemType>::PrintTree(TreeNode<ItemType>* root)
{

}

template <class ItemType>
bool TreeType<ItemType>::PrintOne(ItemType value)
{
   return (PrintOneNode(root, value));
}

template <class ItemType>
bool TreeType<ItemType>::PrintOneNode(TreeNode<ItemType>* root, ItemType value)
{
   if(root == 0)
   {cout<<"Not Found"<<endl;
      return false;
   }
   else if (root-> data == value)
   {
      cout << root->data;
      return false;
   }
   else if (root -> data >= value)
      return PrintOneNode(root->left, value);
   else
      return PrintOneNode(root->right, value);
}  




template <class ItemType>
void TreeType<ItemType>::Update(ItemType value, ItemType &newvalue)
{
   UpdateTree(root, value, newvalue);
}

template <class ItemType>
bool TreeType<ItemType>::UpdateTree(TreeNode <ItemType>*& root, ItemType value, ItemType newvalue)
{

}

template<class ItemType>
bool TreeType<ItemType>::Delete(TreeNode<ItemType>*& root, ItemType item)
{

}
#endif
jose reyes
  • 1,533
  • 1
  • 13
  • 17

2 Answers2

1

As this seems to be a problem for a class I will Refrain from blindly providing code. adding a node isn't simply adding the data to an empty node. Remember a node is made of data and pointers. You first need to find the location were the node needs to go. This is usually done recursively. Once the correct spot is found, then you add the node. Hope this helps.

user9950573
  • 41
  • 1
  • 3
0
if (!root) {
root = new TreeNode<ItemType>();
root->value = value;
} ect

Credit To: https://chat.stackoverflow.com/users/7703024/super

jose reyes
  • 1,533
  • 1
  • 13
  • 17