Binary Tree Representations

Only available on StudyMode
  • Download(s) : 297
  • Published : October 29, 2010
Open Document
Text Preview
#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 *btree; // declaration of a new type of tree structure

/* ------------------------------------------------------------------------- */ // 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("\nPlease input the elements of binary tree (Exit for 0):\n"); 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);
}

/*...
tracking img