我们在这里讨论一下二叉树的一种 :顺序二叉树
特性:
1.二叉排序树中不允许出现相同的值的节点
2.节点的值从小到大 左 根 右分布
每一个跟的左的所有节点的值都小于根的值
每一个跟的右的所有节点的值都大于根的值
我们要对这样一个二叉树进行添加函数的编写
void AddBST(BST*& rpBoot, int id)
参数为 二叉树根的引用 和我们要添加的节点的值
void AddBST(BST*& rpBoot, int id)
{
BST* pTemp = new BST;
pTemp->id = id;
pTemp->pLeft = nullptr;
pTemp->pRight = nullptr;
if (rpBoot == NULL)
{
rpBoot = pTemp;
return;
}
BST* pMark = rpBoot;
while (pMark!=NULL)
{
if (pTemp->id < pMark->id)
{
if (pMark->pLeft == NULL)
{
pMark->pLeft = pTemp;
return;
}
pMark = pMark->pLeft;
}
else if (pTemp->id > pMark->id)
{
if (pMark->pRight == NULL)
{
pMark->pRight = pTemp;
return;
}
pMark = pMark->pRight;
}
else if (pTemp->id == pMark->id)
{
cout << "添加节点的值错误" << endl;
return;
}
}
}
首先常见一个节点pTemp用于添加
然后判断根节点是否为空
为空就把pTemp赋给根节点
不为空就对二叉树进行遍历
从上往下走
若添加的节点的值大于当前根节点就往根的右走
若添加的节点的值小于当前根节点就往根的左走
直到走到当前根结点没有左(右)时
就把当前pTemo赋给根结点的左(右)
如果在二叉树中找到了与添加节点的值相同的节点
报错 添加失败
关于验证 我们用中序遍历函数验证
关于二叉树遍历的相关知识我们之前的文章有介绍过 这里就不一一概述
数据结构 二叉树的遍历 学习笔记_van9527的博客-CSDN博客
void zhongxubianli(BST* pRoot)
{
if (NULL == pRoot)
return;
stack<BST*>st;
while (1)
{
while (pRoot)
{
st.push(pRoot);
pRoot = pRoot->pLeft;
}
if (st.empty() == true)
{
cout << endl;
return;
}
pRoot = st.top();
st.pop();
cout << pRoot->id << " ";
pRoot = pRoot->pRight;
}
}
在主函数中 我们用添加函数添加了几个这样的节点
BST* pBoot = nullptr;
BST* pFather = nullptr;
AddBST(pBoot,3);
AddBST(pBoot, 6);
AddBST(pBoot, 2);
AddBST(pBoot, 4);
AddBST(pBoot, 7);
经过中序遍历 zhongxubianli(pBoot);
执行结果如下:
BST* Serch(BST* rpBoot, int id, BST*& Father)
同时 我们又对顺序二叉树编写了一个函数用来查找二叉树中是否有值和这个数相同的节点
返回的是这个我们要找的节点 参数是根二叉树节点 我们要找的节点的值 要找的节点的父节点
BST* Serch(BST* rpBoot, int id, BST*& Father)
{
BST* fMark = rpBoot;
if (rpBoot == NULL)
{
cout << "二叉树为空" << endl;
return 0;
}
while (rpBoot != NULL)
{
if (id == rpBoot->id)
{
cout << "找到了" << endl;
Father = fMark;
return rpBoot;
}
if (id < rpBoot->id)
{
fMark= rpBoot;
rpBoot = rpBoot->pLeft;
}
else if (id > rpBoot->id)
{
fMark = rpBoot;
rpBoot = rpBoot->pRight;
}
}
cout << "没有" << endl;
}
主函数中
BST* pBoot = nullptr;
BST* pFather = nullptr;
AddBST(pBoot,3);
AddBST(pBoot, 6);
AddBST(pBoot, 2);
AddBST(pBoot, 4);
AddBST(pBoot, 7);
zhongxubianli(pBoot);
cout << Serch(pBoot, 7, pFather)->id << endl;
cout << "父亲节点" << pFather->id << endl;
cout << Serch(pBoot, 6, pFather)->id << endl;
cout << "父亲节点" << pFather->id << endl;
执行结果
完整代码如下
#include<iostream>
#include<stack>
using namespace std;
//二叉排序树中不允许出现相同的值的节点
struct BST
{
int id;
BST* pLeft;
BST* pRight;
};
void AddBST(BST*& rpBoot, int id)
{
BST* pTemp = new BST;
pTemp->id = id;
pTemp->pLeft = nullptr;
pTemp->pRight = nullptr;
if (rpBoot == NULL)
{
rpBoot = pTemp;
return;
}
BST* pMark = rpBoot;
while (pMark!=NULL)
{
if (pTemp->id < pMark->id)
{
if (pMark->pLeft == NULL)
{
pMark->pLeft = pTemp;
return;
}
pMark = pMark->pLeft;
}
else if (pTemp->id > pMark->id)
{
if (pMark->pRight == NULL)
{
pMark->pRight = pTemp;
return;
}
pMark = pMark->pRight;
}
else if (pTemp->id == pMark->id)
{
cout << "添加节点的值错误" << endl;
return;
}
}
}
BST* Serch(BST* rpBoot, int id, BST*& Father)
{
BST* fMark = rpBoot;
if (rpBoot == NULL)
{
cout << "二叉树为空" << endl;
return 0;
}
while (rpBoot != NULL)
{
if (id == rpBoot->id)
{
cout << "找到了" << endl;
Father = fMark;
return rpBoot;
}
if (id < rpBoot->id)
{
fMark= rpBoot;
rpBoot = rpBoot->pLeft;
}
else if (id > rpBoot->id)
{
fMark = rpBoot;
rpBoot = rpBoot->pRight;
}
}
cout << "没有" << endl;
}
void zhongxubianli(BST* pRoot)
{
if (NULL == pRoot)
return;
stack<BST*>st;
while (1)
{
while (pRoot)
{
st.push(pRoot);
pRoot = pRoot->pLeft;
}
if (st.empty() == true)
{
cout << endl;
return;
}
pRoot = st.top();
st.pop();
cout << pRoot->id << " ";
pRoot = pRoot->pRight;
}
}
int main()
{
BST* pBoot = nullptr;
BST* pFather = nullptr;
AddBST(pBoot,3);
AddBST(pBoot, 6);
AddBST(pBoot, 2);
AddBST(pBoot, 4);
AddBST(pBoot, 7);
zhongxubianli(pBoot);
cout << Serch(pBoot, 7, pFather)->id << endl;
cout << "父亲节点" << pFather->id << endl;
cout << Serch(pBoot, 6, pFather)->id << endl;
cout << "父亲节点" << pFather->id << endl;
return 0;
}