从零开始学数据结构系列之第三章《二叉树链式结构及实现2》

文章目录


前序/先序/根遍历


我们拿上图来举例

前序/先序/根遍历:

​   根遍历的原理就是,先遍历根,然后再遍历左子树,然后右子树

​   一句话来讲,就是 中 -> 左 -> 右

所以我们按如上顺序来算的话

  先遍历根树 A 之后根结束,开始遍历左子树

  于是到子树 B 发现B子树是D和E的"根"节点,于是继续向下寻找

继续到子树 D 之后就没有其他子树了,于是开始遍历右子树

  继续到子树 E 之后就没有其他子树了,因为已经全部遍历完了,开始回溯到B,再回溯到A,之后在开始遍历右子树

  继续到子树 C 发现C子树是F和G的"根"节点,于是继续向下寻找

  继续到子树 F 之后就没有其他子树了,于是开始遍历右子树

  继续到子树 G 之后就没有其他子树了,因为已经全部遍历完了,开始回溯到B,再回溯到A,结束遍历

所以根遍历的顺序就是

​   A->B->D->E->C->F->G


递归的方式:

c 复制代码
void preOrder(TreeNode* T) 
{
	if(T == NULL)
		return;
	else
	{
		printf("%c->",T->data);
		preOrder(T->lchild);
		preOrder(T->rchild);
	}
}

非递归的方式,思想类似与栈的方式

验证代码

c 复制代码
/*可以输入
ABD##E##CF##G##
来进行验证
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define size 20

typedef struct TreeNode 
{
	char data;
	struct TreeNode *lchild;
	struct TreeNode *rchild;
}TreeNode;

typedef struct Node 
{
	TreeNode *data;
	struct Node *next;
}Node;


void createTree(TreeNode** T,char* temp,int* index)
{
	char ch;
	
	ch = temp[*index];
	(*index)++;

	if( ch == '#') *T = NULL;
	else
	{
		 *T =(TreeNode*)malloc(sizeof(TreeNode));
		 (*T)->data = ch;
		createTree(&(*T)->lchild,temp,index);
		createTree(&(*T)->rchild,temp,index);		
	}
}
// 前序/先序/根遍历
void preOrder(TreeNode* T) 
{
	if(T==NULL)
		return;
	else
	{
		printf("%c->",T->data);
		preOrder(T->lchild);
		preOrder(T->rchild);
	}
}


Node* initQueue() 
{
	Node* node = (Node*)malloc(sizeof(Node));
	node->data = NULL;
	node->next =NULL;
	return node;
}

void enQueue(TreeNode* data, Node* list) 
{
	Node* node = (Node*)malloc(sizeof(Node));
	node->data = data;
	
	while(list->next)list=list->next;
	node->next = list->next;
	list->next=node;
}


int isEmpty(Node* Q) 
{
	if(Q->next == NULL)
		return 1;
	else
		return 0;
}

Node* deQueue(Node* Q)
{
 if (isEmpty(Q)) 
     return NULL;
 else 
 {
	Node *node = Q->next;
	Q ->next = node->next;
    return node;
 }
}

void levelTraverse(Node* Q, TreeNode* T) 
{
    enQueue(T, Q);
    while (!isEmpty(Q)) 
	{
        Node* node = deQueue(Q);
        printf("%c->", node->data->data);
        if (node->data->lchild) 
		{
            enQueue(node->data->lchild, Q);
        }
        if (node->data->rchild) 
		{
            enQueue(node->data->rchild, Q);
        }
    }
}



int main(int argc, char* argv[]) 
{
	TreeNode *T;
	int i=0;
	char *temp=NULL;
	Node* Q = initQueue();

	temp=(char*)malloc(sizeof(char) * (size+1));
	gets(temp);
	createTree(&T,temp,&i);
	preOrder(T);       
    printf("\n");
    levelTraverse(Q, T);
    printf("\n");

    return 0;
}

往期回顾

1.【第一章】《线性表与顺序表》
2.【第一章】《单链表》
3.【第一章】《单链表的介绍》
4.【第一章】《单链表的基本操作》
5.【第一章】《单链表循环》
6.【第一章】《双链表》
7.【第一章】《双链表循环》
8.【第二章】《栈》
9.【第二章】《队》
10.【第二章】《字符串暴力匹配》
11.【第二章】《字符串kmp匹配》
12.【第三章】《树的基础概念》
13.【第三章】《二叉树的存储结构》
14.【第三章】《二叉树链式结构及实现1》

相关推荐
Sakura_j_h_23 分钟前
MCP SDK 2.0 实战:用 MCPServer 搭本地笔记检索服务
开发语言·笔记·python·qt·ai工具·mcp·mcpserver
QuZhengRong1 小时前
【AI】Agent 全栈进阶|AgentScope 沙箱源码解析
python·学习·ai·agent·沙箱
字节跳动开源1 小时前
OpenViking 3D理解模型上线轴测八视图及PMI解析
人工智能·开源·aigc
Zwawa2 小时前
陀螺仪数据为什么看起来总在动:零偏、噪声和零漂
c语言·单片机·嵌入式
奶油话梅糖2 小时前
3 天 1,705 Star:DeepSeek 开源官方聊天前端——零后端纯静态,浏览器直连 API
前端·开源
用户1213095375673 小时前
买了一瓶“澳洲神经酸”,我顺着溯源码查到了皮包公司,然后写了个工具
开源
武子康3 小时前
H3-World 扩展跳跃与开门:新动作怎样进入训练数据?
大数据·人工智能·算法
卡尔曼的BD SLAMer3 小时前
北斗导航 |GNSS ION论文集汇总1997到2022(链接在文末)
算法·北斗导航
时间的拾荒人3 小时前
Redis Cluster 集群详解:分片算法、搭建、故障处理与扩容实战
数据库·redis·算法·集群
前进的李工4 小时前
Agent 学习笔记 15:Agent 怎样响应事件与中途打断?
人工智能·笔记·agent·openclaw·工具设计·gpt-6 astra·原生异步