How to print out a binary tree in order? - c++

I'm struggling with the printing of a binary tree that I coded. It's a little program where you can type in different states and then it orders them alphabetically in the binary tree and afterwards it should print them out again, in alphabetic order.
How should I start with the listing function? I read in the internet that a recursive algorithm would be the best, but I don't really know how to do that with my setup.
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
int insertInt()
{
int x;
cin>>x;
while(cin.fail())
{
cout<<"Error! Please enter a valid integer: ";
cin.clear();
cin.ignore();
cin>>x;
}
return x;
}//closes insertInt
struct node
{
string info;
node* right;
node* left;
}; //closes node
class States
{
private:
node* start;
public:
void insert();
void delete();
void list();
void search();
States();
}; //closes States class
States::States()
{
start = new node;
start -> left = NULL;
start -> right = NULL;
start -> info = " ";
}
void States::insert()
{
string state;
char c;
node *temp, *p, *s;
p = start;
s = start;
temp = new node;
temp ->info = state;
cout<<"Please enter the state you want to add: ";
cin>>state;
if(s -> info == " ")
{
s -> info = state;
cout<<"Added state "<<state<<"to the list.\n";
cout<<"Ready to continue? (enter y)";
cin>>c;
return;
}//close if
else
{
while(true)
{
//moving pointer until next level is empty
if(s->info > temp->info && s->left != NULL)
{
s = s->left;
continue;
}//close if
if(s->info < temp->info && s->right != NULL)
{
s = s->right;
continue;
}//close if
//inserting the new node
if(s->info > temp->info && s-> left == NULL)
{
s -> left = temp;
break;
}//close if
if(s->info < temp->info && s->right == NULL)
{
s->right = temp;
break;
}//cloese if
}//close while loop
}//close else
}//close insert function
void States::list()
{
node *p, *s;
p = start;
s = start;
if(start->info == " ")
cout<<"Nothing to display!\n";
if(s->left == NULL)
cout<<"-"<<s->info<<endl;
}
void States::search()
{
string state;
cout<<"Please enter the state you're looking for: ";
cin>>state;
node *s, *temp;
s = start;
temp = new node;
temp ->info = state;
while(true)
{
if(s->info == state)
{
cout<<"Found your state!\n";
break;
}
if(s->info > temp->info)
{
if(s->left == NULL)
break;
else
s = s->left;
continue;
}//close if
if(s->info < temp->info)
{
if(s->right == NULL)
break;
else
s = s->right;
continue;
}//close if
}//close while loop
}//close search function
int main()
{
States s1;
int c;
int exit = 0;
while(exit == 0)
{
cout<<"----------------------------------------------------------------------\n";
cout<<"\t\tChoose one of the following options\n";
cout<<"\t\t\t\t(1) Add a state\n";
cout<<"\t\t\t\t(2) List states\n";
cout<<"\t\t\t\t(3) Search for state\n";
cout<<"\t\t\t\t(4) Delete state\n";
cout<<"\t\t\t\t(5) Exit\n";
cout<<"----------------------------------------------------------------------\n";
c = insertInt();
switch(c)
{
case 1:
s1.insert();
break;
case 2:
s1.list();
break;
case 3:
s1.search();
break;
case 4:
s1.delete();
break;
case 5:
exit = 1;
cout<<"Exiting...\n";
break;
default:
cout<<"Error. Please enter a valid integer.\n";
}//close switch
}//closes while loop
}//closes main

Say that node is a pointer of type Node* representing some node of your tree. Then inorder(Node*) is defined as follows:
void inorder(Node* node)
{
if (!node) // end the recursion if node == nullptr
return;
inorder(node->left); // display the left subtree
std::cout << node->info << " "; // display the current node
inorder(node->right); // display the right subtree
}

Related

I keep getting an error on the opening bracket of my main function with the message obj\Debug\main.o||In function `ZN11linked_listC1Ev':|

I've tried compiling in Visual Studio and CodeBlocks and I've received different error messages from both. I've been working at trying to solve this one issue for several hours and would greatly appreciate the help. I'm just trying to write a simple linked list program.
This is my Header file:
#ifndef LINKED_LIST_H_INCLUDED
#define LINKED_LIST_H_INCLUDED
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
/*
* Node Declaration
*/
struct node
{
int info;
struct node *next;
}*start;
/*
* Class Declaration
*/
class linked_list
{
public:
node* create_node(int);
void insert_begin();
void insert_last();
void insert_pos();
void delete_pos();
void delete_begin();
void delete_last();
void display();
linked_list()
{
start = NULL;
}
};
#endif // LINKED_LIST_H_INCLUDED
Here is my implementation file:
#include <iostream>
#include<cstdio>
#include<cstdlib>
#include "linked_list.h"
using namespace std;
/*
* Create Node
*/
node *linked_list::create_node(int value)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->info = value;
temp->next = NULL;
return temp;
}
}
/*
* Display all the elements of the linked list
*/
void linked_list::display()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->info<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
/*
* Inserting at the beginning of the list
*/
void linked_list::insert_begin()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *p;
temp = create_node(value);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<"Element Inserted at beginning"<<endl;
}
/*
* Inserting Node at the end of the list
*/
void linked_list::insert_last()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s;
temp = create_node(value);
s = start;
while (s->next != NULL)
{
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<"Element Inserted at last position"<<endl;
}
/*
* Insertion of node at the specified position
*/
void linked_list::insert_pos()
{
int value, pos, counter = 0;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s, *ptr;
temp = create_node(value);
cout<<"Enter the position at which node to be inserted: ";
cin>>pos;
int i;
s = start;
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos == 1)
{
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
ptr = start;
start = temp;
start->next = ptr;
}
}
else if (pos > 1 && pos <= counter)
{
s = start;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<"Position out of range"<<endl;
}
}
/*
* Deletion element at a given position
*/
void linked_list::delete_pos()
{
int pos, i, counter = 0;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the position of value to be deleted: ";
cin>>pos;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos > 0 && pos <= counter)
{
s = start;
for (i = 1;i < pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
cout<<"Element Deleted"<<endl;
}
}
/*
* Deletion of element at the beginning of the list
*/
void linked_list::delete_begin()
{
struct node *temp, *p;
temp = start;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
else
{
p = start;
start = temp;
p=start->next;
delete temp;
}
cout<<"Element deleted at beginning"<<endl;
}
/*
* Deletion of element at the end of the list
*/
void linked_list::delete_last()
{
struct node *p, *s;
s = start;
while (s->next != NULL)
{
p = s;
s = s->next;
}
p->next = NULL;
delete s;
cout<<"Element deleted at last position"<<endl;
}
And Here is my driver file:
#include <iostream>
#include<cstdio>
#include<cstdlib>
#include "linked_list.h"
using namespace std;
main()
{
int choice, nodes, element, position, i;
linked_list linlist;
start = NULL;
while (1)
{
cout<<endl<<"---------------------------------"<<endl;
cout<<endl<<"Linked List Homework Menu"<<endl;
cout<<endl<<"---------------------------------"<<endl;
cout<<"1.Insert Node at the Beginning of the List"<<endl;
cout<<"2.Insert Node at the Last Position in the List"<<endl;
cout<<"3.Insert Node at Specified Position"<<endl;
cout<<"4.Delete Node at Specified Position"<<endl;
cout<<"5.Delete Node at the Last Position in the List"<<endl;
cout<<"6.Delete Node at the Beginning of the List"<<endl;
cout<<"7.Display Linked List"<<endl;
cout<<"8.Exit "<<endl;
cout<<"Please enter a selection : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at the Beginning: "<<endl;
linlist.insert_begin();
cout<<endl;
break;
case 2:
cout<<"Inserting Node at the Last Position: "<<endl;
linlist.insert_last();
cout<<endl;
break;
case 3:
cout<<"Inserting Node at a Specific Position:"<<endl;
linlist.insert_pos();
cout<<endl;
break;
case 4:
cout<<"Deleting Node at a Specific Position: "<<endl;
linlist.delete_pos();
break;
case 5:
cout<<"Deleting Node at the Last Position: "<<endl;
linlist.delete_last();
break;
case 6:
cout<<"Deleting Node at the Beginning: "<<endl;
linlist.delete_begin();
break;
case 7:
cout<<"Display the linked list"<<endl;
linlist.display();
cout<<endl;
break;
case 8:
cout<<"Exiting Program... Goodbye!"<<endl;
exit(1);
break;
default:
cout<<"Option not viable"<<endl;
}
}
}
The issue is encountered when the compiler gets to line 10 in the driver file. I'll put both error lists from Visual Studio and CodeBlocks below. I'm still grasping C++ so thanks in advance to anyone who decides to help!
Error Log
Other Error Log
The first error:
struct node
{
int info;
struct node *next;
}*start;
You created non-const variable in heaader file. Then this header was included in two source files.
Eventually, you have two variables called 'start' in two object files. That`s why linker throws an error "multiple definition of start".
As was mentioned in the comments, 'start' variable must be moved to linked_list class.
The second error:
main()
should be
int main()

Printing the Circular linked list

#include <iostream>
#include <cstdlib>
using namespace std;
struct node
{
int data;
struct node* link;
};
struct node* front;
struct node* rear;
void insert()
{
struct node*temp;
temp = (struct node*)malloc(sizeof(struct node));
cin >> temp->data;
if (front == NULL)
{
front = rear = temp;
}
else
{
rear->link = temp;
rear = rear->link;
}
rear->link = front;
}
void del()
{
struct node* temp;
temp = front;
if (front == NULL)
cout << "Underflow";
else
{
front = front->link;
free(temp);
}
rear->link = front;
}
void disp()
{
struct node* temp;
temp = front;
if (front == NULL)
cout << "Empty";
else
{
do
{
cout << temp->data << "->";
temp = temp->link;
} while (temp != front);
}
rear->link = front;
}
int main()
{
int n;
bool run = true;
while (run)
{
cin >> n;
switch (n)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
disp();
break;
case 4:
run = false;
break;
}
}
return 0;
}
I am new to the concept.I wrote a code for insertion deletion and display of elements using queue implementing the concept of linked list..The program is working fine without any errors . But when the output is displayed . I need to display the output along with the first element I inserted..E.g: My input is
1
2
1
3
1
4
3
The output is 2->3->4->
but the output I need is 2->3->4->2->
I want to see the first element again at the last
All you have to do is just adding a single line after the do-while loop as follows:
do
{
cout << temp->data << "->";
temp = temp->link;
} while (temp != front);
cout<< front->data << "->";
assuming front is the head of your linked-list. Now I've a question for you, what you gonna do if there is a single entry? Since it is going to be displayed twice.
Simple enough, change this
do
{
cout<<temp->data<<"->";
temp=temp->link;
}
while(temp!=front);
to this
int first = temp->data;
do
{
cout<<temp->data<<"->";
temp=temp->link;
}
while(temp!=front);
cout<<first<<"->"; // print first element again

C++ code working as intended on Linux but not on Windows

I do not know why this code breaks ,probably when adding new node, on Windows .Returns "Process returned -1073741819 (0xC0000005)"),it was compiled with GNU GCC.It works perfectly fine on Linux.
Also tested on geeksforgeeks ide , this is the link https://ide.geeksforgeeks.org/feo8SYMsFP.
When debugged, SIGSEGV is returned when adding node but I am not sure why..
For example, input : 1 10 1 11 then it breaks..
#include <iostream>
#include <stdlib.h>
struct Node
{
int key;
Node * left;
Node * right;
};
class binarySearchTree
{
private:
Node *root;
Node *newNode(int key)
{
Node *temp = new Node;
temp->left = temp->right = NULL;
temp->key = key;
return temp;
}
void traverse_inorder(Node *temp)
{
if (temp==NULL)
return;
traverse_inorder(temp->left);
std::cout <<"Current key: "<< temp->key << "\n";
traverse_inorder(temp->right);
}
void find(Node* temp,int key)
{
if (temp==NULL)
return;
find(temp->left,key);
if (temp->key == key)
std::cout <<"Key " << key << " found\n";
find(temp->right,key);
}
Node* minValueNode(Node* n)
{
Node* x = n;
while (x->left != NULL)
x = x->left;
return x;
}
Node* deleteNode(Node* temp, int key)
{
if (temp == NULL)
return temp;
if (key < temp->key)
temp->left = deleteNode(temp->left, key);
else if (key > temp->key)
temp->right = deleteNode(temp->right, key);
else
{
if (temp->left == NULL)
{
Node *x = temp->right;
delete temp;
return x;
}
else if (root->right == NULL)
{
Node *x = temp->left;
delete temp;
return x;
}
Node* x = minValueNode(temp->right);
temp->key = x->key;
temp->right = deleteNode(temp->right, x->key);
}
return temp;
}
void delete_tree(Node *temp)
{
if (temp == NULL)
return;
delete_tree(temp->left);
delete_tree(temp->right);
delete temp;
}
void traverse_postorder(Node* temp)
{
if (temp == NULL)
return;
traverse_postorder(temp->left);
traverse_postorder(temp->right);
std::cout <<"Current key: "<< temp->key << "\n";
}
void traverse_preorder(Node* temp)
{
if (temp == NULL)
return;
std::cout <<"Current key: "<< temp->key << "\n";
traverse_preorder(temp->left);
traverse_preorder(temp->right);
}
void add(int key)
{
if (root == NULL)
root = newNode(key);
else
{
Node *temp = root;
while (true)
{
if (temp->key > key)
{
if (temp->left == NULL)
{
temp->left = newNode(key);
break;
}
else
temp = temp->left;
}
else if (temp->key < key)
{
if (temp->right == NULL)
{
temp->right =newNode(key);
break;
}
else
temp = temp->right;
}
else
{
std::cout << "Key already added!\n";
break;
}
}
}
}
public:
binarySearchTree()
{
root = NULL;
}
~binarySearchTree()
{
delete_tree(root);
}
void _find(int key)
{
find(root,key);
}
void _del(int key)
{
root = deleteNode(root,key);
}
void _traverse_postorder()
{
traverse_postorder(root);
}
void _traverse_preorder()
{
traverse_preorder(root);
}
void _traverse_inorder()
{
traverse_inorder(root);
}
void _add(int key)
{
add(key);
}
};
int main()
{
binarySearchTree bst;
std::cout << "Binary Search Tree Menu (1-add key, 2-search key, 3-remove key, 4-traverse inorder, 5-traverse postorder, 6-traverse preorder, q-exit)\n";
char input;
do
{
std::cout << "Awaiting input ";
std::cin >> input;
int key;
switch(input)
{
case '1':
std::cout << "Enter key.. ";
std::cin >> key;
bst._add(key);
break;
case '2':
std::cout << "Enter key.. ";
std::cin >> key;
bst._find(key);
break;
case '3':
std::cout << "Enter key.. ";
std::cin>>key;
bst._del(key);
break;
case '4':
bst._traverse_inorder();
break;
case '5':
bst._traverse_postorder();
break;
case '6':
bst._traverse_preorder();
break;
}
}
while (input != 'q');
std::cout << "Deleting Binary Search Tree...\n";
return 0;
}
This code:
struct Node
{
int key;
Node * left;
Node * right;
};
Node *newNode(int key)
{
Node *temp = new Node;
temp->key = key;
return temp;
}
means that newNode returns pointer to node with indeterminate values for left and right. Instead these should be null pointers, (e.g. use new Node(); instead of new Node;).
On the original system it probably happened to get zeroed memory from the allocator so the problem didn't show up.

Run time error when I try printing the linked list

This is my code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct node
{
string program;
node *next;
}
bool isEmpty(node *head)
{
if (head == NULL)
return true;
else
return false;
}
void insertAsFirstElement(node *&head, node *&tail, string program)
{
node *temp = new node;
temp->program = program;
temp->next = NULL;
head = temp;
tail = temp;
}
void initialize(node *&head, node *&tail, string program)
{
if (isEmpty(head))
insertAsFirstElement(head, tail, program);
else
{
node* temp = new node;
temp->program = program;
temp->next = NULL;
tail->next = temp;
tail = temp;
}
}
void insert(node *& head, node *& tail, string program, int num)
{
if (isEmpty(head))
insertAsFirstElement(head, tail, program);
else
{
string free ("FREE");
int i = 0;
while (head != NULL)
{
while (head->program.compare(free) != 0)
head = head->next;
while (head->program.compare(free) == 0)
{
head->program = program;
tail->next = head;
tail = head;
i++;
if (i == (num-1))
return;
}
}
}
}
void showList(node *current)
{
if (isEmpty(current))
cout << "The list is empty. \n";
else
{
int i = 0;
cout << "The list contains: \n";
while(current != NULL)
{
cout << current->program << " ";
if ((i + 1) % 8 == 0)
cout << "\n";
current = current->next;
i++;
}
}
}
int main()
{
cout << "Menu";
cout << "\n1. Add program\n";
cout << "2. Print Memory\n";
cout << "3. Exit\n";
node *head = NULL;
node *tail = NULL;
int choice;
string name;
int memory;
int numPages;
for (int i = 0; i <= 31; i++)
{
initialize(head, tail, "FREE");
}
showList(head);
do
{
cout << "choice - ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Program name - ";
cin >> name;
cout << "Program size - ";
cin >> memory;
if (memory % 4 == 0)
numPages = memory / 4;
else if (memory % 4 != 0)
numPages = memory / 4 + 1;
insert(head, tail, name, numPages);
cout << "Program " << name << " added succesfully.\n";
case 2:
showList(head);
}
} while (choice!=3);
return 0;
}
The error is in the insert function because when I try printing the linked list after I call the insert function it never stops printing, but I don't understand my mistake.
Also in the switch in the main when insert 2 as choice it only runs case 2, but when I insert 1 as choice it runs both case 1 and case 2.
EDIT: I haven't changed anything and now once I call the insert function the program stops running
Regarding switch case, you need to add break; after case 1:
"The error is in the insert function"
So why do you not fix it?
I agree, that your insert function is flawed. Below I have marked 3 lines that probably contribute to the code's problems.
Key Idea: When inserting the second and subsequent items to a linked list, your code should modify only 'head' or 'tail', never both.
When inserting at the head, the tail should not change.
When inserting at the tail, the head should not change.
vv vv
void insert(node *& head, node *& tail, string program, int num)
{
if (isEmpty(head))
insertAsFirstElement(head, tail, program);
else
{
string free ("FREE");
int i = 0;
while (head != NULL)
{
while (head->program.compare(free) != 0)
head = head->next; <<<<<<<<<<<<<<<<<<<<<
while (head->program.compare(free) == 0)
{
head->program = program; <<<<<<<<<<<<<<<<<<<<
tail->next = head;
tail = head; <<<<<<<<<<<<<<<<<<<<
i++;
if (i == (num-1))
return;
}
}
}
}
Your switch statement is missing a break; statement. Since there is no break statement for case 1, the compiler continues through case 1 and 2, since case 2 follows case 1.
Here is a more clear description from Tutorialspoint:
When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
See this code:
switch (choice)
{
case 1:
// ... Add the rest of your code here
break // <-- this is required so that the switch is termintated after completing the appropriate case instead of continuing on to the next case
case 2:
showList(head); // there are no more cases after this, so only this case runs if switch(2) occurs.
}

Output of c++ program not coming as expected

I have made a C++ program for a binary tree. But the terminal is not asking the statement for inputting the direction for where the elements are to be placed.
Also when I replace the statement from " node *temp = new node " to "node *temp=NULL" the program stops working .
#include <iostream>
#include <cstring>
using namespace std;
class node {
int data;
node * left;
node * right;
public:
node * level_order(node * first);
node * create_bt(node * first);
void display(node * first);
};
//node *first=NULL;
node * node::create_bt(node * first) {
node * temp = new node;
int ele;
//char dir;
cout << "\n Enter data ";
cin >> ele;
temp->data = ele;
temp->left = NULL;
temp->right = NULL;
if (first == NULL) {
temp = first;
return first;
} else {
char dir[20];
cout << "\n Enter the direction ";
cin >> dir;
node * cur = first;
int j = 0;
while (dir[j] != '\0') {
if (dir[j] == 'l') {
cur = cur->left;
}
if (dir[j] == 'r') {
cur = cur->right;
}
j++;
}
cur = temp;
return first;
}
}
void node::display(node * first) {
if (first == NULL)
return;
cout << "\n " << first->data;
display(first->left);
display(first->right);
}
int main() {
int n;
node s;
node * first = NULL;
cout << "\n No of elements ";
cin >> n;
for (int i = 0; i < n; i++) {
first = s.create_bt(first);
}
s.display(first);
return 0;
}
first=s.create_bt(first); does not changes state, from NULL to 'l' or 'r'. You have to change that.
node*node::create_bt(node *first)
{
node *temp=new node;
int ele;
//char dir;
cout<<"\n Enter data ";
cin>>ele;
temp->data=ele;
temp->left=NULL;
temp->right=NULL;
char dir[20];
cout<<"\n Enter the direction ";
cin>>dir;
if(first==NULL)
{
temp=first;
return first;
}
else
{
node*cur=first;
int j=0;
while(dir[j]!='\0')
{
if(dir[j]=='l')
{
cur=cur->left;
}
if(dir[j]=='r')
{
cur=cur->right;
}
j++;
}
cur=temp;
return first;
}
}
I believe you re looking something like this. This is a basic binary tree, i had to make a basic one in order to understand how it works and how it chooses left and right. I make a class inside a class, in order to have access to my data members (node class, int data, *left , *right) and have them at the same time protected, all-in-one. As you can see "newnode" just creates a node and NULL s the pointers. Thats it. "Find" searches and finds a node with a current key, and returns it when exits. All the rest, i guess, you can understand them, as they are prety much the same with your code. The only thing you have to do is to define, when you want to direct the node you want. REMINDER: You have to find a way to utilize it, so the leafs will not end far-left or far-right.("Enter the direction"). I hope i helped you understand.
#include <iostream>
#include <conio.h>
using namespace std;
class mybTree {
class node {
public:
int data;
node * left;
node *right;
};
node *root;
node *newnode(int num){
node *newnode1;
newnode1 = new (nothrow) node;
newnode1->data = num;
newnode1->left = NULL;
newnode1->right = NULL;
return newnode1;
}
public:
node *find (int key) {
node *current;
current = root;
while (current->data !=key){
if (key<current->data){
current = current->left;
} else {
current = current->right;
}
if (current == NULL){
return NULL;
}
}
return NULL;
}
void display (node *ptr);
void display_tree();
bool insert(int num);
void post_order_delete(node *ptr);
mybTree();
~mybTree();
};
int main(){
char ch = ' ';
int a;
mybTree mybTree1;
while (ch !='0'){
cout << "0->Exit"<<endl<< "1-> add"<<endl<< "2-> find" <<endl<<"3-> Show me the tree\n";
ch = getch();
switch (ch) {
case '0':
break;
case '1':
cout << "number";
cin >> a;
if (!mybTree1.insert(a)){
cout << "Not enough memory" << endl;
}
break;
case '2' :
cout << "Number:" ;
cin >> a;
if (mybTree1.find(a)!=NULL) {
cout << "Found" << endl;
} else {
cout << "Not existed" << endl;
}
break;
case '3':
mybTree1.display_tree();
cout<<endl;
break;
default:
cout << "Wrong Message";
break;
}
}
return 0;
}
void mybTree::display(node *ptr) {
if (ptr == NULL){
return;
}
display(ptr->left);
cout << ptr->data<<endl;
display(ptr->right);
}
void mybTree::display_tree() {
//Displays the Tree
display(root);
}
bool mybTree::insert(int num) {
//It inserts a node. Desides left or right.
node *next,*current,*ptr;
int isleft;
next = current = root;
ptr = newnode(num);
if (ptr == NULL) {
return false;
}
if (root == NULL) {
root = ptr;
return true;
}
while (1){
if (num < current->data){
next = current->left;
isleft = 1;
} else {
next = current->right;
isleft = 0;
}
if (next == NULL){
if (isleft){
current->left = ptr;
} else {
current->right = ptr;
}
return true;
}
current=next;
}
return false;
}
void mybTree::post_order_delete(node *ptr) {
//deletes the node. Usefull for destructor
if (ptr == NULL){
return;
}
post_order_delete(ptr->left);
post_order_delete(ptr->right);
cout << ptr->data;
delete ptr;
}
mybTree::mybTree() {
//Constructor
root = NULL;
}
mybTree::~mybTree() {
//Destructor
post_order_delete(root);
root = NULL;
}