Siblings of a BST Node Algorithm?

I'm working on an implementation of a Binary Search Tree and I'm having trouble in printing the sibling of a node in BST


Can anyone guide me towards the correct algorithm for this?

Here's 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <iostream>
#include <stdlib.h>


using namespace std;

struct BST
{
	int data;
	BST *left, *right;

	BST* CreateNode(int dat)
	{
		BST *temp = new BST();
		temp->data = dat;
		temp->left = temp->right = NULL;
		return temp;
	}

	void inorder(BST *root)
	{
		if (root == NULL)
		{
			return;
		}
			inorder(root->left);
			cout << root->data << " ";
			inorder(root->right);
		
	}


	BST* insert(BST* ptr, int dat)
	{
		if (ptr == NULL){
			return (CreateNode(dat));
		}

		if (dat < ptr->data)
			ptr->left = insert(ptr->left, dat);
		else if (dat > ptr->data)
			ptr->right = insert(ptr->right, dat);

		return ptr;
	}
	int leaf(BST *Ptr) {
		if (Ptr == NULL) {
			return 0;
		}
		if (Ptr->left == NULL && Ptr->right == NULL) {
			return 1;
		}
		else {
			return (leaf(Ptr->left) + leaf(Ptr->right));
		}
	}

	BST* Search(BST *Ptr, int dat) {
		if (Ptr == NULL) {
			return NULL;
		}
		if (Ptr->data == dat) {
			cout << "Found Node\n";
			return Ptr;
		}
		if (dat < Ptr->data) {
			Ptr->left = Search(Ptr->left, dat);
		}
		if (dat > Ptr->data) {
			Ptr->right = Search(Ptr->right, dat);
		}
		return Ptr;
	}
	BST* MinNode(BST *Ptr) {    //For Deleting
		while (Ptr->left != NULL) {
			Ptr = Ptr->left;
		}
		cout << "Min Node: " << Ptr->data << endl;
		return Ptr;
	}
	BST* Delete(BST *Ptr, int dat) {
		//Base Cases
		if (Ptr == NULL) {
			cout << "Value Doesn't Exist in Tree Can't Delete\n";
			return Ptr;
		}
		if (Ptr->data == dat) {
			cout << "Found Node Deleting....\n";
			//No Child Case
			if (Ptr->left == NULL && Ptr->right == NULL) {
				delete Ptr;
				cout << "Node With No Child Deleted...\n";
				return NULL;
			}
			//One Child Case
			if (Ptr->right==NULL) {
				BST *tmpnode;
				tmpnode = Ptr->left;
				delete Ptr;
				cout << "Node with One child Deleted\n";
				return tmpnode;
			}
			if (Ptr->left==NULL) {
				BST *tmpnode;
				tmpnode = Ptr->right;
				delete Ptr;
				cout << "Node with One child Deleted\n";
				return tmpnode;
			}
			//Two Child Case
			if (Ptr->left && Ptr->right) { //Both Not NULL or both have values associated with them
				BST *tmpnode = NULL;
				tmpnode = MinNode(Ptr->right);
				Ptr->data = tmpnode->data;
				Ptr->right = Delete(Ptr->right, tmpnode->data);
				cout << "Node with 2 children deleted.....\n";
				return Ptr;
			}
		}
		//Recursive Cases
		if (dat < Ptr->data) {
			Ptr->left = Delete(Ptr->left, dat);
		}
		if (dat>Ptr->data) {
			Ptr->right = Delete(Ptr->right, dat);
		}
		return Ptr;
	}
	bool PrintAncestors(BST *ptr,int dat) {
	   //Base Cases
		if (ptr == NULL) {
			return false;
		}
		if (ptr->data == dat) {
			return true;
		}
		//Recursive Cases
		if (PrintAncestors(ptr->left, dat) || PrintAncestors(ptr->right, dat)) {
			cout << ptr->data << " ";
			return true;
		}
		return false;
	}
};




int main()
{
	BST Obj;
	BST *root = NULL;
	root = Obj.insert(root, 50);
	Obj.insert(root, 25);
	Obj.insert(root, 55);
	Obj.insert(root, 54);
	Obj.insert(root, 57);
	Obj.insert(root, 26);
	Obj.inorder(root);
	cout << endl;
	cout << Obj.leaf(root) << endl;
	Obj.Search(root, 57);
	cout << endl;
	Obj.MinNode(root);
 	cout << endl;
        Obj.Delete(root, 55);
	cout << endl;
	Obj.inorder(root);
	cout << endl;
	cout << "Ancestors: ";
	Obj.PrintAncestors(root, 54);
	cout << endl;
	Obj.PrintSiblings(root,54);
	system("PAUSE");
	return 0;
}
Last edited on
It's a lot like search. The function returns true when it finds the value. When the caller sees a true return, he prints the sibling. The only tricky part is that the caller then returns false. In other words, the function only returns true when it finds the node with dat.

Here is the code. I have also changed inorder to print the structure of the tree so you can verify it.
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <iostream>
#include <stdlib.h>


using namespace std;

struct BST
{
	int data;
	BST *left, *right;

	BST* CreateNode(int dat)
	{
		BST *temp = new BST();
		temp->data = dat;
		temp->left = temp->right = NULL;
		return temp;
	}

    void inorder(BST *root, int depth=0)
	{
	    if (root == NULL)
		{
		    return;
		}
	    inorder(root->left, depth+1);
	    for (int i=0; i<depth;++i) cout << '\t';
	    cout << root->data << '\n';
	    inorder(root->right, depth+1);
		
	}


	BST* insert(BST* ptr, int dat)
	{
		if (ptr == NULL){
			return (CreateNode(dat));
		}

		if (dat < ptr->data)
			ptr->left = insert(ptr->left, dat);
		else if (dat > ptr->data)
			ptr->right = insert(ptr->right, dat);

		return ptr;
	}
	int leaf(BST *Ptr) {
		if (Ptr == NULL) {
			return 0;
		}
		if (Ptr->left == NULL && Ptr->right == NULL) {
			return 1;
		}
		else {
			return (leaf(Ptr->left) + leaf(Ptr->right));
		}
	}

	BST* Search(BST *Ptr, int dat) {
		if (Ptr == NULL) {
			return NULL;
		}
		if (Ptr->data == dat) {
			cout << "Found Node\n";
			return Ptr;
		}
		if (dat < Ptr->data) {
			Ptr->left = Search(Ptr->left, dat);
		}
		if (dat > Ptr->data) {
			Ptr->right = Search(Ptr->right, dat);
		}
		return Ptr;
	}
	BST* MinNode(BST *Ptr) {    //For Deleting
		while (Ptr->left != NULL) {
			Ptr = Ptr->left;
		}
		cout << "Min Node: " << Ptr->data << endl;
		return Ptr;
	}
	BST* Delete(BST *Ptr, int dat) {
		//Base Cases
		if (Ptr == NULL) {
			cout << "Value Doesn't Exist in Tree Can't Delete\n";
			return Ptr;
		}
		if (Ptr->data == dat) {
			cout << "Found Node Deleting....\n";
			//No Child Case
			if (Ptr->left == NULL && Ptr->right == NULL) {
				delete Ptr;
				cout << "Node With No Child Deleted...\n";
				return NULL;
			}
			//One Child Case
			if (Ptr->right==NULL) {
				BST *tmpnode;
				tmpnode = Ptr->left;
				delete Ptr;
				cout << "Node with One child Deleted\n";
				return tmpnode;
			}
			if (Ptr->left==NULL) {
				BST *tmpnode;
				tmpnode = Ptr->right;
				delete Ptr;
				cout << "Node with One child Deleted\n";
				return tmpnode;
			}
			//Two Child Case
			if (Ptr->left && Ptr->right) { //Both Not NULL or both have values associated with them
				BST *tmpnode = NULL;
				tmpnode = MinNode(Ptr->right);
				Ptr->data = tmpnode->data;
				Ptr->right = Delete(Ptr->right, tmpnode->data);
				cout << "Node with 2 children deleted.....\n";
				return Ptr;
			}
		}
		//Recursive Cases
		if (dat < Ptr->data) {
			Ptr->left = Delete(Ptr->left, dat);
		}
		if (dat>Ptr->data) {
			Ptr->right = Delete(Ptr->right, dat);
		}
		return Ptr;
	}
	bool PrintAncestors(BST *ptr,int dat) {
	   //Base Cases
		if (ptr == NULL) {
			return false;
		}
		if (ptr->data == dat) {
			return true;
		}
		//Recursive Cases
		if (PrintAncestors(ptr->left, dat) || PrintAncestors(ptr->right, dat)) {
			cout << ptr->data << " ";
			return true;
		}
		return false;
	}
	bool PrintSiblings(BST *Ptr, int dat) {
		if (Ptr == NULL) {
			return false;
		}
		if (Ptr->data == dat) {
		    return true; // only return true when you find it
		}
		if (dat < Ptr->data) {
		    if(PrintSiblings(Ptr->left, dat) && Ptr->right) {
			cout << "Sibling of " << dat << ": "
			     << Ptr->right-> data << '\n';
		    }
		} else {
		    if (PrintSiblings(Ptr->right, dat) && Ptr->left) {
			cout << "Sibling of " << dat << ": "
			     << Ptr->left->data << '\n';
		    }
		}
		return false;
	}
};




int main()
{
	BST Obj;
	BST *root = NULL;
	root = Obj.insert(root, 50);
	Obj.insert(root, 25);
	Obj.insert(root, 55);
	Obj.insert(root, 54);
	Obj.insert(root, 57);
	Obj.insert(root, 26);
	Obj.inorder(root);
	cout << endl;
	cout << Obj.leaf(root) << endl;
	Obj.Search(root, 57);
	cout << endl;
	Obj.MinNode(root);
 	cout << endl;
        Obj.Delete(root, 55);
	cout << endl;
	Obj.inorder(root);
	cout << endl;
	cout << "Ancestors: ";
	Obj.PrintAncestors(root, 54);
	cout << endl;
	Obj.PrintSiblings(root,54);
	Obj.PrintSiblings(root,25);
	system("PAUSE");
	return 0;
}


Registered users can post here. Sign in or register to post.