Advertisement
Guest User

Untitled

a guest
Feb 1st, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct
  5. {
  6.     int             value;  // value of node
  7.     struct Node*    parent; // parent of node (if its root node, this pointer must be null
  8.     struct Node*    child1;
  9.     struct Node*    child2;
  10. } Node;
  11.  
  12. Node* createNode(Node* &parent) {
  13.     Node* node = (Node*)malloc(sizeof(Node));
  14.     node->parent = parent;
  15.     printf("Parent is %p\n", &parent);
  16.     return node;
  17. }
  18.  
  19. void createTree(Node* &parent, size_t depth) {  
  20.     if (depth != 0) {
  21.         parent->child1 = createNode(parent);
  22.         parent->child2 = createNode(parent);
  23.        
  24.         //printf("Creating tree\n");
  25.         //printf("Depth is %d\n", depth);
  26.         //printf("Parent adress = %p\n", &parent);
  27.         //printf("Child1 adress = %p\n", &parent->child1);
  28.         //printf("Child2 adress = %p\n\n", &parent->child2);
  29.        
  30.        
  31.         createTree(parent->child1, depth - 1);
  32.         createTree(parent->child2, depth - 1);
  33.     }
  34.    
  35.     return;
  36. }
  37.  
  38. int main()
  39. {
  40.     size_t depth = 5;
  41.    
  42.     Node* main = createNode(0);
  43.    
  44.     createTree(main, 5);
  45.     //printf("Insert depth\n");
  46.     //scanf("%d", &depth);
  47.    
  48.    
  49.    
  50.    
  51.  
  52.    
  53.  
  54.     return 0;
  55.     system("PAUSE");
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement