What is the inorder predecessor of 15 in given binary search tree?

SOLUTION

Concept –

The in-order sequence can be found following the chronology of Left-> Root-> Right.

Finding the in-order traversal sequence, we get 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20.

The element that comes after 15 is its successor. It can be seen that 15’s successor is 17.

Explanation –

  • In-order successor of a node is the minimum element in right subtree of the node in consideration.
  • Here, in the right subtree of 15, 17 is the element with minimum value. Hence, 17 is 15’s in-order successor.
  • Similarly, for finding the in-order predecessor of a node, the element with maximum value in left sub-tree is the answer. Here, 13 is 15’s in-order predecessor.


portant Point:

Tricks works only when the tree is Binary Search Tree.

Hello everyone, In this post, we will investigate how to solve the Predecessor And Successor In Binary Search Tree Geeksforgeeks programming puzzle by using the programming language.

// C++ program to find predecessor and successor in a BST #include <iostream> using namespace std;   // BST Node struct Node {     int key;     struct Node *left, *right; };   // This function finds predecessor and successor of key in BST. // It sets pre and suc as predecessor and successor respectively void findPreSuc(Node* root, Node*& pre, Node*& suc, int key) {     // Base case     if (root == NULL)  return ;       // If key is present at root     if (root->key == key)     {         // the maximum value in left subtree is predecessor         if (root->left != NULL)         {             Node* tmp = root->left;             while (tmp->right)                 tmp = tmp->right;             pre = tmp ;         }           // the minimum value in right subtree is successor         if (root->right != NULL)         {             Node* tmp = root->right ;             while (tmp->left)                 tmp = tmp->left ;             suc = tmp ;         }         return ;     }       // If key is smaller than root's key, go to left subtree     if (root->key > key)     {         suc = root ;         findPreSuc(root->left, pre, suc, key) ;     }     else // go to right subtree     {         pre = root ;         findPreSuc(root->right, pre, suc, key) ;     } }   // A utility function to create a new BST node Node *newNode(int item) {     Node *temp =  new Node;     temp->key = item;     temp->left = temp->right = NULL;     return temp; }   /* A utility function to insert a new node with given key in BST */ Node* insert(Node* node, int key) {     if (node == NULL) return newNode(key);     if (key < node->key)         node->left  = insert(node->left, key);     else         node->right = insert(node->right, key);     return node; }   // Driver program to test above function int main() {     int key = 65;    //Key to be searched in BST      /* Let us create following BST               50            /     \           30      70          /  \    /  \        20   40  60   80 */     Node *root = NULL;     root = insert(root, 50);     insert(root, 30);     insert(root, 20);     insert(root, 40);     insert(root, 70);     insert(root, 60);     insert(root, 80);         Node* pre = NULL, *suc = NULL;       findPreSuc(root, pre, suc, key);     if (pre != NULL)       cout << "Predecessor is " << pre->key << endl;     else       cout << "No Predecessor";       if (suc != NULL)       cout << "Successor is " << suc->key;     else       cout << "No Successor";     return 0; }

There are a variety of approaches that can be taken to solve the same problem Predecessor And Successor In Binary Search Tree Geeksforgeeks. The remaining options will be discussed further down.

Input: root node, key output: predecessor node, successor node 1. If root is NULL then return 2. if key is found then a. If its left subtree is not null Then predecessor will be the right most child of left subtree or left child itself. b. If its right subtree is not null The successor will be the left most child of right subtree or right child itself. return 3. If key is smaller then root node set the successor as root search recursively into left subtree else set the predecessor as root search recursively into right subtree

We have seen how to solve the Predecessor And Successor In Binary Search Tree Geeksforgeeks with various examples.

What is predecessor and successor in binary search tree?

What is Predecessor and Successor : When you do the inorder traversal of a binary tree, the neighbors of given node are called Predecessor(the node lies behind of given node) and Successor (the node lies ahead of given node).29-Dec-2014

How can you find successors and predecessors in a binary search tree in-order?

Root is the given key: In this case, if the left subtree is not NULL, then predecessor is the rightmost node in left subtree and if right subtree is not NULL, then successor is the leftmost node in right subtree. Root is greater than key: In this case, the key is present in left subtree of root.27-Jan-2022

What is successor in binary search tree?

In Binary Tree, Inorder successor of a node is the next node in Inorder traversal of the Binary Tree. Inorder Successor is NULL for the last node in Inorder traversal. In Binary Search Tree, Inorder Successor of an input node can also be defined as the node with the smallest key greater than the key of the input node.17-Jun-2022

What is successor node and predecessor node in BST?

A node's predecessor in a BST is the greatest value present in its left subtree. If the left subtree doesn't exist, then the predecessor can be one of his ancestors. Similarly, a node's successor in a BST is the smallest value present in its right subtree.21-Nov-2020

What is predecessor and successor?

Predecessor and Successor are the two terms in Mathematics that are most commonly used for the series and sequence of whole numbers. The predecessor is known as before numbers (that appear just before) and the successor is known as after numbers (that appear just after).

What is a predecessor node in a binary tree?

The predecessor node is the largest node that is smaller than the root (current node) – thus it is on the left branch of the Binary Search Tree, and the rightmost leaf (largest on the left branch).31-Aug-2020

What is the inorder predecessor of 15 in given binary search tree?

13

What is the successor method?

In mathematics, the successor function or successor operation sends a natural number to the next one. The successor function is denoted by S, so S(n) = n + 1. For example, S(1) = 2 and S(2) = 3. The successor function is one of the basic components used to build a primitive recursive function.

How do you get a successor?

The successor of a given number can be found by adding 1 to the given number. For example, the successor of 0 is 1, the successor of 1 is 2, the successor of 2 is 3, etc.

How do you find the successor of a node in a binary search tree without doing any inorder traversal?

Suppose the given node is x. Start traversing the tree from root node to find x recursively. If root == x, stop recursion otherwise find x recursively for left and right subtrees. Now after finding the node x, recursion will backtrack to the root.21-Jun-2022

What is inorder predecessor in binary tree?

What is an in-order predecessor in a binary search tree? The in-order predecessor of a node in a Binary Search Tree is the node that comes before our key node when we write the inorder traversal of the tree.

What is predecessor in binary search tree?

The predecessor of a node in BST is that node that will be visited just before the given node in the inorder traversal of the tree. If the given node is visited first in the inorder traversal, then its predecessor is NULL.

How do you find the inorder successor in a binary tree?

We need to take care of 3 cases for any node to find its inorder successor as described below: Right child of node is not NULL. If the right child of the node is not NULL then the inorder successor of this node will be the leftmost node in it's right subtree. Right Child of the node is NULL.

Which of the following is the inorder predecessor of node 1 in the given BST?

The inorder predecessor of a node p is the node q that comes just before p in the binary tree's inorder traversal.

Toplist

Última postagem

Tag