Skip to content

Commit

Permalink
Find right most and left most node of BST
Browse files Browse the repository at this point in the history
  • Loading branch information
jainkhere committed May 5, 2022
1 parent 7a63aac commit a5e69ec
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions binary_search_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,93 @@ void add_value_to_tree(NODE ** addr_of_root, int character) {
}
}

// Function to know if node is right most node or not
// Return 1 if node is right most node
// Return 0 otherwise
int is_right_most_node(NODE * node) {
// Keep going up following right-child
// if we can reach the root, then node is right most node
// if we cannot reach the root, it is not the right most node

// return 0 if node is null
if (node == NULL)
return 0;

// return 0 if right child of node is not null
if ((*node).right != NULL)
return 0;


while(1) {
NODE * parent = (*node).parent;
if (parent == NULL) {
// we have reached the root
return 1;
}
if ((*parent).right == node)
node = (*node).parent;
// if node is not right child then it is left child,
// and therefore it cannot be right most node
else
return 0;
}
}

// Function to get right most node
// In binary search tree, right most node is
// same as max value in the tree
NODE * get_right_most_node(NODE * root) {
// return if root is null
if (root == NULL) {
return NULL;
}

// create a temp node. initialize it with root
// keep on traversing temp.right until temp.right is null
// you will reach right most node
NODE * temp = root;
while((*temp).right != NULL) {
temp = (*temp).right;
}
return temp;
}

// Function to get left most node
// In binary search tree, left most node is
// same as min value in the tree
NODE * get_left_most_node(NODE * root) {
// return if root is null
if (root == NULL) {
return NULL;
}

// create a temp node. initialize it with root
// keep on traversing temp.left until temp.left is null
// you will reach left most node
NODE * temp = root;
while((*temp).left != NULL) {
temp = (*temp).left;
}
return temp;
}

void print_tree_increasing_order (NODE * root) {
if (root == NULL)
return;

// print all values on left subtree
print_tree_increasing_order((*root).left);

// print root
if (is_right_most_node(root) == 1)
printf("%d", (*root).value);
else
printf("%d, ", (*root).value);

// print all values on right subtree
print_tree_increasing_order((*root).right);
}

// Inorder traverdal of tree
void print_in_order (NODE * node) {
// In order traversal - Left Root Right
Expand All @@ -91,12 +178,17 @@ void print_in_order (NODE * node) {
int main(void) {
NODE * root = NULL;

// stdin: 1 10 3 2 20 30 4 18 11

int character;
while((scanf("%d", &character)) != EOF) {
// add value to tree
add_value_to_tree(&root, character);
// printf("%d\n", (*root).value);
}
print_in_order(root);
printf("\n");
print_tree_increasing_order(root);

return 0;
}

0 comments on commit a5e69ec

Please sign in to comment.