定义节点
#include<iostream>
#include<stack>
using namespace std;
typedef char ElemType;
typedef struct BtNode // BinaryTreeNode
{
BtNode* leftchild;
BtNode* rightchild;
ElemType data;
}BtNode, * BinaryTree;
新建节点
BtNode* Buynode()
{
BtNode* s = (BtNode*)malloc(sizeof(BtNode));
if (nullptr == s) exit(1);
memset(s, 0, sizeof(BtNode));
return s;
}
创建二叉树
int FindIs(const char* is, int n, ElemType val)
{
int pos = -1;
for (int i = 0; i < n; ++i)
{
if (is[i] == val)
{
pos = i;
break;
}
}
return pos;
}
BtNode* CreatePI(const char* pr, const char* is, int n)
{
BtNode* s = nullptr;
if (n > 0)
{
s = Buynode();
s->data = pr[0]; //
int pos = FindIs(is, n, pr[0]);
if (pos == -1) exit(1);
s->leftchild = CreatePI(pr + 1, is, pos);
s->rightchild = CreatePI(pr+pos+1,is+pos+1,n-pos-1);
}
return s;
}
//根据给定的前序遍历和中序遍历结果创建二叉树
BtNode* CreateTreePI(const char* pr, const char* is)
{
if (pr == nullptr || is == nullptr) return nullptr;
int pn = strlen(pr);
int in = strlen(is);
if (pn != in) return nullptr;
return CreatePI(pr, is, pn);
}
BtNode* CreateIP(const char* is, const char* ps, int n)
{
BtNode* s = nullptr;
if (n > 0)
{
s = Buynode();
s->data = ps[n - 1];
int pos = FindIs(is, n, ps[n - 1]);
if (pos == -1) exit(1);
s->leftchild = CreateIP(is, ps, pos);
s->rightchild = CreateIP(is + pos + 1, ps + pos, n - pos - 1);
}
return s;
}
//根据给定的中序遍历和后续遍历结果创建二叉树
BtNode* CreateTreeIP(const char* is, const char* ps)
{
if (is == nullptr || ps == nullptr) return nullptr;
int in = strlen(is);
int pn = strlen(ps);
if (in != pn) return nullptr;
return CreateIP(is, ps, in);
}
int main()
{
BinaryTree root = nullptr;
char pr[] = { "ABCDEFGH" };
char is[] = { "CBEDFAGH" };
char ps[] = { "CEFDBHGA" };
//root = CreateTreePI(pr, is);
root = CreateTreeIP(is, ps);
NicePreOrder(root);
NiceInOrder(root);
NicePastOrder(root);
return 0;
}
递归遍历二叉树
1.前序遍历
void PreOrder(BtNode* ptr)
{
if (ptr != nullptr)
{
cout << ptr->data << " ";
PreOrder(ptr->leftchild);
PreOrder(ptr->rightchild);
}
}
2.中序遍历
void InOrder(BtNode* ptr)
{
if (ptr != nullptr)
{
InOrder(ptr->leftchild);
cout << ptr->data << " ";
InOrder(ptr->rightchild);
}
}
3.后序遍历
void PastOrder(BtNode* ptr)
{
if (ptr != nullptr)
{
PastOrder(ptr->leftchild);
PastOrder(ptr->rightchild);
cout << ptr->data << " ";
}
}
非递归遍历二叉树
1.前序遍历
void NicePreOrder(BtNode* ptr)
{
if (ptr == nullptr) return;
std::stack<BtNode*> st;
st.push(ptr);
while (!st.empty())
{
ptr = st.top(); st.pop();
cout << ptr->data << " ";
if (ptr->rightchild != nullptr)
{
st.push(ptr->rightchild);
}
if (ptr->leftchild != nullptr)
{
st.push(ptr->leftchild);
}
}
cout << endl;
}
2.中序遍历
void NiceInOrder(BtNode* ptr)
{
if (ptr == nullptr) return;
std::stack<BtNode*> st;
while(ptr != nullptr || !st.empty())
{
while (ptr != nullptr)
{
st.push(ptr);
ptr = ptr->leftchild;
}
ptr = st.top(); st.pop();
cout << ptr->data << " ";
ptr = ptr->rightchild;
}
cout << endl;
}
3.后序遍历
void NicePastOrder(BtNode* ptr)
{
if (ptr == nullptr) return;
BtNode* tag = nullptr;
std::stack<BtNode*> st;
while (ptr != nullptr || !st.empty())
{
while (ptr != nullptr)
{
st.push(ptr);
ptr = ptr->leftchild;
}
ptr = st.top(); st.pop();
if (ptr->rightchild == nullptr || ptr->rightchild == tag)
{
cout << ptr->data << " ";
tag = ptr;
ptr = nullptr;
}
else
{
st.push(ptr);
ptr = ptr->rightchild;
}
}
cout << endl;
}
4.层次遍历
void NiceLevelOrder(BtNode* ptr)
{
if (ptr == nullptr) return;
std::queue<BtNode*> qu;
qu.push(ptr);
while (!qu.empty())
{
BtNode* p = qu.front(); qu.pop();
cout << p->data << " ";
if (p->leftchild != nullptr)
{
qu.push(p->leftchild);
}
if (p->rightchild != nullptr)
{
qu.push(p->rightchild);
}
}
cout << endl;
}
5.Z字型遍历
void NiceZLOrder(BtNode* ptr)
{
if (ptr == nullptr) return;
std::stack<BtNode*> sta,stb;
sta.push(ptr);
while (!sta.empty() || !stb.empty())
{
while (!sta.empty())
{
ptr = sta.top(); sta.pop();
cout << ptr->data << " ";
if (ptr->leftchild != nullptr) stb.push(ptr->leftchild);
if (ptr->rightchild != nullptr) stb.push(ptr->rightchild);
}
while (!stb.empty())
{
ptr = stb.top(); stb.pop();
cout << ptr->data << " ";
if (ptr->rightchild != nullptr) sta.push(ptr->rightchild);
if (ptr->leftchild != nullptr) sta.push(ptr->leftchild);
}
}
cout << endl;
}
获取二叉树结点个数
int GetSize(BtNode* ptr)
{
if (ptr == nullptr)
return 0;
else
return GetSize(ptr->leftchild) + GetSize(ptr->rightchild) + 1;
}
获取二叉树深度
int GetDepth(BtNode* ptr)
{
if (ptr == nullptr)
return 0;
else
return std::max(GetDepth(ptr->leftchild), GetDepth(ptr->rightchild)) + 1;
}
int GetNiceDepth(BtNode* ptr)
{
int sum = 0;
if (ptr == nullptr) return sum;
std::queue<BtNode*> qu;
qu.push(ptr);
while (!qu.empty())
{
int n = qu.size();
while (n--)
{
ptr = qu.front(); qu.pop();
if (ptr->leftchild != nullptr) qu.push(ptr->leftchild);
if (ptr->rightchild != nullptr) qu.push(ptr->rightchild);
}
sum += 1;
}
return sum;
}
判断是否是满二叉树
bool Is_Full(BtNode* ptr)
{
int he = 1;
bool res = true;
if (ptr == nullptr) return res;
std::queue<BtNode*> qu;
qu.push(ptr);
while (!qu.empty())
{
int n = qu.size();
if (n != he)
{
res = false;
break;
}
while (n--)
{
ptr = qu.front(); qu.pop();
if (ptr->leftchild != nullptr) qu.push(ptr->leftchild);
if (ptr->rightchild != nullptr) qu.push(ptr->rightchild);
}
he += he;
}
return res;
}
判断是否是完全二叉树
bool Is_Comp(BtNode* ptr)
{
bool res = true;
if (ptr == nullptr) return res;
std::queue<BtNode*> qu;
qu.push(ptr);
while (!qu.empty())
{
ptr = qu.front(); qu.pop();
if (ptr == nullptr) break;
qu.push(ptr->leftchild);
qu.push(ptr->rightchild);
}
while (!qu.empty())
{
ptr = qu.front(); qu.pop();
if (ptr != nullptr)
{
res = false;
break;
}
}
return res;
}
查找值
BtNode* FindValue(BtNode* ptr, ElemType val)
{
if (ptr == nullptr || ptr->data == val)
{
return ptr;
}
else
{
BtNode* p = FindValue(ptr->leftchild, val);
if (p == nullptr)
{
p = FindValue(ptr->rightchild, val);
}
return p;
}
}
查找直接双亲结点
BtNode* Parent(BtNode* ptr, BtNode* child)
{
if (ptr == nullptr || ptr->leftchild == child
|| ptr->rightchild == child)
{
return ptr;
}
else
{
BtNode* p = Parent(ptr->leftchild, child);
if (p == nullptr)
{
p = Parent(ptr->rightchild, child);
}
return p;
}
}
BtNode* FindParent(BtNode* ptr, BtNode* child)
{
if (ptr == nullptr || child == nullptr || ptr == child)
{
return nullptr;
}
else
{
return Parent(ptr, child);
}
}
图论