Thread: building a complete binary tree

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > Will I still need to fix the warnings?
    Yes, absolutely, always!

    Because you're trying to use those 'edge' fields, which are only 1-bit integers as pointers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #2
    Registered User
    Join Date
    Jan 2024
    Posts
    23
    I fixed everything and printing works now. do you have any idea why segault came?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define on 1
    #define off 0
    
    
    typedef struct Node node;
    
    
    struct Node{
      unsigned int value;
      struct Node* left;
      unsigned int left_edge : 1;
      struct Node* right;
      unsigned int right_edge : 1;
    };
    
    
    void print_tree(node* root){
      printf("%s = %d\n", "value of node ", root->value);
      if (root->left != NULL){
        print_tree(root->left);
      }
      if (root->right != NULL){
        print_tree(root->right);
      }
    
    
    }
    
    
    int main(){
    
    
      node a = {1,NULL,0,NULL,0};
      node b = {2,NULL,0,NULL,0};
      node c = {3,NULL,0,NULL,0};
      node d = {4,NULL,0,NULL,0};
      node e = {5,NULL,0,NULL,0};
      node f = {6,NULL,0,NULL,0};
      node g = {7,NULL,0,NULL,0};
      node h = {8,NULL,0,NULL,0};
    
    
      node inner_3 = {0, &a, 0, &b, 1};
      node inner_4 = {0, &c, 1, &c, 0};
      node inner_5 = {0, &e, 1, &f, 0};
      node inner_6 = {0, &g, 1, &h, 0};
    
    
      node inner_2 = {0, &inner_5, 1, &inner_6, 0};
      // the error does not occur because I did not reserve space for the object with malloc.
      node* inner_1 = malloc(sizeof(node));
      inner_1->value = 2;
      inner_1->left = &inner_3;
      inner_1->right = &inner_4;
      inner_1->left_edge = 1;
      node root = {0, inner_1, 0, &inner_2, 1};
    
    
      print_tree(&root);
    
    
      return 0;
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. leafs of binary complete tree
    By RyanC in forum C Programming
    Replies: 7
    Last Post: 12-30-2018, 04:25 PM
  2. check if binary tree is complete
    By telmo_d in forum C Programming
    Replies: 0
    Last Post: 11-21-2015, 09:14 AM
  3. Building Binary Tree
    By 7heavens in forum C++ Programming
    Replies: 1
    Last Post: 04-18-2010, 01:27 PM
  4. binary tree complete check
    By ichijoji in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2004, 05:48 PM
  5. Building a Binary Search Tree off sorted data
    By BlackCitan in forum C Programming
    Replies: 0
    Last Post: 12-01-2003, 09:52 PM

Tags for this Thread