I have a program which is using for-loop to print all the elements in the BST in-order. I know that in order to do so, I need to print the left-Node, Parent Node, and then the right node. I can make such programs work when there is no for-loop involved. However, I am failing here to navigate backwards properly. In findNext()
my logic is incorrect, because it prevents me from printing the complete tree. For example, if I have the following tree,
101
/ \
51 161
/ \ / \
31 91 141 171
Then my output would look something like the following (when I run the code which I pasted below).
31
51
101
161
171
In the output above, I am missing the data for 91 and 141. I know why I am missing the data in these nodes ( because of findNext()
) , but I can't figure out how to fix this issue using my logic. I am pasting the code below. I know I can probably use queues to solve this issue, but I think that's unnecessary because then I overcomplicating a simple problem.
include <stdio.h>
#include <stdlib.h>
#include <cstdio>
struct node {
int value;
node * left;
node * right;
node * parent;//Since we are given Parent node, we can keep track of root (previous) node.
node( int value, node * left, node * right, node * parent=NULL ) :
value( value ), left( left ), right( right ), parent( parent ) {}
};
// Find the minimum element in the BST. This function runs as expected.
// It only runs one time to find 31 (the lowest element in BST)
struct node * findMin( struct node *n ){
if (n == NULL)
return NULL;
if (n->left == NULL)
return n;
return findMin(n->left);
}
//This function is supposed to provide the caller with the next available node to print.
//We are trying to print the elements in-order, so findNext() should first return 51, followed
//by 91, then followed by 101 etc. However, it is not returning 91. It returns 51, 101, 161 etc.
struct node * findNext( struct node *n ) {
if (n == NULL)
return NULL;
if (n->parent)
{
// Logic: Return the parent node if we are in the left node. We know we are in the left node
// because n->parent->left == n.
if (n->parent->left == n)
return n->parent;
// Logic: Return the right node if we are in the right node. This is wrong. I am lost after this point.
// I wanna be able to tell that I am in the Parent node or the right node.
// If I am in the parent node, then I wanna return the right node. If I am in
// right node, then I wanna return parent's parent.
if (n->parent->right == n)
return n->right;
else
return n->left;
}
else
{
return n->right;
} // I know how BST works, but I honestly can't figure out how to automate this without queues.
}
// This function can be ignored, only part of setup process.
void connectparent( node * n, node * parent ) {
if( n ) {
n->parent = parent;
connectparent( n->left, n );
connectparent( n->right, n );
}
}
// This function can be ignored, only part of setup process.
node * constructtree() {
node * root = new node(101,
new node( 51, new node( 31, NULL, NULL ), new node( 91, NULL, NULL ) ),
new node( 161, new node( 141, NULL, NULL ),new node( 171, NULL, NULL ) ) );
connectparent( root, NULL );
return root;
}
int main() {
node * root = constructtree();
for( node * n = findMin( root ); n; n = findNext( n ) ) {
printf( "%d \n", n->value );
}
return 0;
}