使用C语言构建基本的二叉树数据结构

所属分类: 软件编程 / C 语言 阅读数: 44
收藏 0 赞 0 分享

二叉树结构常用的一些初始化代码

#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++ socket广播通信的原理与实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

C++计算ICMP头的校验和实例

这篇文章主要介绍了C++计算ICMP头的校验和的方法,代码简单实用,对于校验ICMP报文来说有不错的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++设置超时时间的简单实现方法

这篇文章主要介绍了C++设置超时时间的简单实现方法,涉及系统函数setsockopt对套接口的操作,具有一定的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++实现ping程序实例

这篇文章主要介绍了C++实现ping程序实例,涉及C++对于ICMP数据包的发送与回显处理,具有一定的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++之boost::array的用法

这篇文章主要介绍了C++之boost::array的用法,以实例的形式简单讲述了静态数组的容器boost::array的使用技巧,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++之Boost::array用法简介

这篇文章主要介绍了C++之Boost::array用法简介,较为详细的分析了Boost::array中的常见用法,并用实例的形式予以总结归纳,需要的朋友可以参考下
收藏 0 赞 0 分享

VC文件目录常见操作实例汇总

这篇文章主要介绍了VC文件目录常见操作实例汇总,总结了VC针对文件目录的各种常用操作,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

VC打印word,excel文本文件的方法

这篇文章主要介绍了VC打印word,excel文本文件的方法,是VC操作文本文件中非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

VC++获得当前进程运行目录的方法

这篇文章主要介绍了VC++获得当前进程运行目录的方法,可通过系统函数实现该功能,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

VC中SendMessage和PostMessage的区别

这篇文章主要介绍了VC中SendMessage和PostMessage的区别,较为全面的分析了SendMessage和PostMessage运行原理及用法上的不同之处,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多