Solved

Binary Tree

Posted on 2011-03-04
4
449 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

813 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