使用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)
{
 int i;

 for(i=0;i
 {
 if(NULL==*root)
 {
 InitBinaryTree(root,data[i]);
 }
 else
 {
 InsertNode(*root,data[i]);
 }
 }
}

根据前序序列、中序序列构建二叉树
函数定义

 bt rebuildTree(char *pre, char *in, int len); 

参数:
* pre:前序遍历结果的字符串数组
* in:中序遍历结果的字符串数组
len : 树的长度
例如:
前序遍历结果: a b c d e f g h
中序遍历结果: c b e d f a g h

算法思想

  •     递归思想,递归的终止条件是树的长度len == 0
  •     在中序遍历的数组中找到前序数组的第一个字符,记录在中序数组中的位置index.如果找不到,说明前序遍历数组和中序遍历数组有问题,提示错误信息,退出程序即可;找到index后,新建一个二叉树节点t,t->item = *pre,然后递归的求t的左孩子和有孩子
  •     递归的左孩子:void rebuildTree(pre + 1, in, index)
  •     递归的右孩子:void rebuildTree(pre + (index + 1), in + (index + 1), len - (index + 1))

实现代码(c语言版)

  

 /**
  * Description:根据前序和中序构建二叉树
  */
 bt rebuildTree(char *pre, char *in, int len)
 {
  bt t;
  if(len <= 0)
  {
   //递归终止
   t = NULL;
  }else
  {
   //递归主体
   int index = 0; 

   while(index < len && *(pre) != *(in + index))
   {
    index ++;
   } 

   if(index >= len)
   {
    printf("前序遍历或者中序遍历数组有问题!\n");
    exit(-1);
   } 

   t = (struct bintree *)malloc(sizeof(struct bintree));
   t->item = *pre;
   t->lchild = rebuildTree(pre + 1, in, index);
   t->rchild = rebuildTree(pre + (index + 1), in + (index + 1), len - (index + 1));
  }
  return t;
 }

根据中序序列、后序序列构建二叉树
函数定义

 /**
  * 中序、后序序列构建二叉树
  */
 btree* rebuildTree(char *order, char *post, int len); 

算法思想
中序序列:C、B、E、D、F、A、H、G、J、I

后序序列:C、E、F、D、B、H、J、I、G、A

递归思路:

  •     根据后序遍历的特点,知道后序遍历最后一个节点为根节点,即为A
  •     观察中序遍历,A左侧CBEDF为A左子树节点,A后侧HGJI为A右子树节点
  •     然后递归的构建A的左子树和后子树

实现代码(c代码)

 /**
  * 根据中序和后序序列构建二叉树
  * (ps:昨晚参加阿里笔试,等到最后说可以免笔试直接面试,今天估计还是要根据学校筛选,哈哈,为了这点
  * 也得参加阿里笔试,不能让自己的学校受到鄙视)
  */ 

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h> 

 int n; 

 typedef struct btree {
  struct btree *lchild;
  struct btree *rchild;
  char data;
 } btree; 

 /**
  * 中序、后序序列构建二叉树
  */
 btree* rebuildTree(char *order, char *post, int len)
 {
  btree *t; 

  if (len <= 0) {
   return NULL;
  } else {
   int index = 0; 

   while (index < len && *(post + len - 1) != *(order + index)) {
    index ++;
   }  

   t = (btree *)malloc(sizeof(btree));
   t->data = *(order + index);
   t->lchild = rebuildTree(order, post, index);
   t->rchild = rebuildTree(order + index + 1, post + index, len - (index + 1));
  } 

  return t;
 } 

 /**
  * 前序遍历二叉树
  */
 void preTraverse(btree *t)
 {
  if (t) {
   printf("%c ", t->data);
   preTraverse(t->lchild);
   preTraverse(t->rchild);
  }
 } 

 int main(void)
 {
  int i;
  char *post, *order;
  btree *t; 

  while (scanf("%d", &n) != EOF) {
   post = (char *)malloc(n);
   order = (char *)malloc(n); 

   getchar();
   for (i = 0; i < n; i ++)
    scanf("%c", order + i); 

   getchar();
   for (i = 0; i < n; i ++)
    scanf("%c", post + i); 

   t = rebuildTree(order, post, n); 

   preTraverse(t);
   printf("\n"); 

   free(post);
   free(order); 

  } 

  return 0;
 } 

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索c语言
二叉树
c语言构建二叉树、二叉树 c语言、c语言创建二叉树、c语言二叉树的建立、c语言二叉树遍历,以便于您获取更多的相关知识。

时间: 2024-07-30 00:58:47

使用C语言构建基本的二叉树数据结构_C 语言的相关文章

C++非递归建立二叉树实例_C 语言

本文实例讲述了C++非递归建立二叉树的方法.分享给大家供大家参考.具体分析如下: 思路: 设置一个标记变量flag并初始化为1. flag = 1表示现在需要创建当前结点的左孩子,2表示需要创建右孩子,3则表示当前结点的左右孩子都已经创建完毕,需要执行出栈操作,直到当前结点不是父结点的右孩子为止. 以先序创建如图所示二杈树: 实现代码: PBTree create() { char ch[20]; scanf("%s",ch); int len = strlen(ch); PBTree

C语言对栈的实现基本操作_C 语言

c语言中栈是一种数据结构,后进先出,即最后进入栈的数据最先弹出.c语言中没有栈这种数据类型,需要自己编程构建.下面我们就一起来了解一下c语言中栈的基本操作. C语言对栈的实现基本操作,操作如下: #include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <stdbool.h> typedef struct Node { int data; struct Node * pNext;

C语言单链表常见操作汇总_C 语言

C语言的单链表是常用的数据结构之一,本文总结了单链表的常见操作,实例如下: #include<stdio.h> #include<stdlib.h> //定义单链表结构体 typedef int ElemType; typedef struct Node { ElemType data; struct Node *next; }LNode,*LinkList; //创建单链表 void Build(LinkList L) { int n; LinkList p,q; p=L; pr

C语言实现顺序表基本操作汇总_C 语言

本文汇总了C语言下实现及操作顺序表的方法,对于学习数据结构的朋友来说是一个不错的参考程序.完整代码如下: #include<stdio.h> #include<stdlib.h> #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 typedef int status ;

C语言入门之指针用法教程_C 语言

本文针对C语言初学者详细讲述了指针的用法,并配以实例进行说明.具体分析如下: 对于C语言初学者来说,需要明白指针是啥?重点就在一个"指"上.指啥?指的地址.啥地址?内存的地址. 上面说明就是指针的本质了. 这里再详细解释下.数据存起来是要存在内存里面的,就是在内存里圈出一块地,在这块地里放想放的东西.变量关心的是这块地里放的东西,并不关心它在内存的哪里圈的地:而指针则关心这块地在内存的哪个地方,并不关心这块地多大,里面存了什么东西. 指针怎么用呢?下面就是基本用法: int a, b,

Linux下C语言实现C/S模式编程_C 语言

由标题可知,这篇文章主要讲如何用C语言实现一个C/S模式的程序. 主要功能:时间回送. 客户机发出请求,服务器响应时间,并返回服务器时间,与客户机进行同步. 废话不多说,下面直接贴出源代码. 代码如下: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <time.h> #

C语言实现汉诺塔游戏_C 语言

操作就是:A B 号码A的塔顶一层放在号码B的塔顶.如1(空格) 3 回车. 话说有人能把我这C的代码添加到QT界面框架上去么?  代码写的不好 ,维护性不够,只能玩8层的,写完以后发现很难拓展,软件工程,设计模式有待提高.... 里面提示输入等级的装B用了,没有实现,大家随便输入个个位数就可以玩了. stackfunc.c #include"STACK.h" #include<stdio.h> extern ceng CENG[SIZE]; //数据入栈 void pus

C语言进制转换代码分享_C 语言

代码很简单,功能也很简单,这里就不多废话了 #include<stdio.h> int main() { char ku[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; int zh[32],i=0,w,j; long int b,y; printf("请输入一个十进制数,我能帮您把它转换成2~16任意进制数:\n"); scanf("%d",&y);

C语言实现堆排序的简单实例_C 语言

本文通过一个C语言实现堆排序的简单实例,帮助大家抛开复杂的概念,更好的理解堆排序. 实例代码如下: void FindMaxInHeap(int arr[], const int size) { for (int j = size - 1; j > 0; --j) { int parent = j / 2; int child = j; if (j < size - 1 && arr[j] < arr[j+1]) { ++child; } if (arr[child] &