Binary Search Tree Function Help

I'm trying to write a code for a binary search tree and I currently have it written to traverse when I give it a pointer to the root to start the function on, however I need it to be able to start from the root without being passed a pointer as well as being able to pass it a pointer to any node and start a traversal from that point down throughout its leaves. Any help much appreciated, thanks!

EDIT: I have figured out how to do what I want it to, but now I am getting a segmentation fault before even reaching my first test case. Code has been edited to new version.

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
#include <iostream>

using namespace std;

typedef struct node
{
    int value;
    node * Left;
    node * Right;
    node(int val = 0)
    {
        value = val;
        Right = NULL;
        Left = NULL;
    }
}node;

class BST
{
	public:
	BST();
	BST(int *arr, int size);
	void insert(int val);
	void inOrderTraversal();
	void inOrderTraversal(node * Root);
	
	private:
	node * Root;
	
};

 
void BST::insert(int val)
{
    if(Root == NULL)
        Root = new node(val);
    else if((Root)->value <= val)
        insert(val);
    else if((Root)->value > val)
        insert(val);
}
 
BST::BST(int * arr, int size)
{
    node * Root = NULL;
    for(int i = 0; i < size; i++)
        insert(arr[i]);
}
 
void BST::inOrderTraversal()
{
	cout << "test" << endl;
	if (Root == NULL) return;
    if(Root && Root->Left)
        inOrderTraversal(Root->Left);
    if(Root)
        cout<<Root->value<<" ";
    if(Root && Root->Right)
        inOrderTraversal(Root->Right);
 
}

void BST::inOrderTraversal(node * Root)
{
	cout << "test2" << endl;
    if(Root && Root->Left)
        inOrderTraversal(Root->Left);
    if(Root)
        cout<<Root->value<<" ";
    if(Root && Root->Right)
        inOrderTraversal(Root->Right);
 
}
int main()
{
    int arr[8] = {10,5,15,5,6,7,8,89};
    BST T(arr, 8);
	T.inOrderTraversal();
    cout<<endl;
    return 0;
}
Last edited on
Anyone have any ideas? Anything is much appreciated.
line 45 - I think you want to update the Root pointer that's defined on line 28 Root = NULL; instead of creating a local pointer in the function node * Root = NULL;.

Insert function - if you're not initializing the root, you're recursively calling the insert function. I think this is where your segmentation fault is coming from because the recursion is infinite.

If you're insert a node that's not the root, instead of calling insert again, you need to be creating that node and updating the appropriate left and right pointers. I tweaked the code to create a node instead of calling insert again, and added some output. That got rid of the seg fault (at least for the creation of the BST).

1
2
3
4
5
6
7
8
root is now 10
5 is < than 10
15 is >= than 10
5 is < than 10
6 is < than 10
7 is < than 10
8 is < than 10
89 is >= than 10
Topic archived. No new replies allowed.