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. TensorFlow.js-机器学习

    一.参考学习 https://blog.csdn.net/Quincylk/article/details/85340004 http://www.tensorfly.cn/tfdoc/get_sta ...

  2. [LeetCode] 752. Open the Lock 开锁

    You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', ...

  3. 机器学习技法实现(一):AdaBoost- Decision Stump (AdaBoost - 决策树的基于Matlab的实现)

    经过前面对AdaBoost的总结,下面要基于Matlab实现AdaBoost-Stump进行二维平面数据点的分类的实验. 一. 实验原理 参看 http://blog.csdn.net/lg12591 ...

  4. win10安装网络适配器

    上面这个有些叫Microsoft Loopback Adapter

  5. Redhat7.6Linux版本下,在Oracle VM VirtualBox下hostonly下IP地址配置

    安装配置Linux的Redhat7.6教程见:https://www.cnblogs.com/xuzhaoyang/p/11264563.html 然后,配置完之后,我们开始配置IP地址,配置IP地址 ...

  6. isset和empty,isset和unset,__isset和__unset

    1.isset() 用来检测变量是否存在,如果变量存在,且值不等于零,返回ture empty() 用来检测是否为空,如果变量值值为"".0."0".NULL. ...

  7. jvm堆内存模型原理分析及堆内存分析工具jhat和MAT的使用超详细教程

  8. mysql_重置密码

    # 修改编码 ```pythonshow variables like '%char%'; #查看当前使用的编码 1.打开配置文件: vim /etc/mysql/my.cnf 2.在[client] ...

  9. Spring Cloud Alibaba学习笔记(22) - Nacos配置管理

    目前业界流行的统一配置管理中心组件有Spring Cloud Config.Spring Cloud Alibaba的Nacos及携程开源的Apollo,本文将介绍Nacos作为统一配置管理中心的使用 ...

  10. 缺陷的背后(四)---多进程之for循环下fork子进程引发bug

    导语 业务模块为实现高并发时的更快的处理速度,经常会采用多进程的方式去处理业务.多进程模式下常见的三种bug:for循环下fork子进程导致产生无数孙子进程,僵尸进程,接口窜包.本章主要介绍第一种常见 ...