new to trees --help reqd!!!

This is a discussion on new to trees --help reqd!!! within the C Programming forums, part of the General Programming Boards category; trying to add integer values to a tree and printing values in-order. Code: #include<stdio.h> #include<stdlib.h> #include<conio.h> struct tree{ int info; ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    32

    new to trees --help reqd!!!

    trying to add integer values to a tree and printing values in-order.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    
    struct tree{
                  int info;
                  struct tree *left;
                  struct tree *right;
               };
               
    typedef struct tree *Node;
    
    Node getnode(void);
    Node addtree(Node,int);
    void treeprint(Node);
                            
    int main()
    {
        char ch;
        int d;
        Node root=NULL;
        
        do{
         
            printf("enter info");                        
            scanf("%d",&d);
            
            root=addtree(root,d);
            
            printf("another node(y/n)");
            ch=getch();
                 
          }while(ch=='y' || 'Y');
          
          treeprint(root);
          
          return 0;
              
    }
    
    
    Node getnode(void){
        Node p;
        p = (Node)malloc(sizeof(struct tree));
        return p;
    }    
        
    Node addtree(Node p, int v){
    
          Node n;
          if(p==NULL) {
          n=getnode();
          n->info=v;
          n->left=n->right=NULL;
        }
        
         else if(v<p->info)
         addtree(p->left,v);
        
         else 
        addtree(p->right,v);
        
        return n;                                                                                                                                                                                                                                                                                                                                                                                                          
    }
    
    void treeprint(Node p){
        if(p!=NULL){
            treeprint(p->left);
            printf("%d\t",p->info);
            treeprint(p->right);
        }
    }

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    If I'm not mistaken, your addtree function is broken. You're creating new nodes, but you've done nothing to connect them to the old nodes. You have a bunch of nodes with null pointers.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    If I might blatantly and shamelessly plug my own tutorial on binary search trees: Binary Search Trees I. You can find an older version here, but I made corrections and additions since then, so I highly recommend the first link instead.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trees
    By masterg in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2008, 12:42 PM
  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. Trees
    By wazza13 in forum Windows Programming
    Replies: 3
    Last Post: 03-14-2003, 06:11 AM
  4. AVL Trees
    By kwigibo in forum C Programming
    Replies: 2
    Last Post: 04-17-2002, 05:46 PM
  5. traversing binary trees or partial trees
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 08:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21