xxxxxxxxxx
// by mohanad moumen
#include <iostream>
// Defining a class Node to represent a node in a binary search tree
class Node{
public:
int data;
Node *left,*right;
Node(int value){
data = value;
left = right = NULL;
}
};
// Defining a class BST to represent a binary search tree
class BST{
public:
Node* root;
// Constructor to initialize an empty binary search tree
BST(){
root=NULL;
}
// Function to insert a node with value v into the binary search tree rooted at r
Node* insert(Node* r,int v){
if(r == NULL){
// If the root is NULL, create a new node with value v and make it the root
Node* newNode = new Node(v);
r = newNode;
}
else if(v < r->data)
// If v is less than the data in the current node, recursively insert v in the left subtree
r->left = insert(r->left, v);
else
// If v is greater than or equal to the data in the current node, recursively insert v in the right subtree
r->right = insert(r->right, v);
return r;
}
// Function to insert a node with value v into the binary search tree
void insert(int v){
// Call the insert function with the root of the tree and value v
root = insert(root, v);
}
// Function to traverse the binary search tree in pre-order (NLR) and print the nodes
void preOrder(Node *r){
if(r==NULL)
return;
// Print the data in the current node
std::cout<< r->data <<"\t";
// Recursively traverse the left subtree
preOrder(r->left);
// Recursively traverse the right subtree
preOrder(r->right);
}
// Function to traverse the binary search tree in in-order (LNR) and print the nodes
void inOrder(Node *r){
if(r==NULL)
return;
// Recursively traverse the left subtree
inOrder(r->left);
// Print the data in the current node
std::cout<<r->data<<"\t";
// Recursively traverse the right subtree
inOrder(r->right);
}
// Function to traverse the binary search tree in post-order (LRN) and print the nodes
void postOrder(Node *r){
if(r==NULL)
return;
// Recursively traverse the left subtree
postOrder(r->left);
// Recursively traverse the right subtree
postOrder(r->right);
// Print the data in the current node
std::cout<<r->data<<"\t";
}
};
int main() {
// Create an instance of the BST class
BST tree;
// Insert nodes with values into the binary search tree
tree.insert(45);
tree.insert(15);
tree.insert(79);
tree.insert(90);
tree.insert(10);
tree.insert(55);
tree.insert(12);
tree.insert(20);
tree.insert(50);
// Print the nodes in the binary search tree in pre-order (NLR)
std::cout<<"\npre-order!! \n";
tree.preOrder(tree.root);
// Print the nodes in the binary search tree in in-order (LNR)
std::cout<<"\n\nin-order!! \n";
tree.inOrder(tree.root);
// Print the nodes in the binary search tree in post-order (LRN)
std::cout<<"\n\npost-order!! \n";
tree.postOrder(tree.root);
return 0;
}
xxxxxxxxxx
// Binary Search Tree operations in C++
#include <iostream>
using namespace std;
struct node {
int key;
struct node *left, *right;
};
// Create a node
struct node *newNode(int item) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
// Inorder Traversal
void inorder(struct node *root) {
if (root != NULL) {
// Traverse left
inorder(root->left);
// Traverse root
cout << root->key << " -> ";
// Traverse right
inorder(root->right);
}
}
// Insert a node
struct node *insert(struct node *node, int key) {
// Return a new node if the tree is empty
if (node == NULL) return newNode(key);
// Traverse to the right place and insert the node
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
// Find the inorder successor
struct node *minValueNode(struct node *node) {
struct node *current = node;
// Find the leftmost leaf
while (current && current->left != NULL)
current = current->left;
return current;
}
// Deleting a node
struct node *deleteNode(struct node *root, int key) {
// Return if the tree is empty
if (root == NULL) return root;
// Find the node to be deleted
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else {
// If the node is with only one child or no child
if (root->left == NULL) {
struct node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct node *temp = root->left;
free(root);
return temp;
}
// If the node has two children
struct node *temp = minValueNode(root->right);
// Place the inorder successor in position of the node to be deleted
root->key = temp->key;
// Delete the inorder successor
root->right = deleteNode(root->right, temp->key);
}
return root;
}
// Driver code
int main() {
struct node *root = NULL;
root = insert(root, 8);
root = insert(root, 3);
root = insert(root, 1);
root = insert(root, 6);
root = insert(root, 7);
root = insert(root, 10);
root = insert(root, 14);
root = insert(root, 4);
cout << "Inorder traversal: ";
inorder(root);
cout << "\nAfter deleting 10\n";
root = deleteNode(root, 10);
cout << "Inorder traversal: ";
inorder(root);
}