View Single Post
  #2 (permalink)  
Old February 8th, 2005, 06:08 AM
rupsee rupsee is offline
Registered User
 
Join Date: Feb 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to rupsee Send a message via AIM to rupsee
Default

hi...
you could simply create a binary tree....
check this code...
#include<stdlib.h>
#include<stdio.h>

struct tree_el {
   int val;
   struct tree_el * right, * left;
};

typedef struct tree_el node;

void insert(node ** tree, node * item) {
   if(!(*tree)) {
      *tree = item;
      return;
   }
   if(item->val<(*tree)->val)
      insert(&(*tree)->left, item);
   else if(item->val>(*tree)->val)
      insert(&(*tree)->right, item);
}

void printout(node * tree) {
   if(tree->left) printout(tree->left);
   printf("%d\n",tree->val);
   if(tree->right) printout(tree->right);
}

void main() {
   node * curr, * root;
   int i;

   root = NULL;

   for(i=1;i<=10;i++) {
      curr = (node *)malloc(sizeof(node));
      curr->left = curr->right = NULL;
      curr->val = rand();
      insert(&root, curr);
   }

   printout(root);
}


Reply With Quote