im using book of borland C++ which available in C++ by hung Jinn Kwei to learn C++ in Visual C++ 2008 Express Edition.during my compiling,the output show unexpected value -858993460.below is the code(sorry if printf,%d isnt famous):
#include "stdafx.h"
// ================== Program Description ===================
// program name: ch18.1.c
// binary creation and inorder print the tree
// ==========================================================
#include <stdlib.h>
struct list
{
int data;
struct list *left;
struct list *right;
};
typedef struct list node; //or typedef struct list *btree
typedef node *btree;
It looks like you are using current after it is null inside your build tree method:
if (back->data > current->data)
This condition is following a while (current) block, which would result in current->data being undefined. You probably meant to compare back->data with new->data
now i have my create_btree function as following,but still failed for the same output of -858993460.
btree create_btree(btree root,int val)
{
btree newnode,current,back;
newnode = (btree)malloc(sizeof(node));
newnode->data = val;
newnode->left = NULL;
newnode->right = NULL;
if(root == NULL) //insert root node
{
root = newnode;
return root;
}
else //insert other node
{
current = root;
while (current)
{
back = current;
if(current->data > val)
current = current->left;
else
current = current->right;
}
if(back->data > newnode->data)
back->left = newnode;
else
back->right = newnode;
}
return root;
}
any suggestion?i have no idea where i have been wrong=.=