Help with BSTs

I did something wrong here, I suspect its in the insert function, but I can't seem to fix it, am I doing something wrong? I need to do the insert function nonrecursively and the rest recursively

edit: now I'm 100% sure that it is the insert function that is crashing it.

edit: fixed;

header file
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
#ifndef BST_H
#define BST_H

#include <string>		
#include <iostream>
using namespace std;

template<class T>
class Node
{
public:
	Node(){}
    Node(T theData, Node* theRLink, Node* theLLink) 
		: data(theData), rlink(theRLink), llink(theLLink){}
    Node* getRLink( ) const { return rlink; }
	Node* getLLink( ) const { return llink; }
    T getData( ) const { return data; }
    void setRLink(Node *theRLink) { rlink = theRLink; }
	void setLLink(Node *theLLink) { llink = theLLink; }
	void setData(const T& theData) { data = theData; }
    
private:
    T data;
    Node *rlink, *llink;
};

template<class T>
class BST
{
public:

	BST();
		//Default constructor

	~BST();   
		//Destructor  

    void destroyTree();
		//Deallocates the memory space occupied by the BST

	//Function: insert
		//Inserts a given item in the BST (non-recursive)    
	//Your code here...
	void insert(const T&);
		//Inserts a given item in the BST (non-recursive)
	
	void inorderTraversal() const;
		//Prints nodes of the BT in the inorder sequence
			 
	int treeHeight() const;
		//Returns the height of the BST

	int totalNodes() const;
		//Determines the number of nodes in the BST
	
private:	
	Node<T> *root; //Pointer to the root

	void destroy(Node<T> *p);
		//Destroy the BST to which p points

    //Function: inorder
		//Prints the inorder traversal of the BT to which p points
	//Your code here...
	void inorder(Node<T> *p) const;

	//Function height
		//Returns the height of the tree
	//Your code here...
	int height(Node<T> *p) const;

	//Function nodeCount
		//Returns the number of nodes in the BST to which p points
	//Your code here...
	int nodeCount(Node<T> *p) const;
};

#endif 



CPP File
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "BST.h"

	//default constructor
template<class T>
BST<T>::BST()
{
	root = NULL;
}

	//destructor
template <class T>
BST<T>::~BST()
{
	destroy(root);
}

	//destroyTree
template <class T>
void  BST<T>::destroyTree()
{
	destroy(root);
}

	//Function insert (non-recursive)
//Your code here...
template <class T>
void BST<T>::insert(const T& input)
{
	Node<T> *subRoot = NULL;
	Node<T> *leaf = root;

	while(leaf != NULL)
	{
		subRoot = leaf;
		if(input <= leaf->getData())
			leaf = leaf->getLLink();
		else
			leaf = leaf->getRLink();
	}

	if (input == leaf->getData())
	{
		cerr << "The item to insert is already in the list – duplicates are not allowed.\n";
	}

	else if (subRoot == NULL)
	{
		leaf = new Node<T>;
		leaf->setData(input);
	}

	else if(input < subRoot->getData())
	{
		leaf = new Node<T>;
		leaf->setData(input);

		subRoot->setLLink(leaf);
	}

	else if (input > root->getData())
	{
		leaf = new Node<T>;
		leaf->setData(input);

		subRoot->setRLink(leaf);
	}
}


	//inorderTraversal
template<class T>
void BST<T>::inorderTraversal() const
{
	if (root == NULL)
		cerr << "There is no tree.";
	else
	{
		inorder(root);
	}
}

	//treeHeight
template<class T>
int BST<T>::treeHeight() const
{
	return height(root);
}

	//totalNodes
template<class T>
int BST<T>::totalNodes() const
{
	return nodeCount(root);
}

	//destroy
template <class T>
void  BST<T>::destroy(Node<T> *p)
{
	if(p != NULL)
	{
		destroy(p->getLLink());
		destroy(p->getRLink());
		delete p;
		p = NULL;
	}

	root = NULL;
}

	//Function inorder
//Your code here...
template <class T>
void BST<T>::inorder(Node<T> *p) const
{
	inorder(p->getLLink());
	cout << p->getData() << " ";
	inorder(p->getRLink());
}

	//Function height
//Your code here...
template <class T>
int BST<T>::height(Node<T> *p) const
{
	if(p == NULL)
		return -1;

	int left = height(p->getLLink());
	int right = height(p->getRLink());

	if(left > right)
		return left+1;
	else
		return right+1;
}

	//Function nodeCount
//Your code here...
template <class T>
int BST<T>::nodeCount(Node<T> *p) const
{
	if (p == NULL)
		return 0;
	return 1 + nodeCount(p->getLLink()) + nodeCount(p->getRLink());
}
Last edited on
Tried a different approach, it still crashes.

edit: insert seems to work now, but not how it's suppose to, I think my inOrder function is incorrect also.

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
//Function insert (non-recursive)
template <class T>
void BST<T>::insert(const T& input)
{
	bool flag = false;

	if (root == NULL)
	{
		Node<T> *newNode;
		newNode = new Node<T>;

		newNode->setData(input);
		newNode->setLLink(NULL);
		newNode->setRLink(NULL);

		root = newNode;
		cout << "Root: " << root->getData() << endl;
		flag = true;
	}
	
	Node<T> *subRoot;
	subRoot = root;

	Node<T> *newNode;
	newNode = new Node<T>;

	newNode->setData(input);
	newNode->setLLink(NULL);
	newNode->setRLink(NULL);

	while (!flag && subRoot != NULL)
	{
		//remove
	cout << "Inserting: " << input << endl;

		if (input == subRoot->getData()) //check for duplicates
		{
			cerr << "The item to insert is already in the list - duplicates are not allowed.\n";
			flag = true;
		}

		else if (input < subRoot->getData()) //insert left
		{
			if (subRoot->getLLink() == NULL)
			{
				subRoot->setLLink(newNode);
				flag = true;
			}
			else
				subRoot = subRoot->getLLink();
		}

		else //insert right
		{
			if (subRoot->getRLink() == NULL)
			{
				subRoot->setRLink(newNode);
				flag = true;
			}
			else
				subRoot = subRoot->getRLink();
		}

	}
}
Last edited on
Topic archived. No new replies allowed.