IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 输出根节点到叶节点的所有路径,后序遍历,非递归,c/c++描述 -> 正文阅读

[数据结构与算法]输出根节点到叶节点的所有路径,后序遍历,非递归,c/c++描述

??对二叉树的遍历操作是其他一切操作的基础,如查找,比较。本例中要求遍历二叉树节点时,遇到了叶节点就输出根节点到叶节点的路径。程序是在后序遍历非递归的程序基础上稍加修改而来的。前面文章里已详细写了后序遍历非递归的程序。此处就不再对遍历思路详细叙述。
??函数createBiTree:建立二叉树;函数displayBiTree输出二叉树的逗号表达式,以检验建立过程;函数allPathRootToLeafPostOrder输出根节点到叶节点的所有路径。
??完整代码如下,先是main函数所在源文件:

#include<iostream>
#include<stdio.h>
using namespace std;
#define STACKDEPTH 15
struct BiTreeNode {
	char value;
	BiTreeNode* leftChild;
	BiTreeNode* rightChild;
};
extern void createBiTree(BiTreeNode*& biTreeRoot, char* ptChar);
extern void displayBiTree(BiTreeNode*& biTreeRoot);
extern void allPathRootToLeafPostOrder(BiTreeNode*& biTreeRoot);
int main() {
	char array[] = "A(B(D(,G)),C(E,F))";
	BiTreeNode* biTreeRoot = NULL;

	createBiTree(biTreeRoot,array);
	cout << "the char array is :";
	for (int i = 0; array[i] != '\0'; i++)
		cout << array[i];
	cout<< endl<< "binary tree is    :";
	displayBiTree(biTreeRoot);

	cout <<endl<<endl<< "all path from root to leaf : " << endl;
	allPathRootToLeafPostOrder(biTreeRoot);

	return 0;
}

??接着是各函数所在源文件:

#include<iostream>
#include<stdio.h>
using namespace std;
#define STACKDEPTH 15
struct BiTreeNode {
	char value;
	BiTreeNode* leftChild;
	BiTreeNode* rightChild;
};
void createBiTree(BiTreeNode*& biTreeRoot, char* ptChar) {
	struct {
		BiTreeNode* ptsBiTree[STACKDEPTH];
		int indexTop = -1;
	}sequStack;

	BiTreeNode* ptNew = NULL;
	char s;
	int leftRight;//1 is left   2 is right
	while (*ptChar != '\0') {
		s = *ptChar;
		if ('A' <= s && s <= 'Z') {
			ptNew = new BiTreeNode;
			ptNew->value = s;
			ptNew->leftChild = ptNew->rightChild = NULL;

			if (biTreeRoot == NULL)
				biTreeRoot = ptNew;
			else if (leftRight == 1)
				sequStack.ptsBiTree[sequStack.indexTop]->leftChild = ptNew;
			else if (leftRight == 2)
				sequStack.ptsBiTree[sequStack.indexTop]->rightChild = ptNew;
		}
		else if (s == '(') {
			sequStack.indexTop++;
			sequStack.ptsBiTree[sequStack.indexTop] = ptNew;
			leftRight = 1;
		}
		else if (s == ',')
			leftRight = 2;
		else if (s == ')')
			sequStack.indexTop--;

		ptChar++;
	}
}
void displayBiTree(BiTreeNode*& biTreeRoot) {   // 本查找方法是先序遍历
	if (biTreeRoot == NULL)
		return;//if binary tree does not exsit,return
	cout << biTreeRoot->value;
	if (biTreeRoot->leftChild != NULL || biTreeRoot->rightChild != NULL) {
		cout << '(';
		displayBiTree(biTreeRoot->leftChild);
		if (biTreeRoot->rightChild != NULL) {
			cout << ',';
			displayBiTree(biTreeRoot->rightChild);
		}
		cout << ')';
	}
}

void allPathRootToLeafPostOrder(BiTreeNode*& biTreeRoot) {
	if (biTreeRoot == NULL)
		return;

	BiTreeNode* nodes[STACKDEPTH],*pt,*ptPopped = NULL;
	int indexTop = 0, pathCount = 0;
	nodes[indexTop] = biTreeRoot;
	bool checkLeftChild = true;

	while (indexTop >= 0) {
		pt = nodes[indexTop];
		while (checkLeftChild && pt->leftChild != NULL) {
			indexTop++;
			nodes[indexTop] = pt->leftChild;
			pt = pt->leftChild;
		}

		if (pt->rightChild == NULL && pt->leftChild == NULL) {
			pathCount++;
			cout << "path " << pathCount << " :";
			for (int i = 0; i <= indexTop; i++)
				cout << nodes[i]->value << " ";
			cout << endl;
		
			ptPopped = nodes[indexTop];
			indexTop--;

			checkLeftChild = false;
		}
		else if (pt->rightChild == NULL || pt->rightChild == ptPopped) {
			ptPopped = nodes[indexTop];
			indexTop--;
		}
		else {
			indexTop++;
			nodes[indexTop] = pt->rightChild;
			checkLeftChild = true;
			ptPopped = NULL;
		}
	}
}

??测试结果及对应二叉树如下:
在这里插入图片描述
在这里插入图片描述
谢谢阅读。

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-07-10 11:38:31  更:2021-07-10 11:38:43 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/5 8:54:59-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码