0% found this document useful (0 votes)
18 views

Using Namespace Class Struct: #Include #Include #Include #Include

This document describes a program to implement a binary search tree (BST) data structure in C++. It includes functions for common BST operations like insertion, deletion, searching, and traversing the tree using various orders (inorder, preorder, postorder, level order). Traversal can be done recursively or iteratively using stacks. Additional functions calculate the tree height, find the minimum/maximum nodes, and construct BSTs from preorder/inorder or postorder/inorder traversals. The removal operation handles cases for nodes with 0, 1, or 2 children.

Uploaded by

DeepakVerma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Using Namespace Class Struct: #Include #Include #Include #Include

This document describes a program to implement a binary search tree (BST) data structure in C++. It includes functions for common BST operations like insertion, deletion, searching, and traversing the tree using various orders (inorder, preorder, postorder, level order). Traversal can be done recursively or iteratively using stacks. Additional functions calculate the tree height, find the minimum/maximum nodes, and construct BSTs from preorder/inorder or postorder/inorder traversals. The removal operation handles cases for nodes with 0, 1, or 2 children.

Uploaded by

DeepakVerma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

01/12/2016

untitled.html

/*
Program to implement a Binary Search Tree. This does not considers duplicate elements.
Construction from level and inorder is wrong.
*/
#include <iostream>
#include <string>
#include <queue>
#include <stack>
using namespace std;
class Tree
{
struct Node
{
int data;
Node* right;
Node* left;
Node(int item, Node* left = NULL, Node* right = NULL)
{
data = item;
this->right = right;
this->left = left;
}
};
Node* root;
void inorderRecursive(Node* root);
void inorderIterative(Node* root);
void preorderRecursive(Node* root);
void preorderIterative(Node* root);
void postorderRecursive(Node* root);
void postorderIterative1(Node* root);
void postorderIterative(Node* root);
void printLevel(Node* root, int level);
void levelOrderRecursive(Node* root);
void levelOrderIterative(Node* root);
int heightRecursive(Node* root);
int heightIterative(Node* root);
Node* findMin(Node* root);
Node* findMax(Node* root);
Node* find(Node* root, int item);
Node* remove(Node*root, int item);
Node* insertRecursive(Node* root, int item);
Node* insertIterative(Node* root, int item);
Node* inorderPredecessor(Node* node);
Node* inorderSuccessor(Node* node);
Node* makePreorderInorder(int preorder[], int inorder[], int& index, int low, int high, int n);
Node* makePreorderInorder(int preorder[], int inorder[], int pstart, int pend, int istart, int iend, int n);
Node* makePostorderInorder(int postorder[], int inorder[], int pstart, int pend, int istart, int iend, int n);
Node* makeLevelorderInorder(int levelorder[], int inorder[], int n, int low, int high);
public:
Tree();
void MakePreorderInorder(int * pre, int* in, int n);
void MakePostorderInorder(int * post, int* in, int n);
void MakeLevelorderInorder(int* level, int* in, int n);
void Insert(int item);
bool Delete(int item);
int Height();
int Height(Node* node);
int FindMin();
int FindMax();
void Print();
void PrintLevel(int i);
void Inorder(Node* node);
void Inorder();
void Preorder(Node* node);
void Preorder();
void Postorder(Node* node);
void Postorder();
void LevelOrder();
void LevelOrder(Node* root);
Node* GetRoot();
Node* Find(int item);
Node* InorderSuccessor();
Node* InorderSuccessor(Node* node);
Node* InorderPredecessor();
Node* InorderPredecessor(Node* node);
};

le:///mnt/E/My%20Documents/Desktop/untitled.html

1/9

01/12/2016

untitled.html

void Tree::inorderRecursive(Node* root)


{
if (root != NULL)
{
inorderRecursive(root->left);
cout << root->data << " ";
inorderRecursive(root->right);
}
}
void Tree::inorderIterative(Node* root)
{
if (root == NULL)
return;
stack<Node*> s;
Node* current = root;
bool done = 0;
while (!done)
{
if (current != NULL)
{
s.push(current);
current = current->left;
}
else
{
if (!s.empty())
{
Node* node = s.top();
s.pop();
cout << node->data << " ";
if (node->right)
current = node->right;
}
else
done = true;
}
}
}

//Preorder Traversals
void Tree::preorderRecursive(Node* root)
{
if (root != NULL)
{
cout << root->data << " ";
preorderRecursive(root->left);
preorderRecursive(root->right);
}
}
void Tree::preorderIterative(Node* root)
{
if (root == NULL)
return;
stack<Node*> s;
s.push(root);
while (!s.empty())
{
Node* n = s.top();
s.pop();
cout << n->data << " ";
if (n->right)
s.push(n->right);
if (n->left)
s.push(n->left);
}
}

//Postorder Traversals
void Tree::postorderRecursive(Node* root)
{
if (root != NULL)
{
postorderRecursive(root->left);
postorderRecursive(root->right);
cout << root->data << " ";
}
}
void Tree::postorderIterative1(Node* root) //Using two stacks
{
if (root == NULL)
return;
stack<Node*> s1, s2;
s1.push(root);

le:///mnt/E/My%20Documents/Desktop/untitled.html

2/9

01/12/2016

untitled.html

s1.push(root);
while (!s1.empty())
{
Node* n = s1.top();
s1.pop();
s2.push(n);
if (n->left)
s1.push(n->left);
if (n->right)
s1.push(n->right);
}
while (!s2.empty())
{
cout << s2.top()->data << " ";
s2.pop();
}
}
//Right child should be pushed before the root
//otherwise on popping we can't check if top is right child
void Tree::postorderIterative(Node* root) //Using one stack
{
if (root == NULL)
return;
stack<Node*> s;
bool done = false;
while (!done)
{
if (root != NULL)
{
if (root->right)
s.push(root->right);
s.push(root);
root = root->left;
}
else
{
if (!s.empty())
{
root = s.top();
s.pop();
if (!s.empty() && root->right == s.top())
{
s.pop();
s.push(root);
root = root->right;
}
else
{
cout << root->data << " ";
root = NULL;
}
}
else
done = true;
}
}
}

//Level Order Traversals


//Prints all the elements at a given level
void Tree::printLevel(Node* root, int level)
{
if (root == NULL)
return;
if (level == 0)
cout << root->data << " ";
else
{
printLevel(root->left, level - 1);
printLevel(root->right, level - 1);
}
}
void Tree::levelOrderRecursive(Node* root) //O(n^2) time complexity
{
if (root == NULL)
return;
int height = heightRecursive(root);
for (int i = 0; i <= height; i++)
printLevel(root, i);
}
void Tree::levelOrderIterative(Node* root)
{
if (root == NULL)
return;
queue<Node*> q;
q.push(root);

le:///mnt/E/My%20Documents/Desktop/untitled.html

3/9

01/12/2016

untitled.html

q.push(root);
while (!q.empty())
{
Node* node = q.front();
cout << node->data << " ";
if (node->left != NULL)
q.push(node->left);
if (node->right != NULL)
q.push(node->right);
q.pop();
}
}

//Height of a tree
int Tree::heightRecursive(Node* root)
{
if (root == NULL)
return -1;
else
{
int lheight = heightRecursive(root->left);
int rheight = heightRecursive(root->right);
return (lheight > rheight) ? lheight + 1 : rheight + 1;
}
}
int Tree::heightIterative(Node* root)
{
if (root == NULL)
return -1;
queue<Node*> q;
int maxheight = 0, height = -1;
q.push(root);
while (!q.empty())
{
//Incerement height after traversing a one level.
for (int i = q.size(); i > 0; i--)
{
Node* node = q.front();
q.pop();
if (node->left)
q.push(node->left);
if (node->right)
q.push(node->right);
}
height++;
}
return height;
}
//Basic Functions - Find Min, Find Max, Find and
//Min is the leftmost child
Tree::Node* Tree::findMin(Node* root)
{
if (root == NULL)
return NULL;
else
if (root->left == NULL)
return root;
else
return findMin(root->left);
}
//Max is the right most child
Tree::Node* Tree::findMax(Node* root)
{
if (root == NULL)
return NULL;
else
while (root->right != NULL)
{
root = root->right;
}
return root;
}
Tree::Node* Tree::find(Node* root, int item)
{
if (root == NULL)
return NULL;
else
{
if (root->data == item)
return root;
else
if (root->data > item)
return find(root->left, item);
else
if (root->data < item)

le:///mnt/E/My%20Documents/Desktop/untitled.html

Delete in a BST

4/9

01/12/2016

untitled.html
if (root->data < item)
return find(root->right, item);

}
}
Tree::Node* Tree::remove(Node*root, int item)
{
if (root == NULL)
return NULL;
else if (item < root->data)
root->left = remove(root->left, item);
else if (item > root->data)
root->right = remove(root->right, item);
else if (item == root->data)
{
//Zero child
if (root->left == NULL && root->right == NULL)
{
delete root;
return NULL;
}
else if (root->left == NULL) //One child
{
Node* temp = root;
root = root->right;
delete temp;
}
else if (root->right == NULL) //One child
{
Node* temp = root;
root = root->left;
delete temp;
}
else //Two children
{
Node* temp = findMin(root->right);
root->data = temp->data;
root->right = remove(root->right, root->data);
}
}
return root;
}
Tree::Node* Tree::insertRecursive(Node* root, int item)
{
if (root == NULL)
{
return new Node(item);
}
//Just change sign here and inorder will print the items in descending order.
else if (root->data > item)
{
root->left = insertRecursive(root->left, item);
}
else if (root->data < item)
{
root->right = insertRecursive(root->right, item);
}
return root;
}
Tree::Node* Tree::insertIterative(Node* root, int item)
{
if (root == NULL)
return new Node(item);
Node* prev = NULL;
Node* temp = root;
while (root != NULL)
{
prev = root;
if (root->data > item)
root = root->left;
else if (root->data < item)
root = root->right;
}
if (prev->data > item)
prev->left = new Node(item);
else
prev->right = new Node(item);
return temp;
}
Tree::Node* Tree::inorderPredecessor(Node* node)
{
if (node == NULL)
{
return NULL;
}
if (node->left != NULL) //Predecessor is the rightmost child of left child
{
node = node->left;

le:///mnt/E/My%20Documents/Desktop/untitled.html

5/9

01/12/2016

untitled.html

node = node->left;
while (node->right != NULL)
{
node = node->right;
}
return node;
}
else
{
Node* predecessor = root;
Node* temp = root;
while (temp != NULL)
{
if (temp->data > node->data)
temp = temp->left;
else if (temp->data < node->data)
{
predecessor = temp;
temp = temp->right;
}
else
break;
}
if (predecessor == this->root && node == findMin(root))
return NULL;
return predecessor;
}
}
Tree::Node* Tree::inorderSuccessor(Node* node)
{
if (node == NULL)
return NULL;
if (node->right != NULL)
{
node = node->right;
while (node->left != NULL)
node = node->left;
return node;
}
else
{
Node* successor = root;
Node* temp = root;
while (temp != NULL)
{
if (temp->data > node->data)
{
successor = temp;
temp = temp->left;
}
else if (temp->data > node->data)
temp = temp->right;
else
break;
}
//If element is largest element
if (successor == this->root && root == findMax(this->root))
return NULL;
return (temp == NULL) ? NULL : successor;
}
}

//O(n^2)
Tree::Node* Tree::makePreorderInorder(int * pre, int* in, int& index, int low, int high, int n)
{
if (low > high)
return NULL;
Node* node = new Node(pre[index]);
index++;
int i;
for (i = low; i <= high; i++)
if (in[i] == pre[index - 1])
break;
node->left = makePreorderInorder(pre, in, index, low, i - 1, n);
node->right = makePreorderInorder(pre, in, index, i + 1, high, n);
return node;
}
Tree::Node* Tree::makePostorderInorder(int postorder[], int inorder[], int pstart, int pend, int istart, int iend, int n)
{
if (pstart > pend || istart > iend)
return NULL;
Node* node = new Node(postorder[pend]);
int i;

le:///mnt/E/My%20Documents/Desktop/untitled.html

6/9

01/12/2016

untitled.html

int i;
for (i = 0; i < n; i++)
if (node->data == inorder[i])
break;
node->left = makePostorderInorder(postorder, inorder, pstart, pstart - istart + i - 1, istart, i - 1, n);
node->right = makePostorderInorder(postorder, inorder, pstart + i - istart, pend - 1, i + 1, iend, n);
return node;
}
Tree::Node* Tree::makePreorderInorder(int preorder[], int inorder[], int pstart, int pend, int istart, int iend, int n)
{
if (pstart > pend || istart > iend)
return NULL;
Node* node = new Node(preorder[pstart]);
int i;
for (i = 0; i < n; i++)
if (node->data == inorder[i])
break;
node->left = makePreorderInorder(preorder, inorder, pstart + 1, pstart + i - istart, istart, i - 1, n);
node->right = makePreorderInorder(preorder, inorder, pstart + i - istart + 1, pend, i + 1, iend, n);
return node;
}
int binarysearch(int arr[], int low, int high, int item)
{
if (low <= high)
{
int mid = (low + high) / 2;
if (arr[mid] == item)
return mid;
else
if (arr[mid] < item)
return binarysearch(arr, mid + 1, high, item);
else
return binarysearch(arr, low, mid - 1, item);
}
return -1;
}
int* segregate(int levelorder[], int inorder[], int n, int m)
{
int* a = new int[m];
int j = 0;
for (int i = 1; i < n; i++)
{
if (binarysearch(inorder, 0, n, levelorder[i]) != -1)
a[j++] = levelorder[i];
}
return a;
}
Tree::Node* Tree::makeLevelorderInorder(int levelorder[], int inorder[], int n, int low, int high)
{
if (low > high)
{
return NULL;
}
Node* root = new Node(levelorder[0]);
if (low != high)
{
int i = binarysearch(inorder + low, 0, high, root->data);
if (i == -1)
return NULL;
int* left = segregate(levelorder, inorder, n, i);
int* right = segregate(levelorder, inorder + i + 1, n, n - i - 1);
root->left = makeLevelorderInorder(left, inorder, n, low, i - 1);
root->right = makeLevelorderInorder(right, inorder, n, i + 1, high);
delete[] left;
delete[] right;
}
return root;
}

Tree::Tree()
{
root = NULL;
}
void Tree::MakeLevelorderInorder(int* level, int* in, int n)
{
root = makeLevelorderInorder(level, in, n, 0, n - 1);
}
void Tree::MakePreorderInorder(int * pre, int* in, int n)
{
int k = 0;
root = makePreorderInorder(pre, in, 0, n - 1, 0, n - 1, n);
}
void Tree::MakePostorderInorder(int * post, int* in, int n)
{

le:///mnt/E/My%20Documents/Desktop/untitled.html

7/9

01/12/2016

untitled.html

{
root = makePostorderInorder(post, in, 0, n - 1, 0, n - 1, n);
}
void Tree::Inorder(Node* node)
{
inorderIterative(node);
}
void Tree::Inorder()
{
inorderRecursive(root);
}
Tree::Node* Tree::GetRoot()
{
return root;
}
void Tree::Preorder(Node* node)
{
preorderRecursive(node);
}
void Tree::Preorder()
{
preorderIterative(root);
}
void Tree::Postorder(Node* node)
{
postorderRecursive(node);
}
void Tree::Postorder()
{
postorderIterative(root);
}
void Tree::LevelOrder()
{
levelOrderIterative(root);
}
void Tree::LevelOrder(Node* root)
{
levelOrderRecursive(root);
}
void Tree::Insert(int item)
{
root = insertIterative(root, item);
}
void Tree::Print()
{
LevelOrder();
}
int Tree::Height()
{
return heightRecursive(root);
}
int Tree::Height(Node* node)
{
return heightIterative(node);
}
int Tree::FindMin()
{
Node* n = findMin(root);
if (n == NULL)
return INT_MIN;
else
return n->data;
}
int Tree::FindMax()
{
Node* n = findMax(root);
if (n == NULL)
return INT_MAX;
else
return n->data;
}
Tree::Node* Tree::Find(int item)
{
return find(root, item);
}
bool Tree::Delete(int item)
{
if (remove(root, item) != NULL)
return true;
else
false;
}
Tree::Node* Tree::InorderSuccessor()
{
return inorderSuccessor(root);

le:///mnt/E/My%20Documents/Desktop/untitled.html

8/9

01/12/2016

untitled.html

return inorderSuccessor(root);
}
Tree::Node* Tree::InorderSuccessor(Node* node)
{
return inorderSuccessor(node);
}
Tree::Node* Tree::InorderPredecessor()
{
return inorderPredecessor(root);
}
Tree::Node* Tree::InorderPredecessor(Node* node)
{
return inorderPredecessor(node);
}
void Tree::PrintLevel(int i)
{
printLevel(root, i);
}

int main()
{
Tree t;
int a[] = { 1, 4, 13, 7, 18, 20, 19, 17, 23, 21, 15 };
int b[] = { 1, 4, 7, 13, 15, 17, 18, 19, 20, 21, 23 };
int c[] = { 15, 7, 4, 1, 13, 21, 17, 19, 18, 20, 23 };
int d[] = { 15, 7, 21, 4, 13, 17, 23, 1, 19, 18, 20 };
t.MakeLevelorderInorder(d, b, sizeof(a) / sizeof(*a));
cout << binarysearch(b, 0, 11, 13);
/*t.Insert(15);
t.Insert(7);
t.Insert(4);
t.Insert(21);
t.Insert(13);
t.Insert(17);
t.Insert(23);
t.Insert(1);
t.Insert(19);
t.Insert(18);
t.Insert(20);*/
t.Preorder();
cout << endl;
t.Inorder();
cout << endl;
t.LevelOrder();
system("pause");
return 0;
}

le:///mnt/E/My%20Documents/Desktop/untitled.html

9/9

You might also like