#include
<
iostream
>
#include
<
queue
>
#include
<
stdlib.h
>
using
namespace
std;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
定义树结构
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
typedef
struct
tree_node_tag
...
{
int value;
struct tree_node_tag *left;
struct tree_node_tag *right;
}
TreeNode;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
通过父节点插入建立二叉树
void
insert_node(TreeNode
*
parent, TreeNode
*
node)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
if (!parent) //插入时必须确保父节点不为空
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
cout << "parent not null" << endl;
return;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (node->value < parent->value) ...{ //比父节点小则插入左子树
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (parent->left != NULL) ...{ //如果左子树还有节点,则继续
insert_node(parent->left, node);
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{ //否则直接插入
parent->left = node;
return;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
else...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (parent->right != NULL) ...{
insert_node(parent->right, node);
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
parent->right = node;
return;
}
}
}
//
分配节点空间
int
new_node(TreeNode
**
node)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
*node=(TreeNode*)malloc(sizeof(TreeNode));
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (*node == NULL) ...{
return -1;
}
(*node)->left=(*node)->right = NULL;
(*node)->value = 0;
return 0;
}
//
创建二叉树
void
create_tree(TreeNode
**
head)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
TreeNode *p=NULL;
int value;
int ret;
TreeNode *node= NULL;
ret = new_node(&node);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if ( ret == -1) ...{
cout << "new_node error" << endl;
return;
}
//指向头节点
p=node;
*head=p;
cin >> value;
node->value = value;
//插入其它节点,以-1为结束标志
while (1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
cin >> value;
if (value == -1) break;
ret = new_node(&node);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if ( ret == -1) ...{
cout << "new_node error" << endl;
return;
}
node->value = value;
insert_node(p,node);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
输出节点
void
print(TreeNode
*
t)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
cout << t->value << " ";
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
void
del_node(TreeNode
*
current)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
free(current);
current = NULL;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
void
preorder(TreeNode
*
t,
void
(
*
pred)(TreeNode
*
))
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (t) ...{
pred(t);
preorder(t->left, pred);
preorder(t->right, pred);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
void
inorder(TreeNode
*
t,
void
(
*
pred)(TreeNode
*
))
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (t) ...{
inorder(t->left, pred);
pred(t);
inorder(t->right, pred);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
void
postorder(TreeNode
*
t,
void
(
*
pred)(TreeNode
*
))
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (t) ...{
postorder(t->left, pred);
postorder(t->right, pred);
pred(t);
}
}
void
hierarchy(TreeNode
*
t,
void
(
*
pred)(TreeNode
*
))
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
queue<TreeNode *> que;
TreeNode *pTmp = NULL;
que.push(t);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
while (!que.empty()) ...{
pTmp = que.front();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (pTmp->left != NULL) ...{
que.push(pTmp->left);
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (pTmp->right != NULL) ...{
que.push(pTmp->right);
}
pred(pTmp);
que.pop();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
void
visit(TreeNode
*
t,
void
(
*
pred1)(TreeNode
*
),
void
(
*
pred)(TreeNode
*
,
void
(
*
)(TreeNode
*
)))
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (t) ...{
pred(t, pred1);
}
}
void
clear(TreeNode
**
head)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
//后序遍历清空
visit(*head, del_node, postorder);
*head = NULL;
}
void
main(
void
)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
TreeNode *head=NULL;
//空头节点
cout << "建立二叉树,节点数据为整型,以-1为输入结束" <<endl;
create_tree(&head);
//前序遍历
cout << "前序遍历" << endl;
visit(head, print, preorder);
cout << endl;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
cout << "中序遍历" << endl;
visit(head, print, inorder);
cout << endl;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
cout << "后序遍历" << endl;
visit(head, print, postorder);
cout << endl;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
cout << "层次遍历" << endl;
visit(head, print,hierarchy);
cout << endl;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
cout << "清空二叉树" << endl;
clear(&head);
visit(head, print, inorder);
cout << endl;
getchar();
}
修正clear函数
void clear(TreeNode *&t)
{
//后序遍历清空
// visit(head, del_node, postorder);
if (t != NULL) {
clear(t->left);
clear(t->right);
free(t);
t = NULL;
}
}
测试
建立二叉树,节点数据为整型,以-1为输入结束
5
3
8
6
1
-1
前序遍历
5 3 1 8 6
中序遍历
1 3 5 6 8
后序遍历
1 3 6 8 5
层次遍历
5 3 8 1 6
清空二叉树