C 11
Binary tree Guest on 5th April 2021 12:46:37 AM
  1. /* this code is from http://www.macs.hw.ac.uk/~rjp/Coursewww/Cwww/tree.html */
  2.  
  3. #include<stdlib.h>
  4. #include<stdio.h>
  5.  
  6. struct tree_node {
  7.   int val;
  8.   struct tree_node * right, * left;
  9. };
  10.  
  11. typedef struct tree_node node;
  12.  
  13. void insert(node ** tree, node * item) {
  14.   if(!(*tree)) {
  15.     *tree = item;
  16.     return;
  17.   }
  18.   if(item->val<(*tree)->val)
  19.     insert(&(*tree)->left, item);
  20.   else if(item->val>(*tree)->val)
  21.     insert(&(*tree)->right, item);
  22. }
  23.  
  24. void printout(node * tree) {
  25.   if(tree->left) printout(tree->left);
  26.   printf("%d\n",tree->val);
  27.   if(tree->right) printout(tree->right);
  28. }
  29.  
  30. int main() {
  31.  
  32.   node * curr, * root;
  33.   int i;
  34.  
  35.   root = NULL;
  36.   srand((unsigned int)time(0));
  37.  
  38.   for(i=1;i<=10;i++) {
  39.     curr = (node *)malloc(sizeof(node));
  40.     curr->left = curr->right = NULL;

Paste-bin is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.