[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Binary Tree

Posted on 2011-03-04
4
Medium Priority
?
459 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 2000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

649 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