博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
树与二叉树
阅读量:3947 次
发布时间:2019-05-24

本文共 1341 字,大约阅读时间需要 4 分钟。

一、树

在这里插入图片描述
在这里插入图片描述
二、二叉树的遍历
1、先序遍历
A、递归

void	PreOrder(BiTree T){
if(T!=NULL){
visit(T); PreOrder(T->lchild); PreOrder(T->rchild); }}

B、非递归

void PreOrder(BiTree T){
InitStack(S);BiTree p=T; while(p||!IsEmpty(S)){
if(P){
visit(p);Push(S,p); p=p->lchild; }else{
Pop(S,p); p=p->rchild; } }}

2、中序遍历

A、递归

void InOrder(BiTree T){
if(T!=NULL){
PreOrder(T->lchild); visit(T); PreOrder(T->rchild); }}

B非递归

void InOrder(BiTree T){
InitStack(S);BiTree p=T; while(p||!IsEmpty(S)){
if(P){
Push(S,p); p=p->lchild; }else{
Pop(S,p); visit(p); p=p->rchild; } }}

3、后序遍历

A、递归

void PostOrder(BiTree T){
if(T!=NULL){
PreOrder(T->lchild); PreOrder(T->rchild); visit(T); }}

B、非递归

void PostOrder(BiTree T){
InitStack(S);BiTree p=T; r=NULL; while(p||!IsEmpty(S)){
if(P){
Push(S,p); p=p->lchild; }else{
GetTop(S,p); if(p->rchild&&p->rchild!=r){
p=p->rchild; push(S,p); p=p->lchild; }else{
Pop(S,p); visit(p->data); r=p; p=NULL; } } }}

4、层次遍历

void LevelOrder(BiTree T){
InitQueue(Q); BiTree p; Enqueue(Q,T); while(!IsEmpty(Q)) {
Dequeue(Q,p); visit(p); if(p->lchild!=NULL) Enqueue(Q,p->lchild); if(p->echild!=NULL) Enqueue(Q,p->rchild); } }

5、由遍历序列构造二叉树

由二叉树的先序遍历和中序遍历可以唯一地确定一颗二叉树。
后序和中序可以唯一确定。
层序中序唯一确定。
先序后序不可以确定。

转载地址:http://xwgwi.baihongyu.com/

你可能感兴趣的文章
棋盘问题 POJ - 1321 ( 搜索 DFS)
查看>>
非常可乐 HDU - 1495 ( 搜索 BFS )
查看>>
2698:八皇后问题 OpenJ_Bailian - 2698 ( 搜索 DFS )
查看>>
2754:八皇后 OpenJ_Bailian - 2754 ( 搜索 DFS )
查看>>
1027 打印沙漏 (20 分)
查看>>
1028 人口普查 (20 分)
查看>>
Numbers HDU - 5585
查看>>
1030 完美数列 (25 分)
查看>>
1031 查验身份证 (15 分)
查看>>
1032 挖掘机技术哪家强 (20 分)
查看>>
1033 旧键盘打字 (20 分)
查看>>
Longest k-Good Segment CodeForces - 616D ( 尺取法)
查看>>
二叉搜索树的实现(BST)(插入+删除+查找+各种遍历+高度)
查看>>
今夕何夕 HDU - 6112 ( 模拟 )
查看>>
Dividing HDU - 1059 ( 多重背包 - 二进制简化 )
查看>>
Robberies HDU - 2955 ( 0-1背包 )
查看>>
FATE HDU - 2459 ( 二维完全背包 )
查看>>
B. Working out CodeForces - 429B (动态规划)
查看>>
10635 - Prince and Princess UVA-10635 (最长公共子序列的O(nlogn)的解法:LCS转换为LIS)
查看>>
Sizeof和Strlen
查看>>