CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Binary Tree

  1. #1
    Join Date
    Feb 2015
    Posts
    20

    Binary Tree

    Hello everyone, I've been having problems running this program and i cant figure out the problem. its just a simple program to read a mixed file that has number 1-100000 and sort them in a binary tree.I'm not getting any errors the program is just getting stuck at debug mode and not running properly. just a note there is a second program that generates the file that this program is getting the data from, i didn't post it since it seems to be running fine and doing what its suppose to.


    Header:
    Code:
    class Node{
    	public: int key;
    			int sort_key;
    			Node *left;
    			Node *right;
    			Node(int key1,int key2){
    				this->sort_key=key1;
    				this->key=key2;
    				this->left=NULL;
    				this->right=NULL;
    			}
    };
    
    class BST{
    	public:
    		Node *root;
    		BST(){
    			root=NULL;
    		}
    		
    		void insert(int sort_key, int key)
    		{
    			Node *x=root;
    			Node *y=NULL;
    			Node *newNode = new Node(sort_key,key);
    			if(this->root==NULL)
    			{
    				this->root=newNode;
    				return;
    			}
    			while(x!=NULL)
    			{
    				y=x;
    				if(sort_key<x->sort_key)
    					x=x->left;	
    				else
    					x=x->right;	
    			}
    			if(sort_key<y->sort_key)
    					y->left=newNode;	
    				else
    					y->right=newNode;	
    		}
    		
    		int findDepth(Node *node){
    			if(node==NULL)
    				return -1;
    			int ldepth = findDepth(node->left);
    			int	rdepth = findDepth(node->right);			
    			if(ldepth>rdepth) 
    				return ldepth+1 ;
    			else
    				return rdepth+1;	
    		}
    		
    		int findDepthofNode(int key){
    			Node *temp=this->root;
    			int depth=0;
    			while(temp->sort_key!=key)
    			{
    				if(key<temp->sort_key)
    					temp=temp->left;	
    				else
    					temp=temp->right;	
    				depth++;		
    			}
    			return depth;
    		}
    		
    		int numOfEndNodesWithSecondNum(Node *node, int num)
    		{
    			if(node==NULL)
    				return 0;
    			if(node->left==NULL && node->right==NULL && node->key==num)
    				return 1;				
    			int x =	numOfEndNodesWithSecondNum(node->left, num);	
    			int y =	numOfEndNodesWithSecondNum(node->right, num);
    			return x+y;
    		}
    		
    		int numOfOneChildNodesWithSecondNum(Node *node, int num)
    		{
    			if(node==NULL)
    				return 0;
    			int x=numOfOneChildNodesWithSecondNum(node->left,num);
    			int y=numOfOneChildNodesWithSecondNum(node->right,num);	 
    			if( (node->left==NULL && node->right!=NULL && node->key==num) || 
    				(node->left!=NULL && node->right==NULL && node->key==num)	)
    				return x+y+1;
    			else
    				return x+y;	
    		}
    		
    		int numOfTwoChildNodesWithSecondNum(Node *node, int num){
    			if(node==NULL)
    				return 0;
    			int x=numOfTwoChildNodesWithSecondNum(node->left,num);
    			int y=numOfTwoChildNodesWithSecondNum(node->right,num);
    			if( node->left!=NULL && node->right!=NULL && node->key==num)
    				return x+y+1;
    			else
    				return x+y;		 
    		}	
    		
    };
    code:
    Code:
    #include <sstream>
    #include <string>
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <time.h>
    #include <fstream>
    #include "BST.h"
    //#include "writefile.cpp"
    using namespace std;
    
    //extern int writefile();
    int main()
    {
        int i =0;
        string number;
        int num_count=500;
        int random;
        BST bstree;
        //srand(time(NULL));    
    	int *inputnumbers = new int[num_count];    	
    	//writefile();
    	ifstream input("mixed.dat");
    	//cout<<"done";
    	string temp;            
    	while ( getline(input ,number)) 
    	{
            istringstream token(number);
            while ( token>>temp) {
    		    inputnumbers[i] = atoi(temp.c_str());
                //cout<<setw(5)<<inputnumbers[i]; 
                
    			i++;
    		}
    		
    	}
    	input.close();
        cout<<"Data read from input file..."<<endl;
        for(i=0;i<num_count;i++)
        {
        	random  = rand() % 100 +  1;
        	bstree.insert(inputnumbers[i],random);
    	}
    	cout<<"Binary tree created..."<<endl;
        cout<<"Depth of deepest node in tree is : "<<bstree.findDepth(bstree.root)<<endl;    
    
        float avg_depth=0;
        for(i=0;i<5000;i++)
        {
        	random  = rand() % num_count +  1;
        	avg_depth+=bstree.findDepthofNode(random);
        }
        avg_depth/=5000;
        cout<<"Average depth for searching a key is : "<<avg_depth<<endl;
        
        cout<<"Number of end nodes with value 50 is: "<<bstree.numOfEndNodesWithSecondNum(bstree.root,50)<<endl;
        cout<<"Number of one child nodes with value 50 is: "<<bstree.numOfOneChildNodesWithSecondNum(bstree.root,50)<<endl;
        cout<<"Number of two child nodes with value 50 is: "<<bstree.numOfTwoChildNodesWithSecondNum(bstree.root,50)<<endl;
    }

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Binary Tree

    I'm not getting any errors the program is just getting stuck at debug mode
    Then it seems you have a problem using the debugger as you should be able to use the debugger to step through the code to see where the program deviates from that expected from the design. What version of the compiler are you using - and how are you using the debugger?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Feb 2015
    Posts
    20

    Re: Binary Tree

    Quote Originally Posted by 2kaud View Post
    Then it seems you have a problem using the debugger as you should be able to use the debugger to step through the code to see where the program deviates from that expected from the design. What version of the compiler are you using - and how are you using the debugger?

    I use MVS 2013. i kept running in an error LNK2019 in the program, commenting out some of the code as shown stopped that problem. The biggest issue now the program isn't running so i have no idea if it is doing what it's suppose too or not, and i don't know where to start...

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Binary Tree

    You're contradicting yourself. LNK2019 means the linker is unable to find everything it needs to build an executable program. With that error you'd never be able to debug or run it. What's really going on? If you have a link error, find it and fix it. If you have a problem when the program is running, use the debugger to track it down.

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Binary Tree

    LNk2019 is a linker error stating that it can't find an external symbol. See https://msdn.microsoft.com/en-us/library/799kze2z.aspx

    What do you mean that the program 'isn't running'. If the program has compiled and linked successfully and generated an .exe file then if it has been compiled as a 'debug build' then it should be possible to trace through the program using the debugger. If the program doesn't compile and link successfully then post the compile/linker errors.

    To trace through the program using the debugger, in the code window that contains int main(), select this line and press F9 to set a breakpoint. Then press F5 to start debugging the program. The program should stop on the main() line and show various debug windows. Then press either F10 to execute one statement without tracing through any code involved in that statement or press F11 to execute one statement and trace through the code involved in that statement eg if the statement involves a function then F10 will step over the function code but F11 will step into the function. Keep pressing F10/F11 as required to step through the code. If you know that a section of code is working, then select the line to set another breakpoint and press F9 and then F5 and the code will then execute to that set breakpoint. By setting/un-setting breakpoints (F9) and using F10/F11 and F5 to trace through the program and by watching the values of the variables then you can see what your program is doing and where it is deviating from that expected from the design.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Feb 2015
    Posts
    20

    Re: Binary Tree

    You're contradicting yourself. LNK2019 means the linker is unable to find everything it needs to build an executable program. With that error you'd never be able to debug or run it. What's really going on? If you have a link error, find it and fix it. If you have a problem when the program is running, use the debugger to track it down.
    LNK2019 is not present in the posted program as I managed to fix that error.
    To trace through the program using the debugger, in the code window that contains int main(), select this line and press F9 to set a breakpoint. Then press F5 to start debugging the program. The program should stop on the main() line and show various debug windows. Then press either F10 to execute one statement without tracing through any code involved in that statement or press F11 to execute one statement and trace through the code involved in that statement eg if the statement involves a function then F10 will step over the function code but F11 will step into the function. Keep pressing F10/F11 as required to step through the code. If you know that a section of code is working, then select the line to set another breakpoint and press F9 and then F5 and the code will then execute to that set breakpoint. By setting/un-setting breakpoints (F9) and using F10/F11 and F5 to trace through the program and by watching the values of the variables then you can see what your program is doing and where it is deviating from that expected from the design.
    I will try this to debug the program.

    My issue is we never went over how to use the debugger in class, and I tried to do some research on my own but without much luck. I will follow 2kaud instructions and go from there.

  7. #7
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Binary Tree

    My issue is we never went over how to use the debugger in class
    Black mark to the teacher! Now is a good time to learn. If you're coding in c/c++ you really need to know how to use the debugger.

    For the basics of using the debugger, have a look at https://msdn.microsoft.com/en-us/library/k0k771bt.aspx and associated links. The examples are for c# code but the way you use the debugger is the same for c++.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured