Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

binary tree representation using linked list

simon_chansimon_chan Member Posts: 2
I'v got some problems in the following code, if any one would help solve it, i'll be very grateful.

#include
#include

/* ------------------------------------------------------------------------- */
// global variable declaration

struct tree // definition of a tree structure
{
struct tree *left; // pointer pointed to left child node
int data; // data value of current node
struct tree *right; // pointer pointed to right child node
};
typedef struct tree treeNode; // declaration of a new type of tree structure
treeNode *bTree; // declaration of linked list binary tree

/* ------------------------------------------------------------------------- */
// declaration of prototype function that will be used in the following program
bTree Insert_Node(bTree, int); // insert node of binary tree
bTree Create_Btree(int *, int); // create binary tree
void Print_Btree(bTree ); // print a created binary tree

/* ------------------------------------------------------------------------- */
/* main program: input array element, set up a linked list type of binary */
/* tree and print the binary tree */
/* ------------------------------------------------------------------------- */
void main(void)
{
bTree root = NULL; // pointer of tree root node

int i,index; // variable for loop count and length of array
int value; // variable used to store input data temporarily
int nodeList[20]; // array to store input data

printf("
Please input the elements of binary tree (Exit for 0):
");
index = 0;

/* --------------------------------------------------------------------- */
// read and reserve input data into array nodeList
scanf("%d",&value);
while(value != 0) // read 'til a '0' is input
{
nodeList[index] = value;
index++;
scanf("%d",&value);
}

/* --------------------------------------------------------------------- */
// create a binary tree using linked list
root = Create_Btree(nodeList,index);

// --------------------------------------------------------------------- */
// print the content of created binary tree above
Print_Btree(root);
}

/* ------------------------------------------------------------------------- */
/* create a binary tree by adding new node */
/* ------------------------------------------------------------------------- */
bTree Create_Btree(int *data, int len)
{
bTree root = NULL; // pointer of root node
int i;

for(i=0;idata = data; // reserve into new node
newNode->right = NULL; // set the initial value of right pointer
newNode->left = NULL; // set the initial value of left pointer

if(root = NULL)
return newNode; // return with the position of new node and this
// will always be the root node of the binary tree
else
{
currentNode = root; // store the current node pointer
while(currentNode != NULL)
{
parentNode = currentNode; // store parent node pointer
if(currentNode->data > data) // compare the node data
currentNode = currentNode->left; // left child node
else
currentNode = currentNode->right; // right child node
}

if(parentNode->data > data) // link parent node with child node
parentNode->left = newNode; // child node as left child tree
else
parentNode->right = newNode; // child node as right child tree
}
return root;
}

/* ------------------------------------------------------------------------- */
/* print the binary tree */
/* ------------------------------------------------------------------------- */
void Print_Btree(bTree root)
{
bTree pointer;

pointer = root->left;
printf("Print left subtree node of root:
");
while(pointer != NULL)
{
printf("[%2d]
",pointer->data);
pointer = pointer->left;
}

pointer = root->right;
printf("Print right subtree node of root:
");
while(pointer != NULL)
{
printf("[%2d]
",pointer->data);
pointer = pointer->right;
}
}


Building result:
1>------ Build started: Project: Tree_03, Configuration: Debug Win32 ------
1>Compiling...
1>Tree_03.c
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(26) : error C2061: syntax error : identifier 'Insert_Node'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(26) : error C2059: syntax error : ';'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(26) : error C2143: syntax error : missing ')' before ','
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(26) : error C2040: 'bTree' : 'int' differs in levels of indirection from 'treeNode *'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(26) : error C2059: syntax error : 'type'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(26) : error C2059: syntax error : ')'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(27) : error C2061: syntax error : identifier 'Create_Btree'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(27) : error C2059: syntax error : ';'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(27) : error C2059: syntax error : 'type'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(36) : error C2146: syntax error : missing ';' before identifier 'root'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(36) : error C2065: 'root' : undeclared identifier
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(36) : warning C4047: '=' : 'int' differs in levels of indirection from 'void *'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(38) : error C2143: syntax error : missing ';' before 'type'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(39) : error C2143: syntax error : missing ';' before 'type'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(40) : error C2143: syntax error : missing ';' before 'type'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(43) : error C2065: 'index' : undeclared identifier
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(47) : error C2065: 'value' : undeclared identifier
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(50) : error C2065: 'nodeList' : undeclared identifier
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(50) : error C2109: subscript requires array or pointer type
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(57) : warning C4013: 'Create_Btree' undefined; assuming extern returning int
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(67) : error C2061: syntax error : identifier 'Create_Btree'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(67) : error C2059: syntax error : ';'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(67) : error C2059: syntax error : 'type'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(81) : error C2061: syntax error : identifier 'Insert_Node'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(81) : error C2059: syntax error : ';'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(81) : error C2146: syntax error : missing ')' before identifier 'root'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(81) : error C2061: syntax error : identifier 'root'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(81) : error C2059: syntax error : ','
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(81) : error C2059: syntax error : ')'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(120) : error C2146: syntax error : missing ')' before identifier 'root'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(120) : error C2061: syntax error : identifier 'root'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(120) : error C2059: syntax error : ';'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(120) : error C2059: syntax error : ')'
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(121) : error C2449: found '{' at file scope (missing function header?)
1>d:datastructuresandalgorithmsincchap07_basictreestructure ree_03 ree_03.c(139) : error C2059: syntax error : '}'
1>Build log was saved at "file://d:DataStructuresandAlgorithmsInCChap07_BasicTreeStructureTree_03DebugBuildLog.htm"
1>Tree_03 - 33 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Comments

  • Malcolm_McLeanMalcolm_McLean Member Posts: 254 ✭✭

    : struct tree // definition of a tree structure
    : {
    : struct tree *left; // pointer pointed to left child node
    : int data; // data value of current node
    : struct tree *right; // pointer pointed to right child node
    : };
    : typedef struct tree treeNode; // declaration of a new type of tree
    : structure
    : treeNode *bTree; // declaration of linked list binary
    : tree

    Ypu mean typedef treenode *bTree;
  • simon_chansimon_chan Member Posts: 2
    :
    : : struct tree // definition of a tree structure
    : : {
    : : struct tree *left; // pointer pointed to left child node
    : : int data; // data value of current node
    : : struct tree *right; // pointer pointed to right child node
    : : };
    : : typedef struct tree treeNode; // declaration of a new type of tree
    : : structure
    : : treeNode *bTree; // declaration of linked list binary
    : : tree
    :
    : Ypu mean typedef treenode *bTree;
    :
    typedef treeNode *bTree. yeah, that's it! I've got it, tks very much!
Sign In or Register to comment.