Complex Linked List ImplementationThe implementation we have learnt till now is Singly Linked List as it contains only link to a single successor. But there are also other complex linked list implementation in data structure. Show
Complex linked list implementation consists of three important implementations, i.e. the circularly linked list, the doubly linked list, and the multilinked list. We wil discuss each of them now. Let’s learn about the first type, i.e. Circularly Linked List. Circularly Linked ListIn a circularly linked list, the points of the last node is linked to the first node of the list. They are mainly used in such list that allows access to the middle nodes of the list without starting at the beginning. Doubly-linked list: Linked List Types in Data Structure
Inorder Successor in Binary Search TreeIn 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. So, it is sometimes important to find next node in sorted order. In the above diagram, inorder successor of 8 is 10, inorder successor of 10 is 12 and inorder successor of 14 is 20. Recommended: Please solve it on “PRACTICE” first, before moving on to the solution. Method 1 (Uses Parent Pointer) Input: node, root // node is the node whose Inorder successor is needed.
Implementation: C++
C
Java
Python3
C#
Javascript
Output
Inorder Successor of 14 is 20
Complexity Analysis:
Method 2 (Search from root) Input: node, root // node is the node whose Inorder successor is needed. Output: succ // succ is Inorder successor of node.
Below is the implementation of the above approach: C++
C
Java
Python3
C#
Javascript
Output
Inorder Successor of 14 is 20
Complexity Analysis:
Method 3 (Inorder traversal)An inorder transversal of BST produces a sorted sequence. Therefore, we perform an inorder traversal. The first encountered node with value greater than the node is the inorder successor. Input: node, root // node is the node whose ignorer successor is needed. Below is the implementation of the above approach: C++
Java
C#
Output
Inorder Successor of 14 is 20
Complexity Analysis: Time Complexity: O(h), where h is the height of the tree.In the worst case as explained above we travel the whole height of the tree &list=PLqM7alHXFySHCXD7r1J0ky9Zg_GBB1dbk References:
Article Tags :
Binary Search Tree
Amazon Morgan Stanley Practice Tags :
Morgan Stanley Amazon Binary Search Tree Linked List1234 |