Thread: Binary Tree help

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    1

    Binary Tree help

    Hi, im currently trying to create a game whereby questions and are put into a binary tree, after a few branches of the tree the final leaf will be an object name (such as a cat). for the moment im just trying to create a binary tree populated with hard-coded questions and objects with a print statement just printing all of the elements in the tree in order to prove that the tree is correct, then i will move onto more difficult things such as getting the user to input the questions and objects.

    so basically im having a lot of difficulty correctly creating the binary tree so any help you can provide would be greatly appreciated. my code so far is below.

    Code:
    #include <stdio.h>
    #include<stdlib.h> 
     
     
            typedef struct node {
        char object[200];
        char question[200];
        struct node *left;
        struct node *right;
            }treenode;  
    void insert(treenode **root,char question[]);  
    int main(void)
    {
        treenode* root=NULL;
        
        insert(&root,("Does it have a tail"));
        insert(&root->right,("Is it edible"));
        insert(&root->left,("is it fluffy"));
            printf("%d \n ",root);
            printf("%d \n ",root->right);
            printf("&d \n ", root->left);
            
    return 0;
    }  
        
        
     void insert(treenode **root, char question[]){
      treenode *newnode;
      
      newnode=(treenode*)malloc(sizeof(treenode));        
      newnode->right=NULL;
      newnode->left=NULL;
        
            }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Hint: why are you ignoring the "root" parameter inside your insert() function?

    You'll also want to use consistent indentation. Even for a program this short your arbitrary (as far as I can tell) indentation makes it difficult to read.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM

Tags for this Thread