Binary Search Tree

Write your question here.
Hey guys i was making a BST in c++. It works fine but it just gave me exception when calling Display method. It does not show any element ... what could be the problem? please help



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
  class BST{
	Node *root;
	void traverse( Node *r )
	{
		if (r != NULL)
		{
			traverse(r->left);
			cout << r->data << endl;
			traverse(r->right);
		}
		
	}

public:
	BST(){
		root = NULL;
	}

	//Insert data into the node
	void insert(int data)
	{
		Node *temp = new Node(data);
		Node *t = root;
		if (root == NULL)
		{
			root = temp;
		}
		else if (data < root->data){
			while (t->left != nullptr){
				t = t->left;
			}
			t->left = temp;
		}

		else{
			while (t->right != nullptr){
				t = t->right;
			}
			t->right = temp;
		}
	}

	bool search(int data)
	{
		Node *t = root;
		bool find = false;
		if (root->data == data){
			find = true;
		}

		else if (data < root->data)
		{
			while (t->left != nullptr)
			{
				t = t->left;
				if (t->data == data){
					find = true;
					break;
				}
			}
			
		}

		else{
			while (t->right != nullptr)
			{
				t = t->right;
				if (t->data == data){
					find = true;
					break;
				}
			}
			
		}
		delete t;
		return find;
		
	}

	//Function to show elements
	void Display()
	{
		Node* t = root;
		traverse(t);

	}
};



Topic archived. No new replies allowed.