二叉树的链式结构递归遍历实现
二叉樹的鏈式結構遞歸遍歷實現
1.???? BinTree.h文件
#ifndef__BINTREE_H__
#define__BINTREE_H__
#include<stdio.h>
#include<stdlib.h>
// typedef關鍵字是用來重命名數據類型的 這里重命名了數結點中有效數據的類型
typedef intBinTreeElemType;
// 定義了樹的結點類型??
typedef structBinTreeNode
{
?????? BinTreeElemType data;?????????????????? // 1. 結點中有效數據
?????? struct BinTreeNode *leftChild;?????? // 2. 結點的左孩子指針
?????? struct BinTreeNode *rightChild;???? // 3. 結點的右孩子指針
}BinTreeNode;
?
// 定義了樹的結構
typedef structBinTree
{
?????? struct BinTreeNode *root;????????????? // 1. 樹根的指針
?????? char refvalue;???????????????????????????????? // 2. 定義了結點為空的標志位
}BinTree;
?
// 函數的聲明
intinitBinTree(struct BinTree *);
intcreateBinTree(struct BinTree *);
?
intpreOrder(struct BinTree *tree);
int inOrder(structBinTree *);
intpostOrder(struct BinTree);
?
#endif
2.???? BinTree.c文件
#include"BinTree.h"
?
/*
?*? 函數名 : initBinTree
?*? 函數參數 : struct BinTree *tree(樹結構的地址)
?*? 函數的功能 :在我們創建一顆二叉樹的時候,對二叉樹進行初始化
?*???????????????? 1.將根結點的指針 指向為NULL 避免野指針
?*???????????????? 2.使用 '#' 作為空結點的標志
?*? 函數的返回值 : return 0
?*/
intinitBinTree(struct BinTree *tree)
{
?????? tree->root = NULL;
?????? tree->refvalue = '#';
??????
?????? return 0;
}
?
/*
?*? 函數名 :createBinTree
?*? 函數參數 :struct BinTree *tree(樹結構的地址)
?*? 函數的功能 :創建一顆二叉樹這個方法是給用戶調用的
?*? 函數的返回值 :return 0
?*
?*/
intcreateBinTree(struct BinTree *tree)
{
?????? __createBinTree(tree,&(tree->root));
??????
?????? return 0;
}
?
/*
?*? 函數名 :__createBinTree
?*? 函數參數 :參數1 struct BinTree *tree (樹的地址)
?*????????????參數2 struct BinTreeNode **t(二重指針 自己理解)
?*? 函數功能 : 這個方法是內部的方法 被createBinTree函數調用
?*? 函數返回值 : return 0
?*/
int__createBinTree(struct BinTree *tree, struct BinTreeNode **t)
{
?????? // 測試字符串 ABC##DE##F##G#H##
?????? char item;
?????? scanf("%c", &item);
??????
?????? if(item == tree->refvalue)
?????? {
????????????? *t = NULL;
?????? }
?????? else
?????? {
????????????? *t = (struct BinTreeNode*)malloc(sizeof(struct BinTreeNode));
????????????? (*t)->data = item;
????????????? (*t)->leftChild =(*t)->rightChild = NULL;
?????????????
????????????? __createBinTree(tree,&((*t)->leftChild));
????????????? __createBinTree(tree,&((*t)->rightChild));
?????? }
??????
?????? return 0;
}
?
/*
?*? 函數名 :preOrder
?*? 函數參數 :struct BinTree *tree (樹的地址)
?*? 函數功能 :遞歸前序遍歷二叉樹
?*? 函數返回值 :return 0
?*/
intpreOrder(struct BinTree *tree)
{
?????? printf("遞歸前序遍歷二叉樹 : ");
??????
?????? __preOrder(tree->root);
??????
?????? printf("\n");
?????? return 0;
}
?
/*
?*? 函數名 :__preOrder
?*? 函數參數 :struct BinTreeNode *t (樹中結點的指針)
?*? 函數功能 :該函數是內部函數被preOrder函數調用
?*? 函數返回值 :return 0
?*/
int__preOrder(struct BinTreeNode *t)
{
?????? if(t == NULL)
?????? {
?????? ?????? return0;
?????? }
?????? else
?????? {
????????????? printf("%c",t->data);
????????????? __preOrder(t->leftChild);
????????????? __preOrder(t->rightChild);
?????? }
??????
}
?
// 中序遍歷二叉樹
int inOrder(structBinTree *tree)
{
?????? printf("遞歸中序遍歷二叉樹 : ");
??????
?????? __inOrder(tree->root);
??????
?????? printf("\n");
?????? return 0;
}
?
?
int __inOrder(structBinTreeNode *t)
{
?????? if(t == NULL)
?????? {
????????????? return 0;
?????? }
?????? else
?????? {
????????????? __inOrder(t->leftChild);
????????????? printf("%c",t->data);
????????????? __inOrder(t->rightChild);
?????? }
??????
}
?
?
// 后序遍歷二叉樹
intpostOrder(struct BinTree *tree)
{
?????? printf("遞歸中序遍歷二叉樹 : ");
??????
?????? __postOrder(tree->root);
??????
?????? printf("\n");
?????? return 0;
}
?
?
int__postOrder(struct BinTreeNode *t)
{
?????? if(t == NULL)
?????? {
????????????? return 0;
?????? }
?????? else
?????? {
????????????? __postOrder(t->leftChild);
????????????? printf("%c",t->data);
????????????? __postOrder(t->rightChild);
?????? }
??????
}
3.???? main.c文件
#include"BinTree.h"
int main(int argc,char &argv)
{
struct BinTree tree;
// 初始化二叉樹
initBinTree(&tree);
// 創建二叉樹
createBinTree(&tree);
// 遞歸先序遍歷
preOrder(&tree);
// 遞歸中序遍歷
inOrder(&tree);
// 遞歸后序遍歷
postOrder(&tree);
return 0;
}
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的二叉树的链式结构递归遍历实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GVIM编辑器的配置
- 下一篇: 二叉树的链式结构的非递归遍历