Solved

Binary Tree

Posted on 2011-03-04
4
451 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

724 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