您的位置:首页 > 其它

二叉树的学习(四种遍历方法,搜索,插入,删除等)

2017-08-04 14:48 423 查看
对于任意一棵二叉树,如果其叶结点数为N0,而度数为2的结点总数为N2,则N0=N2+1;

证明过程如下:

假设二叉树的0度,1度,2度结点为n0,n1,n2,总节点数为T(显然,叶节点数就是n0)

则有按照结点求和的

T = n0 + n1 + n2 (1)

按照边求和得:

T = n1 + 2 * n2 + 1 (2)

所以 (2) - (1)可得

n2 + 1 - n0 = 0

所以n0 = n2 + 1。

环境:VS015

#include "stdafx.h"
#include <iostream>
#include <stack>
#include <queue>
using namespace std;

/***************************************
对于任意一棵二叉树,如果其叶结点数为N0,而度数为2的结点总数为N2,则N0=N2+1;
证明过程如下:
假设二叉树的0度,1度,2度结点为n0,n1,n2,总节点数为T(显然,叶节点数就是n0)
则有按照结点求和的
T = n0 + n1 + n2 (1)
按照边求和得:
T = n1 + 2 * n2 + 1 (2)
所以 (2) - (1)可得
n2 + 1 - n0 = 0
所以n0 = n2 + 1
***************************************/
//根每一棵子树是根的儿子(child),根叫做子树的父亲(parent),没有儿子的节点叫做树叶(leaf),
//具有相同的父亲的节点叫做兄弟(sibling)
/******************************
The defination of tree 树的定义
结构体
******************************/
typedef struct TreeNode BinTree;
struct TreeNode
{
int ex;
//the address of the first child.
TreeNode *Left;
//the address of the next sibling
TreeNode *Right;
};

/******************************
功能:(递归)前序遍历二叉树(先根,再右左边,再右边)
输入:二叉树根节点
输出:输出节点数据
******************************/
void preOrderTraversal(TreeNode *BootNode)
{
if (BootNode)
{
cout << BootNode->ex;
preOrderTraversal(BootNode->Left);
preOrderTraversal(BootNode->Right);
}
}
/******************************
功能:(递归)中序遍历二叉树(先左边,再根,再右边)
输入:二叉树根节点
输出:输出节点数据
******************************/
void inOrderTraversal(TreeNode *BootNode)
{
if (BootNode)
{
inOrderTraversal(BootNode->Left);
cout << BootNode->ex;
inOrderTraversal(BootNode->Right);
}
}
/******************************
功能:(递归)后序遍历二叉树(先左边,再右边,再根)
输入:二叉树根节点
输出:输出节点数据
******************************/
void  postOrderTraversal(TreeNode *BootNode)
{
if (BootNode)
{
postOrderTraversal(BootNode->Left);
cout << BootNode->ex;
postOrderTraversal(BootNode->Right);
}
}

/******************************
功能:堆栈实现中序遍历
输入:二叉树根节点
输出:输出节点数据
******************************/
void inOrderTraversal_stack(TreeNode *BootNode)
{
TreeNode *Tree;//创建新根
Tree = BootNode;
Tree = new TreeNode;
stack<TreeNode> S;//创建堆栈
while (Tree || !S.empty())
{
while(Tree)
{
S.push(*Tree);//入栈
inOrderTraversal_stack(Tree->Left);//左边走到尽头
}
if (!S.empty())
{
*Tree = S.top();//取栈顶元素
S.pop();//出栈
cout << Tree->ex;//输出
Tree = Tree->Right;//找这个节点(左边节点已经找完了的)的右边节点的子节点
}
}
}

/******************************
功能:层序遍历(一层一层的输出)
输入:二叉树根节点
输出:输出节点数据
******************************/
void  levelOrderTraversal(TreeNode *BootNode)
{
if (!BootNode) return;
queue<TreeNode> Q;
TreeNode* Tree = NULL;
Q.push(*BootNode);
while (!Q.empty())
{
*Tree = Q.front();
cout << Tree->ex<<endl;
Q.pop();
if(Tree->Left) Q.push(*(Tree->Left));
if (Tree->Right) Q.push(*(Tree->Right));
}
}

/******************************
功能:二叉搜索树(尾递归)
输入:搜索值,二叉树根节点
输出:返回节点
******************************/
TreeNode*  findPosition1(int x,TreeNode *BootNode)
{
if (!BootNode) return NULL;
if (x > BootNode->ex) return findPosition1(x, BootNode->Right);
else if (x < BootNode->ex) return findPosition1(x, BootNode->Left);
else return BootNode;

}

/******************************
功能:二叉搜索树(迭代函数)
输入:搜索值,二叉树根节点
输出:返回节点
******************************/
TreeNode*  findPosition2(int x, TreeNode *BootNode)
{

while (BootNode) {
if (x > BootNode->ex)
BootNode = BootNode->Right;
else if (x < BootNode->ex)
BootNode = BootNode->Left;
else return BootNode;
}
return NULL;

}

/******************************
功能:查找二叉树最小元素(递归)
输入:搜索值,二叉树根节点
输出:返回节点
******************************/
TreeNode*  findMin(TreeNode *BootNode)
{
if (!BootNode) return NULL;
if (!BootNode->Left) return BootNode;
else  return findMin(BootNode->Left);

}

/******************************
功能:查找二叉树最大元素(递归)
输入:搜索值,二叉树根节点
输出:返回节点
******************************/
TreeNode*  findMax(TreeNode *BootNode)
{
if (!BootNode)
return NULL;
if (!BootNode->Right)
return BootNode;
else
return findMax(BootNode->Right);

}

/******************************
功能:二叉搜索树的插入(递归)
输入:搜索值,二叉树根节点
输出:返回节点
******************************/
TreeNode*  insert(int x,TreeNode *BootNode)
{
if (!BootNode)
{
BootNode = new TreeNode;
BootNode->ex = x;
BootNode->Left = BootNode->Right = NULL;
}
else
{
if (BootNode->ex < x)
{
BootNode->Right=insert(x, BootNode->Right);
}
else
BootNode->Left=insert(x, BootNode->Left);
}
return BootNode;
}

/******************************
功能:二叉搜索树的s删除(递归)
输入:要删除的值,二叉树根节点
输出:返回节点
******************************/
TreeNode*  deleteN(int x, TreeNode *BootNode)
{
TreeNode *temp;
temp = new TreeNode;
if (!BootNode) cout << "NULL Value";
else if (x < BootNode->ex)
BootNode->Left = deleteN(x, BootNode->Left);
else
{
if (BootNode->Left && BootNode->Right)
{
temp = findPosition1(x, BootNode->Right);
BootNode->ex = temp->ex;
BootNode->ex = temp->ex;
BootNode->Right = deleteN(BootNode->ex, BootNode->Right);
}
else
{
temp = BootNode;
if (!BootNode->Left)
BootNode =  BootNode->Right;
else if(BootNode->Right)
BootNode = BootNode->Left;
delete(temp);
}
}
return BootNode;
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐