- 浏览: 61878 次
- 性别:
- 来自: 北京
最新评论
c语言写着还挺带感
#include<stdlib.h> #define null 0 struct tree{ struct tree* left; int value; struct tree* right; }; typedef struct tree Tree; Tree* root; Tree* insert_rcs(Tree* root, int value){ //只在叶节点位置插入 if(root == null){ root = malloc(sizeof(Tree)); root->value = value; root->left = null; root->right = null; //返回新增的叶节点 return root; } if(root->value > value){ root->left = insert_rcs(root->left, value); }else{ root->right = insert_rcs(root->right, value); } return root; } Tree* insert_no_rcs(Tree* root, int value){ Tree* newnode = malloc(sizeof(Tree)); newnode->value = value; newnode->left = null; newnode->right = null; if(root == null){ root = newnode; return root; } //p为工作指针 Tree* p = root; //p节点是插入节点,pre节点时插入节点的父节点 Tree* pre; while(p){ pre = p; if(p->value > value){ p = p->left; }else{ p = p->right; } } if(pre->value > newnode->value){ pre->left = newnode; }else{ pre->right = newnode; } return root; } //前序遍历 void pre_print_rcs(Tree* root){ if(root != null){ printf("value: %d \t",root->value); pre_print_rcs(root->left); pre_print_rcs(root->right); } } Tree* Stack[20]; int top = -1; //前序遍历 非递归 void pre_print_no_rcs(Tree* root){ //top != -1 这个条件一定要加上,这是处理单枝情况 while(root != null || top != -1){ if(root != null){ printf("%d \t",root->value); if(top+1 != 20){ Stack[++top] = root; root = root->left; } }else{ root = Stack[top--]->right; } } } //中序遍历 void in_print_rcs(Tree* root){ if(root != null){ in_print_rcs(root->left); printf("value: %d \t",root->value); in_print_rcs(root->right); } } //中序遍历 非递归 void in_print_no_rcs(Tree* root){ //top != -1 这个条件一定要加上,这是处理单枝情况 while(root != null || top != -1){ if(root != null){ if(top+1 != 20){ Stack[++top] = root; root = root->left; } }else{ root = Stack[top--]; printf("%d \t",root->value); root = root->right; } } } //后序遍历 void post_print_rcs(Tree* root){ if(root != null){ post_print_rcs(root->left); post_print_rcs(root->right); printf("value: %d \t",root->value); } } Tree* Q[20]; int front; int rear; //层序遍历 //输出队头元素,并将左右子树如队列 void level_print(Tree* root){ front = rear = 0; if(root != null){ Q[++rear] = root; while(rear != front){ Tree* p = Q[++front]; printf("%d \t",p->value); if(p->left != null){ Q[++rear] = p->left; } if(p->right != null){ Q[++rear] = p->right; } } } } Tree* create_no_rcs(int* list, int n){ int i; for(i=0; i<n; i++){ root = insert_no_rcs(root, list[i]); } return root; } Tree* create_rcs(int* list, int n){ int i; for(i=0; i<n; i++){ root = insert_rcs(root, list[i]); } return root; } int main(){ int list[10] = { 6,9,7,4,5,2,1,8,12,0 }; //root = create_no_rcs(list, 10); root = create_rcs(list, 10); //pre_print_rcs(root); //pre_print_no_rcs(root); in_print_rcs(root); in_print_no_rcs(root); //post_print_rcs(root); //level_print(root); return 0; }
发表评论
-
求链表中间节点的值,检测链表的环
2012-07-27 14:19 806求链表中间节点的值,检测链表的环 int loop(st ... -
实习前记
2012-07-16 15:27 696经过回来一周的找工作,总体感觉就是很累啊,每天东跑西颠的。面了 ... -
php函数参数列表
2012-05-11 16:50 1400[size=medium] 1.直接传值 function ... -
php的ob_flush和flush
2012-05-10 21:20 1069php.ini中 output_buffering = of ... -
php读文件的4中方法。
2012-05-10 20:38 871fopen $fp = fopen("downl ... -
百度笔试算法题一道。
2012-05-10 15:02 932一个数组a[0-n-1],a[0-mid]和a[mid+1-n ... -
自己实现php UTF8中文字符串截取
2012-05-09 11:38 2849header("Content-type: te ... -
C与C++动态分配,释放内存的区别
2012-05-08 17:30 160221. malloc()函数 1.1 malloc的 ... -
nginx rewrite
2012-05-04 11:23 0http://blog.cafeneko.info/2010/ ... -
php magic method
2012-05-04 11:16 859php的魔术方法总结 php的魔术方法都是和类有关的。 ... -
诡异的 shell 08 bug
2012-04-30 01:11 724v=08 echo $v shell里以0开头的都会把它当作8 ... -
排序相关
2012-04-22 16:01 0排序分类 内排序: 交换式排序: ... -
php string
2012-04-22 11:33 854一.字符串类型 php一共有8中数据类型 ... -
php 深度优先递归输出路径下所有文件
2012-04-19 21:27 1491<?php $dir = " ... -
简单的栈
2012-04-19 21:14 679#include <stdio.h> #de ... -
简单的循环队列
2012-04-19 21:13 780#include <stdlib.h> ... -
单链表删除一个节点
2012-04-19 21:10 9813有头结点的情况,附加一个逆置 #include <s ... -
KMP与BF,实现了一个非主流next函数
2012-04-19 20:16 899#include <stdlib.h> #i ... -
ip过滤问题
2012-03-22 21:09 0假设有很多段ip段属于教育网的,如何尽快辨别一用户ip是否属于 ... -
求三叉树高度
2012-03-18 17:05 3054有12345个结点的满3叉数的高度为_____写出计算过程 ...
相关推荐
树的递归、非递归前序中序后序遍历实现,层序遍历,及树的Morris前序中序后序遍历实现。main函数有测试样例,测试样例是A B D F E C G H I 。注意空出的地方是空格,前序建树。
非递归前序,中序,后序遍历二叉树(优化算法)
二叉树先序、中序、后序遍历非递归算法,简述了二叉树的基本算法。
关于数据结构实验的二叉树问题
二叉树的基本操作,包括前序、中序、后序遍历的递归和非递归算法,不得不下的资源
二叉树遍历算法 (递归的、非递归的中序、前序、后序遍历 和 层次遍历 以及 求二叉树的宽度和深度)
关于二叉树前序和后序的非递归遍历算法.rar
创建二叉树,并实现二叉树前序、中序递归遍历算法和二叉树的后序非递归遍历
二叉树、二叉搜索树的构建,前序、中序、后序的 递归和非递归遍历;前序中序序列构建二叉树……
二叉树前序,中序,后序,求深度的递归和非递归方法
C语言实现通用栈结构 递归遍历二叉树 非递归遍历二叉树 (前,中,后序) exmaple.c为测试文件
数据结构二叉树链式结构的前序遍历,中序遍历,后序遍历用递归的方法,层级遍历采用队列结构
已知二叉树的前序和中序遍历,打印后序遍历,采用二叉树的非递归算法,分享给大家~~
二叉树的递归遍历算法非常好写。这个.cpp文件里是二叉树的非递归遍历!
二叉树前序、中序、后序三种遍历的非递归算法(C语言)
自己编写的实验二叉树的后序遍历非递归算法 包括以递归中序遍历建立二叉树 前序,中序,后序递归以及非递归实现二叉树的遍历 经vc6.0编译通过 自己实验,不足之处应该很多,望指出
栈的应用——二叉树的前序、中序、后序非递归遍历算法
本文实例讲述了PHP基于非递归算法实现先序、中序及后序遍历二叉树操作。分享给大家供大家参考,具体如下: 概述: 二叉树遍历原理如下: 针对上图所示二叉树遍历: 1. 前序遍历:先遍历根结点,然后遍历左子树,...
该程序代码实现了二叉树的递归生成创建,递归前序遍历,递归中序遍历,递归后序遍历,非递归前序遍历,非递归中序遍历,非递归后序遍历,以及递归层次遍历,递归求度为0,1,2的节点数,非递归求度为0,1,2的节点数。...
vs2010下运行编写,使用了STL栈,实现了基本的插入、删除、计算深度、查找,主要是遍历,包括递归遍历,以及非递归的前序中序后序遍历,每个函数都有测试用例,如果存在错误,请在给我留言。