Need help on basic binary tree programming

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DemonFox
    New Member
    • Oct 2007
    • 15

    #1

    Need help on basic binary tree programming

    i have started my midterm exersize than is on binary treescan anyone help me on the basics
    i have started and i have made the following

    on my tree.h file:

    [CODE=cpp]struct treenode
    {
    int data;
    struct treenode *left;
    struct treenode *right;
    };typedef struct treenode *PTR;

    class tree
    {
    private:
    PTR tree;
    public:
    void insert_node(PTR *pt,int x);
    void preorder_traver sal(PTR t);
    void inorder_travers al(PTR t);
    void postorder_trave rsal(PTR t);
    void find_node(PTR t,int x,int i);
    };
    [/CODE]


    on my tree.cpp file:


    [CODE=cpp]#include<stdio. h>
    #include<conio. h>
    #include<stdlib .h>
    #include<string .h>
    #include"tree.h "

    void tree::insert_no de(PTR *pt,int x)
    {
    PTR t;
    t=*pt;

    if (t==NULL)
    {
    t=(PTR)malloc(s izeof(struct treenode));
    t->data=x;
    t->left=NULL;
    t->right=NULL;
    }
    else
    if (x<t->data)
    insert_node(&(t->left),x);
    else
    insert_node(&(t->right),x);
    *pt=t;
    }

    void tree::preorder_ traversal(PTR t)
    {
    if(t!=NULL)
    {
    printf("%d",t->data);
    preorder_traver sal(t->left);
    preorder_traver sal(t->right);
    }
    }


    void tree::postorder _traversal(PTR t)
    {
    if (t!=NULL)
    {
    postorder_trave rsal(t->left);
    postorder_trave rsal(t->right);
    printf("%d",t->data);
    }

    }

    void tree::inorder_t raversal(PTR t)
    {
    if (t!=NULL)
    {
    inorder_travers al(t->left);
    printf("%d",t->data);
    inorder_travers al(t->right);
    }

    }

    void tree::find_node (PTR t,int x,int i)
    {
    i++;
    if (t==NULL)
    {
    printf("not found");
    printf("Made %d Try",i);
    }
    else if (t->data==x)
    {
    printf("Found") ;
    printf("Made %d try",i);
    }
    else if (x<t->data)
    find_node(t->left,x,i);
    else
    find_node(t->right,x,i);
    }
    [/CODE]

    on a test.cpp file

    [CODE=cpp]#include<stdio. h>
    #include<conio. h>
    #include<stdlib .h>
    #include<string .h>
    #include"tree.h "

    main()
    {
    int x,n,i=0;
    int choise;
    PTR bt;
    bt=NULL;

    while(x!=0)
    {
    insert_node(&bt ,x);
    printf("Give Number");
    scanf("%d",&x);
    }

    printf("1.preor der\n");
    printf("2.posto rder\n");
    printf("3.inord er\n");
    printf("4.findn ode\n");
    printf("2.exit\ n");
    printf("choise? ");
    scanf("%d",&cho ise);

    switch (choise)
    {
    case 1:
    printf("preorde r\n");
    preorder_traver sal(bt);
    break;
    case 2:
    printf("postord er\n");
    postorder_trave rsal(bt);
    break;
    case 3:
    printf("inorder \n");
    inorder_travers al(bt);
    break;
    case 4:
    printf("GIve Number");
    scanf("%d",&n);
    find_node(bt,n, i);
    break;

    } while (choise!=5);
    getch();


    }[/CODE]




    it gives me the error :

    [BCC32 Error] test.cpp(16): E2268 Call to undefined function 'insert_node'
    [BCC32 Error] test.cpp(33): E2268 Call to undefined function 'preorder_trave rsal'
    [BCC32 Error] test.cpp(37): E2268 Call to undefined function 'postorder_trav ersal'
    [BCC32 Error] test.cpp(41): E2268 Call to undefined function 'inorder_traver sal'
    [BCC32 Error] test.cpp(46): E2268 Call to undefined function 'find_node'
    [BCC32 Warning] test.cpp(49): W8019 Code has no effect




    Do i forget something?????? ??
    are there more()??
    like void destroynode()?? ?
    Last edited by Ganon11; Oct 15 '07, 06:07 PM. Reason: Please use the [CODE] tags provided.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Trouble starts right here:
    Originally posted by DemonFox
    class tree
    {
    private:
    PTR tree;
    etc...
    The private member tree has the same name as your class. Big no-no. The compiler thinks you have delcared a constructor with a return of PTR.

    Fix this first.

    Comment

    • DemonFox
      New Member
      • Oct 2007
      • 15

      #3
      i fixed it same errors:( i am stuck 3 days now

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        This code compiles aand links but I didn't run it:
        [code=c]
        struct treenode
        {
        int data;
        struct treenode *left;
        struct treenode *right;
        };typedef struct treenode *PTR;

        class tree
        {
        private:
        ///PTR tree;
        PTR theData;
        public:
        void insert_node(PTR *pt,int x);
        void preorder_traver sal(PTR t);
        void inorder_travers al(PTR t);
        void postorder_trave rsal(PTR t);
        void find_node(PTR t,int x,int i);
        };



        //on my tree.cpp file:


        #include<stdio. h>
        #include<conio. h>
        #include<stdlib .h>
        #include<string .h>
        //#include"tree.h "

        void tree::insert_no de(PTR *pt,int x)
        {
        PTR t;
        t=*pt;

        if (t==NULL)
        {
        t=(PTR)malloc(s izeof(struct treenode));
        t->data=x;
        t->left=NULL;
        t->right=NULL;
        }
        else
        if (x<t->data)
        insert_node(&(t->left),x);
        else
        insert_node(&(t->right),x);
        *pt=t;
        }

        void tree::preorder_ traversal(PTR t)
        {
        if(t!=NULL)
        {
        printf("%d",t->data);
        preorder_traver sal(t->left);
        preorder_traver sal(t->right);
        }
        }


        void tree::postorder _traversal(PTR t)
        {
        if (t!=NULL)
        {
        postorder_trave rsal(t->left);
        postorder_trave rsal(t->right);
        printf("%d",t->data);
        }

        }

        void tree::inorder_t raversal(PTR t)
        {
        if (t!=NULL)
        {
        inorder_travers al(t->left);
        printf("%d",t->data);
        inorder_travers al(t->right);
        }

        }

        void tree::find_node (PTR t,int x,int i)
        {
        i++;
        if (t==NULL)
        {
        printf("not found");
        printf("Made %d Try",i);
        }
        else if (t->data==x)
        {
        printf("Found") ;
        printf("Made %d try",i);
        }
        else if (x<t->data)
        find_node(t->left,x,i);
        else
        find_node(t->right,x,i);
        }


        //on a test.cpp file

        #include<stdio. h>
        #include<conio. h>
        #include<stdlib .h>
        #include<string .h>
        //#include"tree.h "

        int main()
        {
        int x,n,i=0;
        int choise;
        PTR bt;
        bt=NULL;
        tree theTree;

        while(x!=0)
        {
        theTree.insert_ node(&bt,x);
        printf("Give Number");
        scanf("%d",&x);
        }

        printf("1.preor der\n");
        printf("2.posto rder\n");
        printf("3.inord er\n");
        printf("4.findn ode\n");
        printf("2.exit\ n");
        printf("choise? ");
        scanf("%d",&cho ise);

        switch (choise)
        {
        case 1:
        printf("preorde r\n");
        theTree.preorde r_traversal(bt) ;
        break;
        case 2:
        printf("postord er\n");
        theTree.postord er_traversal(bt );
        break;
        case 3:
        printf("inorder \n");
        theTree.inorder _traversal(bt);
        break;
        case 4:
        printf("GIve Number");
        scanf("%d",&n);
        theTree.find_no de(bt,n,i);
        break;

        } while (choise!=5);
        getchar();


        }
        [/code]

        I fixed:
        1) PTR tree in the tree class is now PTR theData;
        2) main() returns an int
        3) getch() is deprecated. Use getchar()
        4) you did not declare a tree object in main(). I created one anc changed the function calls in the cases.

        Comment

        • DemonFox
          New Member
          • Oct 2007
          • 15

          #5
          To begin with thanks for trying to help me i really need it:)
          The basically idea is i want the functions to work
          to test if they work correct and then put them as code in optical form that s my exam :) is there a function like destroy_tree()? ???
          now trying the code you have given
          excuse my english :P

          Comment

          • DemonFox
            New Member
            • Oct 2007
            • 15

            #6
            sorry compiled see now if it works thanks
            edit
            it works fine :P
            your the man:)
            but it only run once and stops
            how can i destroy the tree and re enter??? do you know a way?????

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by DemonFox
              but it only run once and stops
              how can i destroy the tree and re enter??? do you know a way?????
              Write your main differently.

              Add a menu.

              1) Create new Tree
              2) Update current Tree
              3) Exit

              Write a loop and inside the loop get a choice from the user. Based on the choice call an appropriate function. You stay in the loop forever until the user selects Exit and then you clean up the current tree and break out of the loop.

              Comment

              • DemonFox
                New Member
                • Oct 2007
                • 15

                #8
                weaknes any idea how to start with them?????
                is it mater of memory???
                memalloc etc.????????

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Originally posted by DemonFox
                  weaknes any idea how to start with them?????
                  is it mater of memory???
                  memalloc etc.????????
                  I'm not sure I understand your question.

                  Comment

                  • DemonFox
                    New Member
                    • Oct 2007
                    • 15

                    #10
                    in the function
                    void insert_node(PTR *pt,int x);
                    we have
                    t=(PTR)malloc(s izeof(struct treenode));
                    if i want to clear the tree(destroy it to re-enter values)
                    do i have to use the
                    free
                    command??
                    i acn make the function destroy_tree()
                    and how do i run the proggramm (multiple inorder postorder functioning)
                    is the problem in switch?????
                    sorry for my english:(
                    you are big help!!!!!!!!!!! !!!!!!!!!!!!!!! !!!

                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #11
                      You do not use malloc and free in C++.

                      Instead, you use new and delete.

                      Your tree contains many treenodes. When you delete a treenode, a call is made to the tree node destructor (which you have not written) that destructor checks to see if there are any left treenodes. I so, it deletes the left treenode. Which calls the treenode destructor on that treenode, etc...

                      When the delete of the left treennode returns, the treenode destructor checks to see of there are any right treenodes and if there are then it deletes the right treenode. Which calls the treenode destructor, and off you go again.

                      [code=cpp]
                      treenode::~tree node()
                      {
                      if (left)
                      {
                      delete left;
                      left = 0;
                      }
                      if (right)
                      {
                      delete right;
                      right = 0;
                      }
                      [/code]

                      Then in your tree class:
                      [code=cpp]
                      void tree::insert_no de(PTR *pt,int x)
                      {
                      delete t; //deletes entire tree.
                      }
                      [/code]

                      Comment

                      • DemonFox
                        New Member
                        • Oct 2007
                        • 15

                        #12
                        sorry but why goes the
                        delete t; goes in the
                        insert_node function???
                        shouldn t it be on the destroy_node function???




                        Edit: I got your point i have idded constructors and destructors :)
                        Now i have to figure out how to use them :P

                        Comment

                        • weaknessforcats
                          Recognized Expert Expert
                          • Mar 2007
                          • 9214

                          #13
                          Originally posted by DemonFox
                          sorry but why goes the
                          delete t; goes in the
                          insert_node function???
                          shouldn t it be on the destroy_node function???
                          I just did that because you said earlier:
                          Originally posted by DemonFox
                          in the function
                          void insert_node(PTR *pt,int x);
                          we have
                          t=(PTR)malloc(s izeof(struct treenode));
                          if i want to clear the tree(destroy it to re-enter values)
                          do i have to use the
                          free
                          command??
                          Looked odd to me too.

                          Comment

                          • DemonFox
                            New Member
                            • Oct 2007
                            • 15

                            #14
                            Now i need help again
                            I made everything you told me and all in console works fine
                            exept the part deleting the tree
                            now i need help on the following.
                            i am going to put it in optical enviroment and it say error on the construtors
                            it is like this.
                            on tree.h:


                            [CODE=cpp]struct treenode
                            {
                            int data;
                            struct treenode *left;
                            struct treenode *right;
                            };typedef struct treenode *PTR;

                            class tree
                            {
                            private:
                            //PTR treeT;
                            PTR theData;
                            public:
                            tree();
                            tree(PTR t);
                            ~tree();
                            void insert_node(PTR *pt,int x);
                            void destroy_tree(PT R *pt);
                            int countNodes(PTR t);
                            void preorder_traver sal(PTR t);
                            void inorder_travers al(PTR t);
                            void postorder_trave rsal(PTR t);
                            void find_node(PTR t,int x,int i);
                            };



                            on tree.cpp:


                            tree::~tree()
                            {
                            if (theData->left)
                            {
                            delete theData->left;
                            }
                            if (theData->right)
                            {
                            delete theData->right;
                            }
                            }

                            tree::tree()
                            {

                            }

                            tree::tree(PTR t)
                            {
                            theData->data=t->data;
                            theData->left=t->left;
                            theData->right=t->right;
                            }




                            and the errors are:
                            [ILINK32 Error] Error: Unresolved external 'tree::tree()' referenced from C:\DOCUMENTS AND SETTINGS\DEMONF OX\VERSION 0.0.1.2.0\DEBUG \FORMTREETEST.O BJ
                            [ILINK32 Error] Error: Unresolved external 'tree::~tree()' referenced from C:\DOCUMENTS AND SETTINGS\DEMONF OX\VERSION 0.0.1.2.0\DEBUG \FORMTREETEST.O BJ


                            Please what do i wrong?????[/CODE]
                            Last edited by Ganon11; Oct 31 '07, 01:02 PM. Reason: Please use the [CODE] tags provided.

                            Comment

                            • weaknessforcats
                              Recognized Expert Expert
                              • Mar 2007
                              • 9214

                              #15
                              I assume the cpp file with main() does a #include of tree.h and that you have added tree.cpp to the project build.

                              The linker is telling you that it can't find the code for the tree:tree() constructor. Usually that means the tree.cpp file was not compiled or was somehow left out of the build.

                              The fact that tje linker can't find the tree destructor just comfirms that the tree.cpp file was left out of your build.

                              Comment

                              Working...