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;

// binary tree creation

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 > current->data)

back->left = newnode;

else

back->right = newnode;

}

return root;

}

void inorder(btree root)

{

if(root != NULL)

{

inorder(root->left);

printf("\2: %d\n",root->data);

inorder(root->right);

}

}

void main(void)

{

int arr[] = { 7,4,1,5,12,8,13,11};

btree ptr;

int val,i;

ptr= NULL; //initial the root pointer

printf("\1: Create binary tree for folllowing value.\n");

for(i=0;i<8;i++);

{

ptr = create_btree(ptr,arr[i]);

printf("\2: %d\n",arr[i]);

}

printf("\1: Using inorder to print the binary tree.\n");

inorder(ptr);

}

any pro can help me here?

#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;

// binary tree creation

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 > current->data)

back->left = newnode;

else

back->right = newnode;

}

return root;

}

void inorder(btree root)

{

if(root != NULL)

{

inorder(root->left);

printf("\2: %d\n",root->data);

inorder(root->right);

}

}

void main(void)

{

int arr[] = { 7,4,1,5,12,8,13,11};

btree ptr;

int val,i;

ptr= NULL; //initial the root pointer

printf("\1: Create binary tree for folllowing value.\n");

for(i=0;i<8;i++);

{

ptr = create_btree(ptr,arr[i]);

printf("\2: %d\n",arr[i]);

}

printf("\1: Using inorder to print the binary tree.\n");

inorder(ptr);

}

any pro can help me here?

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

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

ohh issit?i go check it out later...

but i read some c++ code on binary tree like almost the same

so i donno how to make it correct.

but i read some c++ code on binary tree like almost the same

so i donno how to make it correct.

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=.=

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=.=

:D i just found that i did a super lousy mistake,

i finally success compile this program.^^

i finally success compile this program.^^

Topic archived. No new replies allowed.