Programalama > C#

Etiketler: agac, veri, yapisi

Ort. 0
Puan ver:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

typedef struct treeentry{
                         int key;
                        }TreeEntry;

typedef struct node{
                    TreeEntry entry;
                    struct node *left;
                    struct node *right;
                   }Node;

typedef struct tree{
                    Node *root;
                   }Tree;

void CreateTree(Tree *T){
     T->root=NULL;
}

void PreorderTraverse(Node *root,void(*visit)(TreeEntry)){
     if(root!=NULL)
     {
      visit(root->entry);
      PreorderTraverse(root->left,visit);
      PreorderTraverse(root->right,visit);
     }
}

void InorderTraverse(Node *root,void(*visit)(TreeEntry)){
     if(root)
     {
      InorderTraverse(root->left,visit);
      visit(root->entry);
      InorderTraverse(root->right,visit);
     }
}

void PostorderTraverse(Node *root,void(*visit)(TreeEntry)){
     if(root)
     {
      PostorderTraverse(root->left,visit);
      PostorderTraverse(root->right,visit);
      visit(root->entry);
     }
}
Node *Makenode(TreeEntry t){
	Node *np;
if((np=(Node*)malloc(sizeof(Node)))==NULL)
	printf("no place");
else{
	np->entry=t;
	np->left=NULL;
	np->right=NULL;
}
return np;
}

Node *InsertTree(Node *root,Node *newnode){
     if(root==NULL)
       root=newnode;
     else if(newnode->entry.key<root->entry.key)
             root->left=InsertTree(root->left,newnode);
          else
             root->right=InsertTree(root->right,newnode);
     return root;
}
void print(TreeEntry t){
	printf("%d ",t);
}
int main(){
  int i;
  Tree T;
  Node *newnode;
  TreeEntry item;
  CreateTree(&T);

  printf("5 sayi girin:\n");
  for(i=0;i<5;i++){
  scanf("%d",&item.key);
  newnode=Makenode(item);
  T.root=InsertTree(T.root,newnode);
  }
  printf("preorder:");
  PreorderTraverse(T.root,print);
  printf("\ninorder:");
  InorderTraverse(T.root,print);
  printf("\npostorder:");
  PostorderTraverse(T.root,print);


  return 0;

  }


Yorumlar                 Yorum Yaz
Bu hazır kod'a ilk yorumu siz yapın!
KATEGORİLER
ASP - 240
ASP.NET - 24
C# - 75
C++ - 174
CGI - 8
DELPHI - 247
FLASH - 49
HTML - 536
PASCAL - 246
PERL - 11
PHP - 160
WML - 9
XML - 2
Copyright © 2002 - 2024 Hazır Kod - Tüm Hakları Saklıdır.
Siteden yararlanırken gizlilik ilkelerini okumanızı tavsiye ederiz.
hazirkod.com bir İSOBİL projesidir.