本文给出二叉树操作中某些功能的一个实现,个别函数用到STL的stack。
本文包含的功能有
1. 二叉树的前序遍历,递归形式。BinaryTree_preorderTraversal
2. 二叉树的前序遍历,非递归形式,要用到stack。函数名BinaryTree_preorderTraversalUseStack
3. 求二叉树的高度。函数名BinaryTree_height
4. 求二叉树的叶子节点数。函数名BinaryTree_leafNodeCount
5. 二叉树的拷贝。函数名BinaryTree_copy
本文包含的功能有
1. 二叉树的前序遍历,递归形式。BinaryTree_preorderTraversal
2. 二叉树的前序遍历,非递归形式,要用到stack。函数名BinaryTree_preorderTraversalUseStack
3. 求二叉树的高度。函数名BinaryTree_height
4. 求二叉树的叶子节点数。函数名BinaryTree_leafNodeCount
5. 二叉树的拷贝。函数名BinaryTree_copy
6. 二叉树的销毁。函数名BinaryTree_destroy
先给出头文件。
#ifndef MY_BIN_TREE_2018_02_13_18_47_DEF_H
#define MY_BIN_TREE_2018_02_13_18_47_DEF_H
typedef struct BINARYNODE{
char dat;
struct BINARYNODE *lchild;
struct BINARYNODE *rchild;
}BinaryNode;
// the node for traveling binary-tree
typedef struct BINTRAVLENODE{
const struct BINARYNODE *p;
bool flag;
}BinTravelNode;
#endif
再给出CPP文件
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include "biTree.h"
#include <iostream>
using namespace std;
void print_treenode(const BinaryNode *p)
{
cout << p->dat << " ";
}
// Recursion pre-order traversal of binary true
// 递归形式的二叉树的前序遍历
void BinaryTree_preorderTraversal(const BinaryNode *root)
{
if ( root==NULL)
return ;
print_treenode(root);
BinaryTree_preorderTraversal(root->lchild);
BinaryTree_preorderTraversal(root->rchild);
}
// 返回二叉树的高度
int BinaryTree_height(const BinaryNode *root)
{
int h1,h2;
if ( root==NULL)
return 0;
h1=BinaryTree_height(root->lchild);
h2=BinaryTree_height(root->rchild);
return (h1<h2)? h2+1: h1+1;
}
// get leaf node count,
// 返回二叉树的叶子节点数
int BinaryTree_leafNodeCount(const BinaryNode *root)
{
int n1,n2;
if ( root==NULL)
return 0;
if ( root->lchild==NULL && root->rchild==NULL)
return 1;
n1=BinaryTree_leafNodeCount(root->lchild);
n2=BinaryTree_leafNodeCount(root->rchild);
return n1+n2;
}
// 创建一个新的二叉树,并将root这个树的内容复制到新的二叉树,并返回
BinaryNode* BinaryTree_copy(const BinaryNode *root)
{
BinaryNode *left;
BinaryNode *right;
BinaryNode *rootCopy;
if ( root==NULL)
return NULL;
left =BinaryTree_copy(root->lchild);
right=BinaryTree_copy(root->rchild);
rootCopy= new BinaryNode;
rootCopy->dat= root->dat;
rootCopy->lchild=left;
rootCopy->rchild=right;
return rootCopy;
}
// 删除整个二叉树
void BinaryTree_destroy(BinaryNode *root)
{
if ( root==NULL)
return ;
BinaryTree_destroy(root->lchild);
BinaryTree_destroy(root->rchild);
free(root);
}
// non-recursion pre-order traversal of binary true
// 非递归形式的二叉树的前序遍历
void BinaryTree_preorderTraversalUseStack(const BinaryNode *root)
{
stack<BinTravelNode> st; // a stack for pre-order traversal
BinTravelNode parent;
BinTravelNode left;
BinTravelNode right;
parent.p=root;
parent.flag=false;
st.push(parent);
while ( !st.empty() )
{
parent=st.top();
st.pop();
if ( parent.p==NULL )
continue;
if ( !parent.flag)
{
left.p= parent.p->lchild;
left.flag=false;
right.p= parent.p->rchild;
right.flag=false;
parent.flag=true;
st.push(right);
st.push(left);
st.push(parent);
}
else
{
print_treenode(parent.p);
}
}
}
void BinaryTree_test() {
/*
A
/ \
B F
\ \
C G
/ \ /
D E H
*/
// 创建上图所示的二叉树
BinaryNode node1= {'A',NULL,NULL};
BinaryNode node2= {'B',NULL,NULL};
BinaryNode node3= {'C',NULL,NULL};
BinaryNode node4= {'D',NULL,NULL};
BinaryNode node5= {'E',NULL,NULL};
BinaryNode node6= {'F',NULL,NULL};
BinaryNode node7= {'G',NULL,NULL};
BinaryNode node8= {'H',NULL,NULL};
//建立节点关系
node1.lchild = &node2;
node1.rchild = &node6;
node2.rchild = &node3;
node6.rchild = &node7;
node3.lchild = &node4;
node3.rchild = &node5;
node7.lchild = &node8;
// pre-order traversal , recursion mode
BinaryTree_preorderTraversal(&node1);
cout << endl;
// pre-order traversal , non-recursion mode
BinaryTree_preorderTraversalUseStack(&node1);
cout << endl;
// 返回这个二叉树的叶子节点数
int num= BinaryTree_leafNodeCount(&node1);
cout << "Total " << num << " leaf node" << endl;
// 返回这个二叉树的高度
int height=BinaryTree_height(&node1);
cout << "The height of binaryTree is " << height << endl;
// 返回一个二叉树的拷贝
BinaryNode* copyTree= BinaryTree_copy(&node1);
// 输出这个二叉树的拷贝
cout << "The copy of binaryTree is ";
BinaryTree_preorderTraversal(copyTree);
cout << endl;
// 将这个二叉树的拷贝销毁
BinaryTree_destroy(copyTree);
}