Binary tree destructor

I am trying to write the destructor for my binary search tree.
But I have no idea how to write this.
I have tried
-----------------------
delete root; and delete [] root; but it doesn't work.
Please help me.

so, here is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31


#ifndef TREE_H
#define TREE_H

struct PersonRec
{
	char name[20];
	int bribe;
	PersonRec* lChild;
	PersonRec* rChild;
	PersonRec (char*, int, PersonRec*, PersonRec* );
};

class CTree
{
	private:
		PersonRec* root;
		void AddItem (PersonRec* &, char*, int);
		void DisplayTree(PersonRec*);
//		void destroySub(PersonRec*);

	public:
		CTree();
		~CTree();
		bool isEmpty();
		void Add();
		void View();
};
#endif

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118


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

// PersonRec 4-arg constructor
PersonRec::PersonRec(char* N, int B, PersonRec* L = NULL, PersonRec* R = NULL)
{
	for (int x = 0; x < 40; x++)
		name[x] = N[x];
	bribe = B;
	lChild = L;
	rChild = R;
}

// CTree constructor which initailized root to NULL
CTree::CTree()
{	
	root = NULL;
}

// CTree destructor
CTree::~CTree()
{
	cout << "Destructor is running . . . " << endl;
//	delete [] root;
	delete root->lChild;
	delete root->rChild;
}

// If root is NULL, the list is empty
bool CTree::isEmpty()
{
	return root == NULL;
}

void CTree::Add()
{
	char aName[40];
	int aBribe;

	cout << "Enter the person's name: ";
	cin >> aName;
	cout << "Enter the person's contribution: ";
	cin >> aBribe;

	AddItem(root, aName, aBribe);
}


void CTree::AddItem( PersonRec*& ptr, char* name, int bribe)
{
	// ptr is NULL, create a new PersonRec structure, and have ptr point to it.
	if (!ptr)
		ptr = new PersonRec(name, bribe);

	else
	{
		// If bribe is greater than the bribe in the node pointed to by ptr, call AddItem recursively, 
		// with the first argument being the rChild of the node pointed to by ptr
		if ( bribe > ptr->bribe)
            AddItem(ptr->rChild, name, bribe);

		// Else if bribe is less than the bribe in the node pointed to by ptr, call AddItem recursively, 
		// with the first argument being the lChild of the node pointed to by ptr
		else if ( bribe < ptr->bribe)
            AddItem(ptr->lChild, name, bribe); 
		
		// Else, bribe must be equal to the bribe in the node pointed to by ptr, 
		// in which case insertion is not allowed, and the function just returns and ends.
		else
			return;
	}
}
void CTree::View()
{
	// If the list is not empty, call DisplayTree recursively
	if (isEmpty() != true)
	{
		cout << "\n# New Contribution\n";
		cout << "==============================\n\n";
		DisplayTree(root);
	}

	// The list is empty and cout
	else if (isEmpty() == true)
		cout << "\nList is empty\n";
}

// Since you want to display the nodes in order of the bribe amount, you should use inorder traversal.
void CTree::DisplayTree(PersonRec *ptr)
{
	static int x = 1;

	if (ptr == NULL)
		return;

	DisplayTree(ptr->rChild);
	cout << x << " " << ptr->name << " " << "$" << ptr->bribe << endl;
	x ++;
	DisplayTree(ptr->lChild);
}

/*
void CTree::destroySub(PersonRec* N)
{
	if(N)
	{
		destroySub(N->lChild);
		destroySub(N->rChild);
		delete N;
	}
	N = NULL;
}
*/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49


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

int displayMenu(void);
void processChoice(int, CTree&);

int main(void)
{
	int num;
	CTree ct;
	
	do
	{
		num = displayMenu();
		
		if (num != 3)
			processChoice(num, ct);
	} while (num != 3);

	system("pause");
	return 0;
}

int displayMenu(void)
{
	int choice;
	cout << "\nMenu\n";
	cout << "==============================\n\n";
	cout << "1. Add student to waiting list\n";
	cout << "2. View waiting list\n";
	cout << "3. Exit program\n\n";
	cout << "Please enter choice: ";
	cin >> choice;

	return choice;
}

void processChoice (int choice, CTree& myTree)
{
	switch (choice)
	{
	case 1: myTree.Add (); break;
	case 2: myTree.View (); break;
	}
}


Last edited on
If you define a destructor for PersonRec, all you will have to do in ~CTree() is delete root;.
Topic archived. No new replies allowed.