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. 【翻译】Flink Table Api & SQL — 自定义 Source & Sink

    本文翻译自官网: User-defined Sources & Sinks  https://ci.apache.org/projects/flink/flink-docs-release-1 ...

  2. mybatis自动生成后无法获取主键id问题

    使用mybatis的自动生成工具生成的主键id: <selectKey resultType="java.lang.Integer" keyProperty="id ...

  3. [LeetCode] 284. Peeking Iterator 瞥一眼迭代器

    Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...

  4. Bro-Sysmon:一款让Bro-IDS(Bro)监视Windows端点活动的工具

    Bro-Sysmon是啥? 受到了Bro-OSQuery(https://github.com/bro/bro-osquery)项目的启发,为了使Bro-IDS(Bro)监视Windows端点活动所以 ...

  5. 「模拟赛20191019」A 简单DP

    题目描述 给一个\(n\times m\)的网格,每个格子上有一个小写字母. 对于所有从左上角\((1,1)\)到右下角\((n,m)\)只向下或向右走的路径构成的集合,判断是否存在两条走法不同的路径 ...

  6. 罗辑思维首席架构师:Go微服务改造实践

    转自:http://www.infoq.com/cn/news/2018/05/luojisiwei 方圆 曾先后在 Cisco,新浪微博从事基础架构研发工作.十多年一直专注于后端技术的研发,在消息通 ...

  7. Java开发笔记(一百零九)XML报文的定义和解析

    前面介绍了JSON格式的报文解析,虽然json串短小精悍,也能有效表达层次结构,但是每个元素只能找到对应的元素值,不能体现更丰富的样式特征.比如某个元素除了要传输它的字符串文本,还想传输该文本的类型. ...

  8. Django模型层之ORM

    Django模型层之ORM操作 一 ORM简介 我们在使用Django框架开发web应用的过程中,不可避免地会涉及到数据的管理操作(如增.删.改.查),而一旦谈到数据的管理操作,就需要用到数据库管理软 ...

  9. MySQL多表查询答案

    一.综合练习 1.1 init.sql文件内容 /* 数据导入: Navicat Premium Data Transfer Source Server : localhost Source Serv ...

  10. go 连接到数据库

    package main import ( "fmt" _ "github.com/go-sql-driver/mysql" "github.com/ ...