Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct binary_tree{
  5. struct binary_tree *left;
  6. struct binary_tree *right;
  7. int value;
  8. }binary_tree;
  9.  
  10. void init_btree(binary_tree *root);
  11.  
  12. // traverse the tree in pre-order recursively
  13. void pre_order_r(const binary_tree *root){
  14. if(root == NULL){
  15. return;
  16. }
  17. printf("%d ", root->value);
  18. pre_order_r(root->left);
  19. pre_order_r(root->right);
  20. }
  21.  
  22. int main() {
  23. binary_tree *root = (binary_tree*)malloc(sizeof(binary_tree*));;
  24. init_btree(root);
  25. pre_order_r(root);
  26. printf("n");
  27. }
  28.  
  29. void init_btree(binary_tree *root){
  30. root->left = root->right = NULL;
  31. root->value = 1;
  32. binary_tree * p1 = (binary_tree*)malloc(sizeof(binary_tree*));
  33. p1->left = p1->right = NULL;
  34. p1->value = 2;
  35. binary_tree * p2 = (binary_tree*)malloc(sizeof(binary_tree*));
  36. p2->left = p2->right = NULL;
  37. p2->value = 3;
  38.  
  39. root->left = p1;
  40. root->right = p2;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement