You are on page 1of 8

#include <iostream>

using namespace std;


struct node{
int info;
node* left;
node* right;
};

class tree{
private:
node *root;
public:
tree(){root = NULL;}
node *getroot(){return root;}
void insert(int);
void attatchnode(node *,node *);
void inOrder(node *);
void preOrder(node *);
void postOrder(node *);
void descendingOrder(node *);
void deleteTree(node *);
bool search1(node *, int);// as boolean
node *search(node *, int);
node *min(node *);
node *minUsingLoop(node *);
int max(node *);
int count(node *);
int sum(node *);
int noOfExternalNodes(node *);
int noOfInternalNodes(node *);
double average();
double average(node *);
int height(node *);

};
void tree::attatchnode(node *r,node *p){
if(p->info < r->info){
if(r->left != NULL)
attatchnode(r->left,p);
else
r->left = p;
}
else{
if(r->right != NULL)
attatchnode(r->right,p);
else
r->right = p;
}
}

void tree::insert(int x){


node *p = new node;
p->info = x;
p->right = p->left = NULL;

if(root == NULL)
root = p;
else
attatchnode(root , p );
}

void tree::inOrder(node *r){


if(r){// if it's not NULL (Stopping condition (base
case))

// if(r->left) ‫ﻧﻘﺪر ﻧﺤﺬف اﻟﺸﺮط ﻻﻧﮫ ﻣﺎراح ﯾﺪﺧﻞ ﻟﻮ ﻛﺎن ﻧﻞ‬


inOrder(r->left);
cout<<r->info;
// if(r->right) ‫ﻧﻘﺪر ﻧﺤﺬف اﻟﺸﺮط ﻻﻧﮫ ﻣﺎراح ﯾﺪﺧﻞ ﻟﻮ ﻛﺎن ﻧﻞ‬
inOrder(r->right);
}
}
void tree::preOrder(node *r){
if(r){
cout<<r->info;
preOrder(r->left);
preOrder(r->right);
}
}

void tree::postOrder(node *r){


if(r){
postOrder(r->left);
postOrder(r->right);
cout<<r->info;
}
}

void tree::descendingOrder(node *r){


if(r){
descendingOrder(r->right);
cout<<r->info<<" ";
descendingOrder(r->left);
}
}

void tree::deleteTree(node *r){


if(r){
deleteTree(r->right);
deleteTree(r->left);
delete r;
}
}
bool tree::search1 ( node *r, int k){ // ‫اﻟﺘﺮﺗﯿﺐ ﻣﮭﻢ ﺟﺪا ﻻن ﻟﻮ‬
‫ﻋﻜﺴﺖ ﺑﯿﻄﻠﻊ اﯾﺮور‬
if (r == NULL)
return NULL;
else{

if (r->info == k)
return true ;
if (k < r->info)
return search1(r->left , k);
if (k > r->info)
return search1(r->right , k);
}
return NULL;
}

// ‫اذا ﻛﻨﺖ اﺑﯿﮭﺎ ﺗﺮﺟﻊ ﻟﻲ ﻋﻨﻮان اﻟﻨﻮد‬


node * tree::search (node *r,int k){ //‫اﻟﺘﺮﺗﯿﺐ ﻣﮭﻢ ﺟﺪا‬
if (r){

if (r->info == k)
return r ;
if (k < r->info)
return search (r->left , k);
if (k > r->info)
return search (r->right, k);
}
return NULL;
}

node * tree::min(node *r){


if(r == NULL)
return NULL;
if (r->left == NULL)
return r;
return min(r->left);
}
int tree::max (node *r){
if(r == NULL){
cout<<"Tree Is Empty \n";
return -1;
}
else if(r->right == NULL)
return r->info;
return max(r->right);
}

node * tree::minUsingLoop(node *r){


if(r == NULL)
return NULL;
for (; r->left != NULL; r=r->left);
return r;
}

int tree::count(node *r){


if(r == NULL)
return 0;
return 1+count(r->left)+count(r->right);
}

int tree::sum(node *r){


if(r == NULL)
return 0;
return r->info+sum(r->left)+sum(r->right);
}

int tree::noOfExternalNodes(node *r){


if(r == NULL)
return 0;
if(r->left == NULL && r->right == NULL)
return 1;
return noOfExternalNodes(r-
>left)+noOfExternalNodes(r->right);
}
int tree::noOfInternalNodes(node *r){
if((r == NULL) || (r->left == NULL && r->right ==
NULL))// ‫ﻣﻤﻜﻦ ﻧﻔﺼﻞ اﻟﺸﺮطﯿﻦ‬
return 0;
return 1 + noOfInternalNodes(r-
>left)+noOfInternalNodes(r->right);
}

double tree::average(){
if(getroot() == NULL)
return 0;
return
(double)sum(getroot())/tree::count(getroot());
}

double tree:: average(node *r){


if(r == NULL)
return 0;
static int count = 0, sum = 0;
count++;
sum += r->info;
average(r->left);
average(r->right);
return (double)sum / count;
}

int tree:: height(node *r){


int h = 1;
if(r != NULL){
int L_height = height(r->left);
int R_height = height(r->right);

int max_height = std::max(L_height,R_height);


h = 1 + max_height;
}
return h;
}
int main(){
tree obj;
obj.insert(30);
obj.insert(18);
obj.insert(45);
obj.insert(10);
obj.insert(25);
obj.insert(65);

obj.descendingOrder(obj.getroot());
cout<<endl<<endl;

cout<<"Does 45 found in tree? "<<obj.search1(obj.getroot(),


45)<<endl;//1 means true
cout<<"Does 14 found in tree? "<<obj.search1(obj.getroot(),
14)<<endl<<endl;//0 means fales

cout<<"Adress of node that contain number 18


"<<obj.search(obj.getroot(), 18)<<endl;
cout<<"Adress of node that contain number 18
"<<obj.getroot()->left<<endl<<endl;// to match the result of
search

cout<<"Adress of min value "<<obj.min(obj.getroot())<<endl;


cout<<"Adress of node that contain number 10
"<<obj.search(obj.getroot(), 10)<<endl<<endl; //to match the
result of min

cout<<"Max value "<<obj.max(obj.getroot())<<endl<<endl;

cout<<"Number of nodes = "<<obj.count(obj.getroot())<<endl;


cout<<"Number of External nodes =
"<<obj.noOfExternalNodes(obj.getroot())<<endl;
cout<<"Number of Internal nodes =
"<<obj.noOfInternalNodes(obj.getroot())<<endl<<endl;

cout<<"Sum all of nodes = "<<obj.sum(obj.getroot())<<endl;


cout<<"Average = "<<obj.average(obj.getroot())<<endl;
cout<<"Hight of tree =
"<<obj.height(obj.getroot())<<endl<<endl;
return 0;
}
Output: (addresses will be different in your output)

65 45 30 25 18 10

Does 45 found in tree? 1


Does 14 found in tree? 0

Adress of node that contain number 18 0x100704910


Adress of node that contain number 18 0x100704910

Adress of min value 0x1007069d0


Adress of node that contain number 10 0x1007069d0

Max value 65

Number of nodes = 6
Number of External nodes = 3
Number of Internal nodes = 3

Sum all of nodes = 193


Average = 32.1667
Height of tree = 4

You might also like