Ağaçlar Kod Örneği

Ağaçlar ile ilgili kod örneği aşağıdadır.

#include<iostream>
#include<conio.h>

using namespace std;

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

void InsertNode(node *&, node *);
void inorder(node *);
void preorder(node *);
void postorder(node *);
node* search(node* &, int);
int count(node* &);
int sum(node* &);
int counteven(node* &);
int countleaf(node* &);
int count1child(node* &);
int count2child(node* &);
int Height(node* &);

int main()
{
int veri[]={10, 7, 4, 8, 9, 3, 21, 55, 35, 55, 67};
int aranan;
node *root=NULL;

for(int i=0; i<sizeof(veri)/sizeof(int);i++)
        {
        node *temp;
        temp=new node();
        temp->data=veri[i];
        InsertNode(root,temp);
        }

cout<<"inorder dizilimi"<<endl;
inorder(root);
cout<<endl<<"preorder dizilimi"<<endl;
preorder(root);
cout<<endl<<"postorder dizilimi"<<endl;
postorder(root);
cout<<endl<<"aranan elemani giriniz :";
cin>>aranan;
cout<<endl<<"aranan eleman "<<search(root,aranan)<<" pozisyonundadir";
cout<<endl<<"agactaki dugum sayisi ="<<count(root);
cout<<endl<<"agactaki dugum degerleri toplami ="<<sum(root);
cout<<endl<<"agactaki cift degerli dugum sayisi ="<<counteven(root);
cout<<endl<<"agactaki yaprak sayisi ="<<countleaf(root);
cout<<endl<<"agactaki tek evlatli dugum sayisi ="<<count1child(root);
cout<<endl<<"agactaki cift evlatli dugum sayisi ="<<count2child(root);
cout<<endl<<"agacin yuksekligi ="<<Height(root);

getch();
return 0;
}

void InsertNode(node *&root, node *newnode)
{
if (root==NULL)
root=newnode;
else
if ((root->data)>(newnode->data))
InsertNode(root->left,newnode);
else
if (root->data<newnode->data)
InsertNode(root->right,newnode);
}

void inorder(node *r)
{
if (r!=NULL)
   {
   inorder(r->left);
   cout<<r<<" "<<r->data<<endl;
   inorder(r->right);
   }     
}

void preorder(node *r)
{
if (r!=NULL)
   {
   cout<<r->data<<"  ";
   preorder(r->left);
   preorder(r->right);
   }     
}

void postorder(node *r)
{
if (r!=NULL)
   {
   postorder(r->left);
   postorder(r->right);  
   cout<<r->data<<"  ";
   }     
}

node* search(node* &root, int data)
{
	if (root==NULL)
		return NULL;
	else 
		if (root->data==data)
			return root;
	else
		if (data<root->data)
			return search(root->left,data);		
		else
			if (data>root->data)
			 return search(root->right,data);		
}

int count(node* &root)
{
if( root == NULL)
return 0;
else
return(1 + (count(root->left) + count(root->right)));
}

int sum(node* &root)
{
if(root == NULL)
return 0;
else
return(root->data+sum(root->left)+sum(root->right));
}

int counteven(node* &root)
{
if (root!=NULL)
if (root->data%2==0)
return 1+ counteven(root->left)+counteven(root->right);
else
return counteven(root->left)+counteven(root->right);
else
return 0;
}

int countleaf(node* &root)
{
if (root!=NULL)
if (root->left==NULL && root->right==NULL)
return 1;
else
return countleaf(root->left)+countleaf(root->right);
else
return 0;
}

int count1child(node* &root)
{
if (root!=NULL)
if ((root->left==NULL && root->right!=NULL) || (root->left!=NULL && root->right==NULL))
return 1+count1child(root->left)+count1child(root->right);
else
return count1child(root->left)+count1child(root->right);
else
return 0;
}

int count2child(node* &root)
{
if (root!=NULL)
if (root->left!=NULL && root->right!=NULL)
return 1+count2child(root->left)+count2child(root->right);
else
return count2child(root->left)+count2child(root->right);
else
return 0;
}

int Height(node* &root)
{
if(root==NULL) return 0;
else return 1+max(Height(root->left), Height(root->right));
}

Leave a Reply

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir