Hi.
I'm working on a binary tree home assignment and I have a problem. Initially my program used global variables. I decided to change that and declare variables inside functions (various reasons, you probably understand why).
I want the program to run without using global variables.
However, I can't figure a way to get rid of this one last global (it's in red) variable (it's a reference actually). When I try fixing this, I get error when running program:
Thread stopped.
Fault: Acess violation at [address].
My program actually contains more functions, such as inorder, reorder, postorder etc but I removed them, cause they are not related to my problem. Less code for you to read :)
Thanks.
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
struct Node
{ int Data;
Node* Right;
Node* Left;
};
void Process (Node* BinTree);
void Insert (Node* &BinTree, int i );
bool Search (Node* BinTree, int a );
int Input();
Node* Root;
int main()
{
int end=1;
char Type;
for(int i=0; i<13; i++)
{
Insert(Root,i);
}
while(end!=0)
{
clrscr();
cout<<"S - Search Number\n";
cout<<"C - Close\n";
cin>>Type;
Type=toupper(Type);
switch(Type)
{
case 'S': {int z=Input(); Search(Root,z);} break;
case 'C': return 0;
default: break;
}
}
getch();
}
void Process(Node* BinTree)
{ cout << BinTree->Data << " "; }
void Insert(Node* &BinTree, int i)
{
Node* one;
Node* two;
int Array[13]={1,2,3,6,5,4,7,8,9,10,13,12,11};
if(!Root)
{
Root = new(Node);
Root->Data = Array[i];
cout << "Root: " << Root->Data << endl;
Root->Right = NULL;
Root->Left = NULL;
}
else
{
int Number = Array[i];
cout << "one " << Number;
one = Root;
while (one != NULL)
{
two = one;
if (Number > one->Data)
{
one = one->Right;
cout << two->Data << " >> " ;
}
else
{
one = one->Left;
cout << two->Data << " << ";
}
}
cout << endl;
if (Number > two->Data)
{
two->Right = new(Node); two = two->Right;
}
else
{
two->Left = new(Node);
two = two->Left;
}
two->Data = Number; two->Right = NULL; two->Left = NULL;
}
}
bool Search( Node* BinTree, int a )
{
int down = 0;
Node *temp;
temp = BinTree;
while (true)
{
if (temp == NULL)
{
cout<<"Not found!\n";
down-=down;
getch();
return false;
}
else if ( a == temp->Data )
{
cout << "Level: " << down << endl;
down-=down;
getch();
return true;
}else if ( a < temp->Data )
{
temp = temp->Left;
down++;
}
else
{
temp = temp->Right;
down++;
}
}
}
int Input()
{
int Array[13]={1,2,3,6,5,4,7,8,9,10,13,12,11};
char row[256];
int var;
do{
cout<<"Enter a number: ";
fgets(row, 256, stdin);
var = atoi (row);
}while(var<1);
return var;
}