易采站长站为您分析使用C语言使用C语言构建基本的二叉树数据结构,包括根据前序序列和中序序列构建二叉树的方法,需要的朋友可以参考下
二叉树结构常用的一些初始化代码
- #include #include
- typedef struct Node{
- int data; Node *leftchild;
- Node *rightchild; }Node;
- /* 初始化一棵二叉树排序树。
- */ void InitBinaryTree(Node**root,int elem)
- { *root=(Node*)malloc(sizeof(Node));
- if(!(*root)) {
- printf("Memory allocation for root failed.n"); return;
- } (*root)->data=elem;
- (*root)->leftchild=NULL; (*root)->rightchild=NULL;
- }
- /* 向二叉树排序树中插入节点。
- */ void InsertNode(Node *root,int elem)
- { Node *newnode=NULL;
- Node *p=root,*last_p=NULL;
- newnode=(Node*)malloc(sizeof(Node)); if(!newnode)
- { printf("Memory allocation for newnode failed.n");
- return; }
- newnode->data=elem; newnode->leftchild=NULL;
- newnode->rightchild=NULL;
- while(NULL!=p) {
- last_p=p; if(newnode->datadata)
- { p=p->leftchild;
- } else if(newnode->data>p->data)
- { p=p->rightchild;
- } else
- { printf("Node to be inserted has existed.n");
- free(newnode); return;
- } }
- p=last_p; if(newnode->datadata)
- { p->leftchild=newnode;
- } else
- { p->rightchild=newnode;
- } }
- /*
- 创建一棵二叉树排序树。 */
- void CreatBinarySearchTree(Node **root,int data[],int num) {










