C实现二叉树

简单说明

实现了先序遍历、中序遍历、后序遍历、搜索

本来想着和平衡二叉树一起放上来的,但是花了一个下午也只是把平衡二叉树原理弄懂和左右旋代码实现,最难的平衡左/右旋还没弄,就不显摆了,就分开来写吧。

代码实现

利用了堆栈来存储每一个左节点,利用左节点把所有点的信息全部记录下来,因为左节点可以记录其子节点的地址,然后,按照树的存储规则将堆栈中的信息分配到二叉树中。

#include <stdio.h>
#include <stdlib.h> typedef struct treenode{
char str;
struct treenode *left;
struct treenode *right;
}*btree,treenode; // x(a(b,c),d(e(g,h),f))
// x
// a d
// b c e f
// g h
void createtree(btree btre, char *str, int num){
int lr = 0; // left 0, right 1
int top = 0;
btree p;
btree pstack[num]; for(int i=0; i < num; i++){
switch(str[i]){
case '(':
{
printf("(");
lr = 0;
top ++;
pstack[top] = p;
break;
}
case ')':
{
printf(")");
if(top < 1){
printf("stack is empty\n");
exit(0);
}
top --;
break;
}
case ',':
{
printf(",");
lr = 1;
break;
}
default:
{
printf("d");
p = (btree)malloc(sizeof(treenode));
p->left = p->right = NULL;
p->str = str[i];
if(top == 0){
btre->str = p->str;
break;
}
if(lr == 0){
pstack[top]->left = p;
}
else
pstack[top]->right = p;
}
}
}
btre->right = pstack[1]->right;
btre->left = pstack[1]->left;
} void preorder(btree btre){
btree p = btre; if(p != NULL){
printf("%c->",p->str);
preorder(p->left);
preorder(p->right);
}
} void inorder(btree btre){
btree p = btre; if(p != NULL){
inorder(p->left);
printf("%c->",p->str);
inorder(p->right);
}
} void postorder(btree btre){
btree p = btre; if(p != NULL){
postorder(p->left);
postorder(p->right);
printf("%c->",p->str);
}
} void cleartree(btree btre){
if(btre != NULL){
cleartree(btre->left);
cleartree(btre->right);
free(btre);
btre = NULL;
printf(".");
}
} char search(btree btre,char x){
if(btre == NULL){
return 'N';
}else{
if(x == btre->str){
return btre->str;
}else{
if(x == search(btre->left,x)){
return x;
}
if(x == search(btre->right,x)){
return x;
}
return 'N';
}
}
} int main(){
char *str = "x(a(b,c),d(e(g,h),f))";
printf("%s\n",str);
btree btre = (btree)malloc(sizeof(treenode));
createtree(btre, str, 21);
printf("\npreorder:\n");
preorder(btre);
printf("\ninorder:\n");
inorder(btre);
printf("\npostorder:\n");
postorder(btre); char c = search(btre,'d');
printf("\nsearch result:%c",c); printf("\nclear");
cleartree(btre);
printf("\n");
}

c实现二叉树的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. 二叉树的递归实现(java)

    这里演示的二叉树为3层. 递归实现,先构造出一个root节点,先判断左子节点是否为空,为空则构造左子节点,否则进入下一步判断右子节点是否为空,为空则构造右子节点. 利用层数控制迭代次数. 依次递归第二 ...

  3. c 二叉树的使用

    简单的通过一个寻找嫌疑人的小程序 来演示二叉树的使用 #include <stdio.h> #include <stdlib.h> #include <string.h& ...

  4. Java 二叉树遍历右视图-LeetCode199

    题目如下: 题目给出的例子不太好,容易让人误解成不断顺着右节点访问就好了,但是题目意思并不是这样. 换成通俗的意思:按层遍历二叉树,输出每层的最右端结点. 这就明白时一道二叉树层序遍历的问题,用一个队 ...

  5. 数据结构:二叉树 基于list实现(python版)

    基于python的list实现二叉树 #!/usr/bin/env python # -*- coding:utf-8 -*- class BinTreeValueError(ValueError): ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  8. [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化

    One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...

  9. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  10. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

随机推荐

  1. Grafana修改背景色

    grafana默认主题色是黑底白字,我们将它修改成白底黑字: in /etc/grafana/grafana.ini uncomment line and set default_theme = li ...

  2. [LeetCode] 210. Course Schedule II 课程安排II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  3. MinGW离线安装

    今天安装下载MinGW-W64-install.exe安装MinGW试了好几次都失败了 因此决定用离线安装包进行安装 1.下载 下载地址https://sourceforge.net/projects ...

  4. dubbo问题集合

    背景:用于整理在开发过程中遇到的相关问题. Dubbo Server的时候出现“Will not attempt to authenticate using SASL (unknown error)” ...

  5. Java基础笔试练习(十二)

    1.(C#.JAVA)扩展方法能访问被扩展对象的public成员 A.能 B.不能 答案: A 解析: 翻译一下,子类方法是否能够访问父类中的public成员. 2.如果子类要调用父类的构造函数,则通 ...

  6. pandas中的axis参数(看其他人的博客中产生的疑问点,用自己的话解析出来)

    axis有两个值:axis=0或者axis=1 看到很多资料都不太理解,把我个人理解说一下: 下面这张图,在很多资料中都看到了,我只能说先死记住 axis=0,代表跨行(注意看这张图的axis=0的箭 ...

  7. MySQL数据库-表操作-SQL语句(一)

    1. 数据库操作与存储引擎 1.1   数据库和数据库对象 数据库对象:存储,管理和使用数据的不同结构形式,如:表.视图.存储过程.函数.触发器.事件等. 数据库:存储数据库对象的容器. 数据库分两种 ...

  8. Spring MVC前端控制器不拦截静态资源配置

  9. Spring Cloud常用组件及各组件版本对应关系图

    Spring Cloud常用组件: 架构图: 版本对应关系:

  10. Java的基础类型笔记

    数据类型       大小 byte(字节)    1(8位) shot(短整型)       2(16位) int(整型)       4(32位) long(长整型)       8(32位) f ...