Binary Tree Traversal - Preorder, Inorder, Postorder - c++

I am trying to get the following output
Preorder : 50 5 20 15 27 42 35 25 55 70 60 80 95 90 75
Inorder : 5 15 20 25 27 35 42 50 55 60 70 75 80 90 95
Postorder: 5 20 15 27 42 35 25 55 70 60 80 95 90 75 50
Instead I am getting this
Preorder : 136161285312597531284813109128551336214133138721362114128146401438414645
Inorder : 531259712848128531285513109133621361613621138721412814133143841464014645
Postorder: 531284812597128551336213109128531362114128138721438414645146401413313616
Here is my code
#include "stdafx.h"
#include<iostream>
using namespace std;
struct Node {
int data;
Node *left;
Node *right;
};
//Preorder Funcation
void Preorder(struct Node *root) {
if (root == NULL) return; // if tree/sub-tree is empty, return and exit
cout << root->data; // Visit
Preorder(root->left); // Move left
Preorder(root->right); // move right
}
//Inorder Funcation
void Inorder(Node *root) {
if (root == NULL) return; // if tree/sub-tree is empty, return and exit
Inorder(root->left); //Move left
cout << root->data; //Visit
Inorder(root->right); // move right
}
//Postorder Funcation
void Postorder(Node *root) {
if (root == NULL) return; // if tree/sub-tree is empty, return and exit
Postorder(root->left); // Move left
Postorder(root->right); // move right
cout << root->data; // Visit
}
// Function to Insert Node in a Binary Search Tree
Node* Insert(Node *root, int data) {
if (root == NULL) {
root = new Node();
root->data = data;
root->left = root->right = NULL;
}
else if (data <= root->data)
root->left = Insert(root->left, data);
else
root->right = Insert(root->right, data);
return root;
}
int main() {
Node *root = NULL;
root = Insert(root, '50');
root = Insert(root, '75');
root = Insert(root, '25');
root = Insert(root, '15');
root = Insert(root, '60');
root = Insert(root, '35');
root = Insert(root, '90');
root = Insert(root, '42');
root = Insert(root, '20');
root = Insert(root, '27');
root = Insert(root, '5');
root = Insert(root, '55');
root = Insert(root, '95');
root = Insert(root, '80');
root = Insert(root, '70');
cout << "Preorder: "; //Diaplay Nodes in Preorder.
Preorder(root);
cout << "\n";
cout << "Inorder: "; //Display Nodes in Inorder
Inorder(root);
cout << "\n";
cout << "Postorder: "; //Display Nodes in Postorder
Postorder(root);
cout << "\n";
return 0;
}

Related

Height of Skewed Binary Search Tree

I have implemented the code as follows, In this I have made two functions to calculate the height of binary search tree using recursion and without recursion.
#include <iostream>
#include <list>
using namespace std;
struct node
{
int key;
struct node *left, *right;
};
struct node *newNode(int item)
{
struct node *temp = new node;
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}
struct node *insert(struct node *node, int key)
{
if (node == NULL)
return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
return node;
}
int heightRecursive(struct node *node)
{
if (node == NULL)
return -1;
else
{
int lDepth = heightRecursive(node->left);
int rDepth = heightRecursive(node->right);
if (lDepth > rDepth)
return (lDepth + 1);
else
return (rDepth + 1);
}
}
int heightNonRecursive(node* root)
{
if (root == NULL) {
return 0;
}
list<node*> queue;
queue.push_back(root);
node* front = NULL;
int height = 0;
while (!queue.empty())
{
int size = queue.size();
while (size--)
{
front = queue.front();
queue.pop_front();
if (front->left) {
queue.push_back(front->left);
}
if (front->right) {
queue.push_back(front->right);
}
}
height++;
}
return height;
}
int main()
{
struct node *root = NULL;
root = insert(root, 10);
insert(root, 20);
insert(root, 30);
insert(root, 40);
insert(root, 50);
insert(root, 60);
insert(root, 70);
insert(root, 75);
insert(root, 80);
inorder(root);
int h = heightRecursive(root);
cout << "\n\nHeight of tree using recursive function: " << heightRecursive(root);
cout << "\nHeight of tree using non-recursive function: " << heightNonRecursive(root);
return 0;
}
I have implemented a skewed binary tree like 10->20->30->40->50->60->70->75->80, but in the heightNonRecursive() function, I am getting the height of this binary search tree as 9. Please help where I am doing mistake.
Output of above code:
10 20 30 40 50 60 70 75 80
Height of tree using recursive function: 8
Height of tree using non-recursive function: 9
You have 9 different numbers in increasing order, in unbalanced tree, so the height should be 8, which is correct with recursive function.
10
20
30
40
50
60
70
75
80
With non-recursive function, you just have to start with height = -1;, it should return 0 if there is only one item in the tree.
int heightNonRecursive(node* root)
{
if (root == NULL)
return 0;
list<node*> queue;
queue.push_back(root);
node* front = NULL;
int height = -1; //<-start at -1
while (!queue.empty())
{
int size = queue.size();
while (size--)
{
front = queue.front();
queue.pop_front();
if (front->left)
queue.push_back(front->left);
if (front->right)
queue.push_back(front->right);
}
height++;
}
return height;
}

Why my code for deleting a node in BST instead of deletion is assigning a new value like 0 or random value to node to be deleted?

Node* search(Node* &root, int data) {
if (root == NULL) {
cout << "key not found\n";
return NULL;
}
if (root -> key == data)
return root;
else if(root-> key > data)
return search(root -> left, data);
else
return search(root -> right,data);
}
Node* findMinVal(Node* &root){
Node* current = root;
while (current -> left != NULL)
current = current -> left;
return current;
}
void deleteNode(Node* &root, int key) {
if (root == NULL)
return;
Node* node = search(root, key);
if (node -> left == NULL) {
Node* temp = node;
node = node -> right;
free(temp);
}
else if (node -> right == NULL) {
Node* temp;
node = node -> left;
free(temp);
}
else {
Node* temp = findMinVal(node -> right);
node -> key = temp -> key;
free(temp);
}
}
example tree:
Inorder traversal of the given tree:
20 30 40 50 60 70 80
output and commands :
Delete 20
Inorder traversal of the modified tree
0 30 40 50 60 70 80

delete element BST

When I delete 25 from BT than it works perfectly.
But when I want to delete another number like 17,
it deletes 17 with some other number are deleted from BT.
#include<iostream>
using namespace std;
struct node
{
int data;
node *left;
node *right;
};
node *root = NULL;
node* createnode(int data)
{
node *t = new node();
t->data = data;
t->left = NULL;
t->right = NULL;
return t;
}
node* min(node*);
node* insert(node *root, int data)
{
if (root == NULL)
{
root = createnode(data);
return root;
}
else{
node *t = root;
if (data <= t->data)
t->left = insert(t->left, data);
else{
t->right = insert(t->right, data);
}
}
}
node* print(node *root)
{
if (root == NULL)
{
return root;
}
else{
cout << root->data << " ";
print(root->left);
print(root->right);
}
return root;
}
node* deleten(node *root, int data)
{
if (root == NULL)
return root;
else{
if (data<root->data)
root->left = deleten(root->left, data);
if (data>root->data)
root->right = deleten(root->right, data);
else{
if (root->left == NULL && root->right == NULL)
{
node *t = root;
root = NULL;
delete t;
}
else if (root->left == NULL)
{
node *t = root;
root = root->right;
delete t;
}
else if (root->right == NULL)
{
node *t = root;
root = root->left;
delete t;
}
else{
node *t = min(root->right);
root->data = t->data;
root->right = deleten(root->right, t->data);
}
}
return root;
}
}
node* min(node *root)
{
if (root == NULL)
return root;
else{
if (root->left != NULL)
min(root->left);
else{
return root;
}
}
}
int main()
{
root = insert(root, 15);
root = insert(root, 10);
root = insert(root, 8);
root = insert(root, 12);
root = insert(root, 11);
root = insert(root, 20);
root = insert(root, 23);
root = insert(root, 17);
root = insert(root, 25);
root = print(root);
cout << endl;
root = deleten(root, 17);
root = print(root);
}
case 1:
before deletion : 15 10 8 12 11 20 17 23 25
after deletion : 15 10 8 12 11 23 25 // root=deleten(root,17);
expectation : 15 10 8 12 11 20 23 25
case 2:
before deletion : 15 10 8 12 11 20 17 23 25
after deletion : 15 10 8 12 11 20 17 23 // root=deleten(root,25);
expectation : 15 10 8 12 11 20 17 23
case 3:
before deletion : 15 10 8 12 11 20 17 23 25
after deletion : 17 12 11 23 25 // root= deleten(root,8);
expectation : 15 10 12 11 20 17 23 25
The error is here:
if (data<root->data)
root->left = deleten(root->left, data);
if (data>root->data)
root->right = deleten(root->right, data);
else{
//...
}
If data is less than root->data, it will delete the left side of the tree, and the node, since you are using if not else if to test for the right.
The answer is simple, change to an else if instead:
if (data<root->data)
root->left = deleten(root->left, data);
else if (data>root->data)
root->right = deleten(root->right, data);
else{
//...
}

What's wrong with this code for BST insertion?

So I wrote this code for Insertion of a node in BST (Binary Search Trees) but the program always prints that the tree is empty. I guess there's a problem with the function call I've made. Can you explain the problem.
#include <bits/stdc++.h>
#include <conio.h>
using namespace std;
struct node
{
int key;
node* left;
node* right;
};
void insert(node* root, int item)
{
if(root == NULL)
{
node* temp = new node();
temp->key = item;
temp->left = NULL;
temp->right = NULL;
root = temp;
}
else
{
if((root->key)>item)
{
insert(root->left,item);
}
else
{
insert(root->right,item);
}
}
}
void inorder(node* root)
{
if(root!=NULL)
{
inorder(root->left);
cout<<" "<<root->key<<" ";
inorder(root->right);
}
else cout<<"The tree is empty.";
}
int main()
{
// cout<<endl<<" Here 5 ";
node* root = NULL;
int flag = 1 , item;
while(flag == 1)
{
cout<<"Enter the number you want to enter : ";
cin>>item;
// cout<<endl<<" Here 6";
insert(root, item);
cout<<"Do you want to enter another number (1 -> YES)?";
cin>>flag;
}
cout<<"The final tree is :";
inorder(root);
getch();
return 0;
}
First, the insertion is slightly incorrect. The root pointer must be passed by reference. Just some such as:
void insert(node *& root, int item)
{
if(root == NULL)
{
node* temp = new node();
temp->key = item;
temp->left = NULL;
temp->right = NULL;
root = temp;
}
else
{
if ((root->key) > item)
{
insert(root->left,item);
}
else
{
insert(root->right,item);
}
}
}
which is structurally identic to your code (except for the reference to the root)
Also, your inorder traversal is bizarre because it will print out the message "The tree is empty." each time that the traversal detects a null node. I would modify thus:
void inorder(node* root)
{
if (root == NULL)
return;
inorder(root->left);
cout<<" "<<root->key<<" ";
inorder(root->right);
}
Finally, I would slightly modify main() for managing the case when the tree is empty (instead of doing it inside inorder traversal):
int main()
{
node* root = NULL;
int flag = 1 , item;
while(flag == 1)
{
cout<<"Enter the number you want to enter : ";
cin>>item;
insert(root, item);
cout<<"Do you want to enter another number (1 -> YES)?";
cin>>flag;
}
cout<<"The final tree is :";
if (root == NULL)
cout << "The tree is empty.";
else
inorder(root);
cout << endl;
return 0;
}

Searching for an object inside a binary search tree

**The trace function is the one I am using to search through the binary search tree it returns the right order of numbers when I search for something within the tree but when I want to see if a number doesn't exist i get segmentation fault **
// Binary search tree
#include <stdio.h>
#include <iostream>
#include <assert.h>
using namespace std;
struct node
{
int data;
node * left;
node * right;
};
node * newNode(int data){
node * newnode = new node();
newnode -> data = data;
newnode -> left = NULL;
newnode -> right = NULL;
return newnode;
}
node* Insert (node * root, int data){
if(root == NULL){
root = newNode(data);
}
else if(data <= root-> data){
root->left = Insert(root->left, data);
}
else{
root -> right = Insert(root->right, data);
}
return root;
};
int Trace(node* root,int find){
node * searcher = root;
if(searcher->data == find){
cout << searcher->data << endl;
}
else if(find <= root -> data){
Trace(searcher->left, find);
cout << searcher ->data<< endl;
}
else if(find >= root -> data){
Trace(searcher->right, find);
cout << searcher ->data << endl;
}
else cout << "not found" << endl;
return searcher-> data;
};
int main(){
node * root = NULL; // creating an empty tree
root = Insert(root, 234234);
root = Insert(root, 2334);
root = Insert(root, 23784);
root = Insert(root, 223);
root = Insert(root, 4244);
root = Insert(root, 673234);
root = Insert(root, 2);
root = Insert(root, 2344);
root = Insert(root, 234);
Trace(root,4244);
return 0;
}
When you are walking through the tree looking for a member that doesn't exist, you'll eventually reach a NULL node. When you try to assess its data, you get a segmentation fault. You can avoid this by checking if the node you are going to assess is valid. For example, change your function Trace to:
int Trace(node* root, int find) {
node * searcher = root;
if(!searcher) {
cout << "Node not found" << endl;
//return something here
}
//rest of function stays the same
}