Thread: Why does this crash??

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    4

    Arrow Why does this crash??

    code:

    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    #include <string.h>
    #include <dos.h>


    class tnode
    {
    public:
    int nodevalue;
    tnode *left, *right;

    //Constructor
    tnode (int item, tnode *lptr = NULL,tnode *rptr = NULL)
    {
    nodevalue=item;
    left=lptr;
    right=rptr;
    }

    };

    tnode *maxnode=NULL;

    void findmaxvalue(tnode* tempnode)
    {
    if (tempnode!=NULL)
    {
    if (tempnode->nodevalue > maxnode->nodevalue)
    {
    maxnode=tempnode;
    }
    findmaxvalue(tempnode->left);
    findmaxvalue(tempnode->right);
    }
    }

    int main()

    {

    system("CLS");
    //Building the tree
    tnode *b,*c,*d,*e,*f,*root;
    d= new tnode(5);
    e= new tnode(38);
    f= new tnode(42);
    b= new tnode(15, d);
    c= new tnode(40,e,f);
    root=new tnode(35,b,c);

    findmaxvalue(root);
    cout<<"\n The maximum value in the tree is :: "<< maxnode->nodevalue;
    getch();
    return 0;
    }

    /code

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You have to initialize maxvalue before you access it. It's still NULL when you first access it in findmaxvalue.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    4
    You lost me......

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    void findmaxvalue(tnode* tempnode)
    {
    if (tempnode!=NULL)
    {
    if (tempnode->nodevalue > maxnode->nodevalue)
    The first time you call findmaxvalue, maxnode is NULL. So when it attemts to find maxnode->nodevalue, it will throw an error. Try having 'tnode maxnode = root;' instead of =NULL.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    4
    Changing
    tnode *maxnode=NULL;
    to
    tnode *maxnode=root;

    errors: root' : undeclared identifier
    and

    'initializing' : cannot convert from 'int' to 'class tnode *'

    what did I miss ??

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    4
    I got it now! Thanks a ton!!

  7. #7
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Actually there is quite many errors in your code but dont worry, we will help you .

    The problem lies in your finmaxvalue
    Code:
    void findmaxvalue(tnode* tempnode)
    {
    if (tempnode!=NULL)
    {
    if (tempnode->nodevalue > maxnode->nodevalue)
    {
    maxnode=tempnode;
    }
    findmaxvalue(tempnode->left);
    findmaxvalue(tempnode->right);
    }
    }
    To find the maximum value in a B-Tree is easy (both with recursion or iteration, IŽll use recusrion here). What you really want to find is the element which is the most rightfar in the tree.
    In other words
    Code:
    void findmaxvalue(tnode* tempnode)
    {
    
    	/* You have a root node which is NULL */
    	if (tempnode == NULL)
    		return ;
    
    	/* Reached 'last rightmost' (your case 42) element */
    	if (tempnode->right == NULL)
    	{
    		maxnode = tempnode;
    		return ;
    	}
    /* Pass next right childen to findmaxvalue recursivly */
    findmaxvalue(tempnode->right);
    }
    IŽll leave findminvalue as an exercise for you to figure out (almost identical with findmaxvalue).

    Why not implement a B-Tree with class and high-level functions for it???

    Also read this and this before posting next time.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donŽt come easy in life!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Hooking Crash?
    By Elysia in forum Windows Programming
    Replies: 9
    Last Post: 03-15-2008, 01:13 PM
  3. Can not debug a crash
    By hannibar in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2007, 10:02 AM
  4. Dynamic array sizing causes crash
    By Mithoric in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2003, 07:46 AM
  5. FYI: asctime(gmtime(&mytime)) = crash!
    By anonytmouse in forum C Programming
    Replies: 2
    Last Post: 09-29-2003, 02:24 AM