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. PAT 甲级 1076 Forwards on Weibo (30分)(bfs较简单)

    1076 Forwards on Weibo (30分)   Weibo is known as the Chinese version of Twitter. One user on Weibo m ...

  2. [LeetCode] 337. House Robber III 打家劫舍 III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  3. linux:vim中全选复制

    全选(高亮显示):按esc后,然后ggvG或者ggVG 全部复制:按esc后,然后ggyG 全部删除:按esc后,然后dG 解析: gg:是让光标移到首行,在vim才有效,vi中无效 v : 是进入V ...

  4. [07]Go设计模式:过滤器模式(FilterPattern)

    目录 过滤器模式 一.简介 二.代码 三.参考链接 过滤器模式 一.简介 过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使 ...

  5. LeetCode 82. 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II)

    82. 删除排序链表中的重复元素 II 82. Remove Duplicates from Sorted List II 题目描述 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中没有 ...

  6. Linux学习-软件包管理安装

    rpm RPM是Red-Hat Package Manager(RPM软件包管理器)的缩写 软件包类型 二进制包:已经使用GCC编辑后的 tar源码包:需要编译 rpm包获取方式 1,系统镜像   需 ...

  7. python学习67-面向对象-封装

    封装 1.什么是封装? 根据名字寓意为:把一个东西装起来,然后密封,类似这样的面向对象的编程为封装. 真正的封装是明确的区别内外,只能在内部用,外部无法调用. 2. 举例: class Car: _s ...

  8. Linux基础系统优化(二)

    SELinux功能 SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,这个功能管理员又爱又恨,大多数生产环境也是关闭的做法,安全手段使 ...

  9. kubernetes 实践二:kubectl命令使用

    这里记录kubernetes学习和使用过程中的内容. CentOS7 k8s-1.13 flanneld-0.10 docker-18.06 etcd-3.3 kubectl用法概述 kubectl是 ...

  10. docker 部署 nsq

    这篇文章主要介绍如何使用docker部署 nsq 组件 环境准备 本文基于一台 ubuntu 虚拟机试验 docker 安装 docker 安装方式 使用docker version 命令检查 doc ...