[Data Structure] Creation and Traverse of Binary Trees

#include #include #include #include #define OK 1#define ERROR 0#define TRUE 1#define FALSE 0#define MAXSIZE 100 /* Initial allocation of storage space*/typedef int Status;typedef char TElemType; /* Assuming that the elements of the binary tree are all character types*/TElemType Nil = '';typedef struct BiTNode{ TElemType data ; struct BiTNode *lchild, *rchild;}BiTNode, *BiTree;/* Simulate the generation of char array, which is used to construct the data produced by the binary tree ******************/int index$ = 1;typedef char String[24];String str;/* Use String structure to represent chars*/Status StrAssign(String S, char *chars){ int i; if (strlen(chars)> MAXSIZE) {return ERROR; // Exceeds the maximum capacity} else {// The length of the string stored in the first position S[0] = strlen(chars); for (i = 1; i data = ch; // generate root node CreateBiTree(&(*T)->lchild ); // Construct the left subtree CreateBiTree(&(*T)->rchild); // Construct the right subtree))/* Preorder Traverse*/void PreOrderTraverse(BiTree *T){ if (*T == NULL ) return; printf("%c ", (*T)->data); PreOrderTraverse(&(*T)->lchild); PreOrderTraverse(&(*T)->rchild);}int main() {BiTree T; InitBiTree(&T); // Construct a string StrAssign(str, "ABDH#K###E##CFI###G#J##"); // Construct a binary tree CreateBiTree(&T); PreOrderTraverse(&T); return 0;}

Leave a Comment

Your email address will not be published.