
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Binary Search Tree insert with Parent Pointer in C++
We can insert new node into the BST in recursive manner. In that case we return the address of the root of each subtree. Here we will see another approach, where parent pointer will need to be maintained. Parent pointer will be helpful to find ancestor of a node etc.
The idea is to store the address of the left and right subtrees, we set parent pointers of the returned pointers after recursive call. This confirms that all parent pointers are set during insertion. The parent of root is set to null.
Algorithm
insert(node, key) −
begin if node is null, then create a new node and return if the key is less than the key of node, then create a new node with key add the new node with the left pointer or node else if key is greater or equal to the key of node, then create a new node with key add the new node at the right pointer of the node end if return node end
Example
#include<iostream> using namespace std; class Node { public: int data; Node *left, *right, *parent; }; struct Node *getNode(int item) { Node *temp = new Node; temp->data = item; temp->left = temp->right = temp->parent = NULL; return temp; } void inorderTraverse(struct Node *root) { if (root != NULL) { inorderTraverse(root->left); cout << root->data << " "; if (root->parent == NULL) cout << "NULL" << endl; else cout << root->parent->data << endl; inorderTraverse(root->right); } } struct Node* insert(struct Node* node, int key) { if (node == NULL) return getNode(key); if (key < node->data) { //to the left subtree Node *left_child = insert(node->left, key); node->left = left_child; left_child->parent = node; } else if (key > node->data) { // to the right subtree Node *right_child = insert(node->right, key); node->right = right_child; right_child->parent = node; } return node; } int main() { struct Node *root = NULL; root = insert(root, 100); insert(root, 60); insert(root, 40); insert(root, 80); insert(root, 140); insert(root, 120); insert(root, 160); inorderTraverse(root); }
Output
40 60 60 100 80 60 100 NULL 120 140 140 100 160 140
- Related Articles
- Insert into a Binary Search Tree in C++
- Binary Tree to Binary Search Tree Conversion in C++
- Binary Search Tree Iterator in C++
- Recover Binary Search Tree in C++
- Maximum parent children sum in Binary tree in C++
- Find right sibling of a binary tree with parent pointers in C++
- Binary Search Tree - Search and Insertion Operations in C++
- Binary Search Tree to Greater Sum Tree in C++
- Balance a Binary Search Tree in c++
- Binary Search Tree - Delete Operation in C++
- Binary Tree to Binary Search Tree Conversion using STL set C++?
- Difference between Binary Tree and Binary Search Tree
- Closest Binary Search Tree Value II in C++
- Binary Search Tree in Javascript
- Optimal Binary Search Tree

Advertisements