Binary tree problem

This is a discussion on Binary tree problem within the C++ Programming forums, part of the General Programming Boards category; I've been trying to code my own binary tree (without looking at any examples), but for some reason my code ...

  1. #1
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    Posts
    56

    Angry Binary tree problem

    I've been trying to code my own binary tree (without looking at any examples), but for some reason my code just refuses to compile! here is my source:

    Code:
    #include <iostream.h>
    
    struct node
    	{
    	node *right;
    	node *left;
    	int key;
    	};
    typedef node* NodePtr;
    
    void add_node(NodePtr &tree, int value, int switch);
    	
    main()
    	{
    	int num, num_of_nodes;
    	NodePtr root, pointer; int called = 0;
    
    	cout << "How many numbers do you whish to add to the tree?";
    	cin >> num_of_nodes;
    	cout << "Enter the first node:";
    	cin >> num;
    	root->key = num;
    	pointer = root;
    	for (int i = 0; i < num_of_nodes; i++)
    		{
    		cout << "Enter a number to add:";
    		cin >> num;
    		add_node(pointer, num, called);
    		pointer = root;
    		called = 0;
    		} 
    	
    	return 0;
    	}
    void add_node(NodePtr &tree, int value, int switch)
    	{
    	if (switch == 0)
    		{
    		node* tmp = new node;   
    		tmp->key = value;  
    		switch = 1;  
    		}
    	if (value >= tree->key)    
    	    if (tree->right != NULL)
    		{
    		tree = tree->right;
    		add_node(tree, value);
    		}
    	    else
    		tree->right = tmp;
    
    	if (value <= tree->key)    
    	    if (tree->left != NULL)
    		{
    		tree = tree->left;
    		add_node(tree, value);
    		}
    	    else
    		tree->left = tmp;
    			
    	}
    It all seems to check out fine, yet I get the following errors from g++:
    Code:
    binary_tree.cpp:11: parse error before `switch'
    binary_tree.cpp:35: parse error before `switch'
    binary_tree.cpp: In function `void add_node(...)':
    binary_tree.cpp:37: parse error before `switch'
    binary_tree.cpp:40: `tmp' undeclared (first use this function)
    binary_tree.cpp:40: (Each undeclared identifier is reported only once for each
       function it appears in.)
    binary_tree.cpp:40: `value' undeclared (first use this function)
    binary_tree.cpp:41: parse error before `=' token
    binary_tree.cpp: At global scope:
    binary_tree.cpp:47: `tree' was not declared in this scope
    binary_tree.cpp:47: ISO C++ forbids declaration of `add_node' with no type
    binary_tree.cpp:47: `int add_node' redeclared as different kind of symbol
    binary_tree.cpp:36: previous declaration of `void add_node(...)'
    binary_tree.cpp:47: initializer list being treated as compound expression
    binary_tree.cpp:48: parse error before `}' token
    binary_tree.cpp:56: `tree' was not declared in this scope
    binary_tree.cpp:56: ISO C++ forbids declaration of `add_node' with no type
    binary_tree.cpp:56: redefinition of `int add_node'
    binary_tree.cpp:47: `int add_node' previously defined here
    binary_tree.cpp:56: initializer list being treated as compound expression
    binary_tree.cpp:57: parse error before `}' token
    WHY!?
    I am Error. When all else fails, use fire.

    My Current Screenshot

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    switch is a reserved keyword. That's like naming a variable if or else or return.

  3. #3
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    Posts
    56
    Originally posted by jdinger
    switch is a reserved keyword. That's like naming a variable if or else or return.
    Silly me. I just wasn't paying attention!
    I am Error. When all else fails, use fire.

    My Current Screenshot

  4. #4
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i'm not sure what tmp should be declared as so u have to fix that. Your add_node function takes 3 params, u only had the first two so i NULL'd the third, declare tmp and it should work i think.

    Code:
    #include <iostream.h>
    
    struct node
    	{
    	node *right;
    	node *left;
    	int key;
    	};
    typedef node* NodePtr;
    
    void add_node(NodePtr &tree, int value, int switch1);
    	
    main()
    	{
    	int num, num_of_nodes;
    	NodePtr root, pointer; int called = 0;
    
    	cout << "How many numbers do you whish to add to the tree?";
    	cin >> num_of_nodes;
    	cout << "Enter the first node:";
    	cin >> num;
    	root->key = num;
    	pointer = root;
    	for (int i = 0; i < num_of_nodes; i++)
    		{
    		cout << "Enter a number to add:";
    		cin >> num;
    		add_node(pointer, num, called);
    		pointer = root;
    		called = 0;
    		} 
    	
    	return 0;
    	}
    void add_node(NodePtr &tree, int value, int switch1)
    	{
    	if (switch1 == 0)
    		{
    		node* tmp = new node;   
    		tmp->key = value;  
    		switch1 = 1;  
    		}
    	if (value >= tree->key)    
    	    if (tree->right != NULL)
    		{
    		tree = tree->right;
    		add_node(tree, value, NULL);
    		}
    	    else
    		tree->right = tmp;
    
    	if (value <= tree->key)    
    	    if (tree->left != NULL)
    		{
    		tree = tree->left;
    		add_node(tree, value,NULL);
    		}
    	    else
    		tree->left = tmp;
    			
    	}

  5. #5
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    Posts
    56
    Ok... I got the following program worked out... works nicely too, IMHO.

    Code:
    #include <iostream>
    using namespace std;
    
    struct node
    	{
    	node *right;
    	node *left;
    	int key;
    	};
    typedef node* NodePtr;
    
    void add_node(NodePtr &tree, NodePtr &tmp);
    
    void print_inorder(NodePtr p) 
    	{
        	if (p != NULL) 
    		{
           		print_inorder(p->left);  // print left subtree
            	                cout << p->key << endl; // print this node
            	                print_inorder(p->right); // print right subtree
        		}
    	}
    	
    main()
    	{
    	int num, num_of_nodes;
    	NodePtr root, pointer; 
    
    	root = new node;
    	pointer = new node;
    
    	cout << "How many numbers do you whish to add to the tree?";
    	cin >> num_of_nodes;
    
    	cout << "Enter the first node:";
    	cin >> num;
    	root->key = num;
    	pointer = root;
    	for (int i = 0; i < num_of_nodes-1; i++)
    		{
    		cout << "Enter a number to add:";
    		cin >> num;
    		node* tmp = new node;   
    		tmp->key = num; 
    		add_node(pointer, tmp);
    		pointer = root;
    		} 
    	cout << "The numbers entered in order is:" << endl;
    	print_inorder(root);
    	return 0;
    	}
    void add_node(NodePtr &tree, NodePtr &tmp)
    	{ 
    
    	if (tmp->key >= tree->key)    
    	    if (tree->right != NULL)
    		{
    		tree = tree->right;
    		add_node(tree, tmp);
    		}
    	    else
    		tree->right = tmp;
    
    	if (tmp->key <= tree->key)    
    	    if (tree->left != NULL)
    		{
    		tree = tree->left;
    		add_node(tree, tmp);
    		}
    	    else
    		tree->left = tmp;
    			
    	}
    Please telll me if there are some things I can improve on in this!

    EDIT: oh.. as for tmp, I decided it only needed to be declared once, so I left it out of the function. The print function prints the numbers out in a sorted fashion, and I've tested a modification using a random number generator. For some odd reason, whenever I use 5 or more nodes, it hangs. Maybe its the way I use random numbers, but it couldbe that I have too many things running. Let me know if anyone has any ideas, and could point out other things I coudl do with this program.
    Last edited by carrja99; 02-28-2003 at 09:49 AM.
    I am Error. When all else fails, use fire.

    My Current Screenshot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  2. binary tree problem
    By spank in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 06:27 AM
  3. problem in storing data in a binary search tree
    By alavardi in forum C Programming
    Replies: 5
    Last Post: 02-13-2005, 03:20 PM
  4. Tree Problem
    By recluse in forum C Programming
    Replies: 36
    Last Post: 12-04-2004, 03:06 PM
  5. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 10:07 PM

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