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. Android Capabilities讲解

    1.Capabilities介绍 可以看下之前代码里面设置的capabilities DesiredCapabilities capabilities =newDesiredCapabilities( ...

  2. hashMap常见问题

    [解析hashMap的源码实现]      点击进入hashMap的源码实现 0.谈谈对hashMap的理解? 从底层结构.存取.扩容.冲突.实现原理.源码等方面说明. 1.你知道哪些常用的Map集合 ...

  3. 13 Spring 的事务控制

    1.事务的概念 理解事务之前,先讲一个你日常生活中最常干的事:取钱.  比如你去ATM机取1000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉1000元钱:然后ATM出1000元钱.这两个步骤必 ...

  4. [转帖]为什么需要 Zookeeper

    为什么需要 Zookeeper 柳树 学习&思考&写作 | 公众号:柳树的絮叨叨 ​关注他 童话 . 沈万马 等 351 人赞同了该文章 很多中间件,比如Kafka.Hadoop.HB ...

  5. Fedora30 install VS Code

    We currently ship the stable 64-bit VS Code in a yum repository, the following script will install t ...

  6. C++多态性----运算符重载与虚函数

    一.多态性 ①概述:多态是指同样的消息被不同类型的对象接收时导致的不同行为. ②类型: 可以分为四类:重载多态.强制多态.包含多态.参数多态. ------------------------ --- ...

  7. kotlin --- 时间戳与字符串互相转换

    直接贴代码,清晰易懂.喜欢点个赞 class Timestamp { /** * Timestamp to String * @param Timestamp * @return String */ ...

  8. kali_Airmon-ng第一次渗透测试

    再看了一些资料之后,决定自己整理一下进行第一次测试,测试目标,自己宿舍的WIFI.教程仅供学习参考 断开kali连接的wifi,并检查网卡状态 airmon-ng 开启无线网卡的监控模式 airmon ...

  9. MGR并行复制从节点复制线程死锁

    1.故障现象 20191113-22:32 datax全量同步t_shop_info表到 eorder所在的实例,t_shop_info表有两个唯一约束.总数据量不超过1w行,同步完成后MGR从库复制 ...

  10. mysql中常见正则表达式的应用

    查找name字段中以'st'为开头的所有数据: mysql> SELECT name FROM person_tbl WHERE name REGEXP '^st'; 查找name字段中以'ok ...