JIYIK CN >

Current Location:Home > Learning > ALGORITHM >

In-order descendants in a binary search tree

Author:JIYIK Last Updated:2025/03/18 Views:

The in-order descendant of a binary tree is the next node in the in-order traversal of the binary tree. So, for the last node in the tree, it is NULL. Since the in-order traversal of a binary search tree is a sorted array. The node with the smallest key greater than a given node is defined as its in-order descendant. In a BST, there are two possibilities for an in-order descendant, namely, the node with the smallest value in the right subtree or ancestor of the node. Otherwise, the in-order descendant of the node does not exist.

In-order descendant algorithm in BST algorithm

  • If root= NULL, then succset to NULLand return .
  • If root->data< current->data, then succis current, currentand is current->left.
  • If root->data> current->data, then currentis current->right.
  • If root->data== current->dataand root->right!= NULL, succ= minimum(current->right).
  • return succ.

Diagram of in-order descendants in a BST

Binary Search Tree

3The in-order descendants of are 4, because 3has a right node that is the smallest node 4in the right subtree that is larger than .3

4The sequential descendants of are 5, since 4has no right node, we need to look at its ancestors, where 5is 4the smallest node larger than .

Implementation of BST in-order descendants

#include <iostream>
using namespace std;

class Node {
public:
    int data;
    Node *left, *right;
    Node(int x) {
        this->data = x;
        this->left = this->right = NULL;
    }
};

Node* insert(Node* root, int key)
{
    if (root == NULL) {
        return new Node(key);
    }
    if (key < root->data) {
        root->left = insert(root->left, key);
    }
    else {
        root->right = insert(root->right, key);
    }
    return root;
}

Node* getNextleft(Node* root)
{
    while (root->left) {
        root = root->left;
    }

    return root;
}

void inorderSuccessor(Node* root, Node*& succ, int key)
{

    if (root == NULL) {
        succ = NULL;
        return;
    }

    if (root->data == key)
    {
        if (root->right) {
            succ = getNextleft(root->right);
        }
    }

    else if (key < root->data)
    {
        succ = root;
        inorderSuccessor(root->left, succ, key);
    }
    else {
        inorderSuccessor(root->right, succ, key);
    }
}

int main()
{
    int keys[] = { 1, 5, 8, 2, 6, 3, 7, 4 };
    Node* root = NULL;
    for (int key : keys) {
        root = insert(root, key);
    }
    for (int key : keys)
    {
        Node* prec = NULL;
        inorderSuccessor(root, prec, key);
        if (prec) {
            cout << "Inorder successor of node " << key << " is " << prec->data;
        }
        else {
            cout << "No inorder Successor of node " << key;
        }

        cout << '\n';
    }

    return 0;
}

Algorithmic complexity of finding in-order descendants in BST

Time Complexity

  • Average situation

On average, the time complexity of finding an in-order descendant in a BST is comparable to the height of the binary search tree. On average, the height of a BST is O(logn). This occurs when the resulting BST is a balanced BST. Therefore, the time complexity is [Big Theta]: O(logn).

  • Best Case

The best case is when the tree is a balanced BST. In the best case, the time complexity of deletion is O(logn). It is the same as the time complexity of the average case.

  • Worst case scenario

In the worst case, we may need to go from the root node to the deepest leaf node, which is the entire height of the tree h. If the tree is unbalanced, i.e. it is skewed, the height of the tree may become n, so the worst case time complexity of insertion and search operations is O(n).

Space complexity

Due to the additional space required for the recursive calls, the space complexity of this algorithm is O(h).

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Convert a binary tree to a binary search tree

Publish Date:2025/03/18 Views:52 Category:ALGORITHM

A binary tree is a non-linear data structure. It is called a binary tree because each node has at most two children. These children are called left and right children. It can also be interpreted as an undirected graph where the topmost node

Binary Search Tree Deletion

Publish Date:2025/03/18 Views:93 Category:ALGORITHM

In the article Binary Search Trees: Searching and Inserting , we discussed how to insert an element into a binary search tree and how to search for a value in a binary search tree. In this article, we will discuss how to delete a node from

Binary Search Tree Check

Publish Date:2025/03/18 Views:79 Category:ALGORITHM

A binary tree is a non-linear data structure. It is called a binary tree because each node has at most two children. These children are called left children and right children. For a binary tree to be a BST, it must satisfy the following pr

Iterative insertion into a binary search tree

Publish Date:2025/03/18 Views:158 Category:ALGORITHM

In the previous article Binary Search Tree , we discussed the recursive method to insert a node in BST. In this article, we will discuss the iterative method to insert a node in BST. It is better than the recursive method because the iterat

Binary Search Tree

Publish Date:2025/03/18 Views:181 Category:ALGORITHM

A binary search tree (BST) is an ordered binary tree data structure based on nodes. A node has a value and two child nodes (a binary tree has at most two child nodes) connected to each other. A node has a value and two child nodes (a binary

Binary Tree Traversal

Publish Date:2025/03/18 Views:115 Category:ALGORITHM

A binary tree is a non-linear data structure. It is called a binary tree because each node has at most two children. These children are called left and right children. It can also be interpreted as an undirected graph where the topmost node

Circular Doubly Linked List

Publish Date:2025/03/18 Views:67 Category:ALGORITHM

A circular doubly linked list is a combination of a circular linked list and a doubly linked list. Its two nodes are connected by a previous and next pointer. The next pointer of the last node points to the first node, and the previous poin

Circular Linked List

Publish Date:2025/03/18 Views:172 Category:ALGORITHM

A circular linked list is a data structure that is slightly more complex than a linked list. It is a linked list where all the nodes are connected in a loop and form a chain. The last node does not have a NULL . Instead, it stores the addre

Doubly Linked List

Publish Date:2025/03/18 Views:69 Category:ALGORITHM

Doubly linked list is a linear data structure. It is a collection of objects defined as nodes. But unlike a linked list, the node has two pointers, one is the previous pointer and the other is the next pointer. Just like the linked list nod

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial