Solved

Binary Tree

Posted on 2011-03-04
4
447 Views
Last Modified: 2012-05-11
I'm finding it hard do debug the attached code that constructs a binary search tree.
Please let me know what I am missing.

Thanks!
treeNode* constructBinaryTree()
{
	treeNode **root = new treeNode*;
	treeNode *current = new treeNode;
	//root = NULL;
	//current = NULL;
	int data = 0;

	while( data!= -1)
	{
		printf("\n\nEnter a value (Enter -1 to stop): ");
		scanf("%d", &data);

		treeNode *newNode = new treeNode;
		newNode->left = NULL;
		newNode->right = NULL;
		newNode->data = data;

		if(!current)
		{
			*root = newNode;
		}
		else
		{
			current = *root;
			while(current->left || current->right)
			{
				if(data<=current->data)
					current = current->left;
				else 
					current = current->right;
			}
			if(data<=current->data)
					current->left = newNode;
			else 
				current->right = newNode;
		}
	}

	return *root;
}

Open in new window

#include <iostream>
#include <conio.h>
#pragma once

typedef struct treeNode
{
	struct treeNode *left;
	struct treeNode *right;
	int data;
} treeNode;

Open in new window

0
Comment
Question by:dshrenik
  • 3
4 Comments
 
LVL 13

Accepted Solution

by:
Superdave earned 500 total points
ID: 35041676
I might be missing something, but I don't see why root needs to be doubly-deferred; it could probably just be treeNode *.

I think you need to uncomment your initializing your variables to NULL, and get rid of the new's in the declarations.  Each time you read in a value in the while loop, you want to create a node for it, which you are doing.  There will be no other nodes other than the values you read in, so you are already creating all the nodes you need without the initializers.

You need to move your line 25 (current = *root;) before the test (!current).  You only want to do this only once, the first time, when you have not yet assigned a root node, the rest of the time you will start searching from the root.  As I said previously, I think this could be "current=root" if root is declared differently (otherwise you'd need to initialize *root to be NULL rather than root).

      
0
 

Author Comment

by:dshrenik
ID: 35041697
Thank you!

Can you tell me why I must not initialize them to NULL, and why I must not use "new" to declare them?

It is working better now, but not for all inputs. When I enter 3, 4, 5, -1 it creates the tree without any problems.

When I enter 3, 4, 1, then I get an exception : "Access violation reading location 0x00000000."

0
 

Author Comment

by:dshrenik
ID: 35041707
My code now looks like this:
0
 

Author Comment

by:dshrenik
ID: 35041719
This code now works:

Thanks!
#include <iostream>
#include <conio.h>
#include "treeNode.h"
#pragma once

treeNode* constructBinaryTree()
{
	treeNode *root = new treeNode;
	treeNode *current = new treeNode;
	treeNode *previous = new treeNode;
	root = NULL;
	current = NULL;
	previous = NULL;
	int data;

	while(1)
	{
		printf("\n\nEnter a value (Enter -1 to stop): ");
		scanf("%d", &data);
		if(data==-1)
			break;

		treeNode *newNode = new treeNode;
		newNode->left = NULL;
		newNode->right = NULL;
		newNode->data = data;

		current = root;
		previous = root;
		
		if(!current)
		{
			root = newNode;
		}
		else
		{			
			while(current)
			{
				previous = current;

				if(data <= current->data)
					current = current->left;
				else 
					current = current->right;
			}
			if(data<=previous->data)
					previous->left = newNode;
			else 
				previous->right = newNode;
		}
	}

	return root;
}

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now