本篇主要讲解二叉树基本操作的递归实现。
树相关的术语:
节点的度:一个节点含有的子树的个数称为该节点的度
叶节点或终端节点:度为零的节点称为叶节点
非终端节点或分支节点:度不为零的节点
双亲节点或父节点:若一个结点含有子节点,则这个节点称为其子节点的父节点
孩子节点或子节点:一个节点含有的子树的根节点称为该节点的子节点
兄弟节点:具有相同父节点的节点互称为兄弟节点
树的度:一棵树中,最大的节点的度称为树的度
节点的层次:从根开始定义起,根为第1层,根的子节点为第2层,以此类推
树的高度或深度:树中节点的最大层次
堂兄弟节点:双亲在同一层的节点互为堂兄弟
节点的祖先:从根到该节点所经分支上的所有节点
子孙:以某节点为根的子树中任一节点都称为该节点的子孙
森林:由m(m>=0)棵互不相交的树的集合称为森林
二叉树 是每个节点最多有两个子树的树结构,如下图。
#include <malloc.h>
#include <Stack>
#include <Queue>
#include<iostream>
using namespace std;
#define format "%c"
typedef struct Node {
Element data;
struct Node *lchild;
struct Node *rchild;
} *Tree,tree;
void treeNodeConstructor(Tree &root, Element data[]);//建立树
int GetDepth(Tree root);//求树高度void preorderTraversal(Tree root);//先序遍历
void inorderTraversal(Tree root);//中序遍历void postorderTraversal(Tree root);//后序遍历
void depthFirstSearch(Tree root);//深度优先遍历void breadthFirstSearch(Tree root);//广度优先遍历
int changechild(Tree root);//交换左右孩子节点
int main(){
Tree root;
Element data[] ="-+a##*b##-c##d##/e##f##";
printf("二叉树建立\n");
treeNodeConstructor(root, data);
printf("------------------------------------\n");
printf("\nCreate binary tree successfully!\n");
printf("\n------------------------------------");
printf("\n\n先序遍历二叉树结果: ");
preorderTraversal(root);
printf("\n\n中序遍历二叉树结果: ");
inorderTraversal(root);
printf("\n\n后序遍历二叉树结果: ");
postorderTraversal(root);
printf("\n\n二叉树的高度为:");
cout<<GetDepth(root)<<endl;
printf("\n\n深度遍历二叉树结果:");
depthFirstSearch(root);
printf("\n\n广度遍历二叉树结果:");
breadthFirstSearch(root);
cout<<endl;
return 0;
}
//二叉树构造器,按先序遍历顺序构造二叉树
//无左子树或右子树用'#'表示
void treeNodeConstructor(Tree &root, Element data[])
{
static int index=0;//int index = 0; //全局索引变量
Element e = data[index++];
if(e == '#')
{
root = NULL;
}
else
{
root = (tree *)malloc(sizeof(tree));
root->data = e;
treeNodeConstructor(root->lchild, data); //递归构建左子树
treeNodeConstructor(root->rchild, data); //递归构建右子树
}
}
//二叉树的高度
int GetDepth(Tree root){
if (root == NULL)
{
return 0;
}
int leftDepth = GetDepth(root->lchild);
int rightDepth = GetDepth(root->rchild);
return leftDepth > rightDepth ? leftDepth +1: rightDepth+ 1;
}
//先序遍历二叉树
void preorderTraversal(Tree root)
{
if(root){
printf(format, root->data);
preorderTraversal(root->lchild);
preorderTraversal(root->rchild);
}
}
//中序遍历二叉树
void inorderTraversal(Tree root)
{
if(root){
inorderTraversal(root->lchild);
printf(format, root->data);
inorderTraversal(root->rchild);
}
}
//后序遍历二叉树
void postorderTraversal(Tree root)
{
if(root){
postorderTraversal(root->lchild);
postorderTraversal(root->rchild);
printf(format, root->data);
}
}
//深度优先遍历
void depthFirstSearch(Tree root)
{
stack<tree *> nodeStack; //使用C++的STL标准模板库
nodeStack.push(root);
Node *node;
while(!nodeStack.empty())
{
node = nodeStack.top();
printf(format, node->data); //遍历根结点
nodeStack.pop();
if(node->rchild)
{
nodeStack.push(node->rchild); //先将右子树压栈
}
if(node->lchild)
{
nodeStack.push(node->lchild); //再将左子树压栈
}
}
}
//广度优先遍历
void breadthFirstSearch(Tree root)
{
queue<tree *> nodeQueue; //使用C++的STL标准模板库
nodeQueue.push(root);
Node *node;
while(!nodeQueue.empty())
{
node = nodeQueue.front();
nodeQueue.pop();
printf(format, node->data);
if(node->lchild)
{
nodeQueue.push(node->lchild); //先将左子树入队
}
if(node->rchild)
{
nodeQueue.push(node->rchild); //再将右子树入队
}
}
}
//交换左右子树节点
int changechild(Tree root)
{
Tree d;
if(root)
{
d=root->lchild;
root->lchild=root->rchild;
root->rchild=d;
changechild(root->lchild);
changechild(root->rchild);
}
}