typedef char ElemType;
typedef struct BtNode//BinaryTreeNode
{
ElemType data;
struct BtNode* leftchild;
struct BtNode* rightchild;
}BtNode,*BinaryTree;
struct StkNode
{
BtNode* pnode;
int pos;
public:
StkNode(BtNode* p) :pnode(p), pos(0) {}
};
BtNode* Buynode()
{
BtNode* s = (BtNode*)malloc(sizeof(BtNode));
if (NULL == s)exit(1);
memset(s, 0, sizeof(BtNode));
return s;
}
//中序遍历
void InOrder(BtNode* p)
{
if (p != NULL)
{
InOrder(p->leftchild);
cout << p->data << " ";
InOrder(p->rightchild);
}
}
void NiceInOrder(BtNode* ptr)
{
if (ptr == NULL) return;
std::stack<BtNode*> st;
while (ptr != NULL || !st.empty())
{
while (ptr != NULL)
{
st.push(ptr);
ptr = ptr->leftchild;
}
ptr = st.top();
st.pop();
cout << ptr->data;
ptr = ptr->rightchild;
}
}
void StkNiceInOrder(BtNode* ptr)
{
if (ptr == NULL) return;
stack<StkNode> st;
st.push(StkNode(ptr));
while (!st.empty())
{
StkNode node = st.top(); st.pop();
if (++node.pos == 2)//中序遍历,当第二次出栈时打印
{
cout << node.pnode->data;
if (node.pnode->rightchild != NULL)//可能还存在右子树
{
st.push(StkNode(node.pnode->rightchild));
}
}
else
{
st.push(node);//记得入栈
if (node.pnode->leftchild != NULL)
{
st.push(StkNode(node.pnode->leftchild));
}
}
}
}
//先序遍历
void PreOrder(BtNode* p)
{
if (p != NULL)
{
cout << p->data << " ";
PreOrder(p->leftchild);
PreOrder(p->rightchild);
}
}
void NicePreOrder(BtNode* ptr)
{
if (ptr == NULL) return;
stack<BtNode*> st;
st.push(ptr);
while (!st.empty())
{
ptr = st.top();
st.pop();
cout << ptr->data;
if (ptr->rightchild != NULL)
{
st.push(ptr->rightchild);
}
if (ptr->leftchild != NULL)
{
st.push(ptr->leftchild);
}
}
}
//后序遍历
void PastOrder(BtNode* p)
{
if (p != NULL)
{
PastOrder(p->leftchild);
PastOrder(p->rightchild);
cout << p->data << " ";
}
}
void NicePastOrder(BtNode* ptr)
{
if (ptr == NULL) return;
std::stack<BtNode*> st;
BtNode* tag = NULL;
while (ptr != NULL || !st.empty())
{
while (ptr != NULL)
{
st.push(ptr);
ptr = ptr->leftchild;
}
ptr = st.top();
st.pop();
if (ptr->rightchild == NULL || ptr->rightchild == tag)
{
cout << ptr->data;
tag = ptr;
ptr = NULL;
}
else
{
st.push(ptr);
ptr =ptr->rightchild;
}
}
}
void StkNicePastOrder(BtNode* ptr)
{
if (ptr == NULL) return;
stack<StkNode> st;
st.push(StkNode(ptr));
while (!st.empty())
{
StkNode node = st.top(); st.pop();
if (++node.pos == 3)//后序遍历,当出栈3次时,打印
{
cout << node.pnode->data;
}
else
{
st.push(node);//记得入栈
if (node.pos == 1 && node.pnode->leftchild != NULL)
{
st.push(StkNode(node.pnode->leftchild));
}
else if (node.pos == 2 && node.pnode->rightchild != NULL)
{
st.push(StkNode(node.pnode->rightchild));
}
}
}
}
BtNode* CBTree1()
{
BtNode* s = NULL;
ElemType elem;
cin >> elem;
if (elem != '#')
{
s = Buynode();
s->data = elem;
s->leftchild = CBTree1();
s->rightchild = CBTree1();
}
return s;
}
int FindIs(const char* is, int n, char ch)
{
int pos = -1;
for (int i = 0; i < n; ++i)
{
if (is[i] == ch)
{
pos = i;
break;
}
}
return pos;
}
BtNode* CreatePI(const char* ps, const char* is, int n)
{
BtNode* s = NULL;
if (n > 0)//还有数据
{
s = Buynode();//购买一个节点
s->data = ps[0];
int pos = FindIs(is, n, ps[0]);
if (pos == -1) exit(1);
s->leftchild = CreatePI(ps+1,is,pos);
s->rightchild = CreatePI(ps+pos+1,is+pos+1,n-pos-1);
}
return s;
}
BtNode* CreateIL(const char* is, const char* ls, int n)
{
BtNode* s = NULL;
if (n > 0)//还有数据
{
s = Buynode();//购买一个节点
s->data = ls[n-1];//下标是从0开始的,最后一个元素的下标是n-1
int pos = FindIs(is, n, ls[n-1]);
if (pos == -1) exit(1);
s->leftchild = CreateIL(is, ls , pos);
s->rightchild = CreateIL(is + pos + 1, ls + pos,n-pos-1);
}
return s;
}
//根据先序和中序可以推测出结构
BtNode* CreateBinartTreePI(const char*ps, const char* is, int n)
{
if (ps == NULL || is == NULL || n <= 0) return NULL;
else return CreatePI(ps, is, n);
}
//根据后序和中序可以推测出结构
BtNode* CreateBinartTreeIL(const char* is, const char* ls, int n)
{
if (is == NULL || ls == NULL || n <= 0) return NULL;
else return CreateIL(is,ls, n);
}
//层次遍历,从左往右一层一层输出
void LevelOrder(BtNode* ptr)
{
if (ptr == NULL) return;
queue<BtNode*> qu;
qu.push(ptr);
while (!qu.empty())
{
ptr = qu.front();
qu.pop();
cout << ptr->data;
if (ptr->leftchild != NULL)
{
qu.push(ptr->leftchild);
}
if (ptr->rightchild != NULL)
{
qu.push(ptr->rightchild);
}
}
}
//计算二叉树节点的个数count,左子树的个数+右子树的个数+1
int Count(BtNode* ptr)
{
if (ptr == NULL) return 0;
return Count(ptr->leftchild) + Count(ptr->rightchild) + 1;
}
//计算二叉树的深度depth,左子树和右子树中深度大的+1
int Depth(BtNode* ptr)
{
if (ptr == NULL) return 0;
return std::max(Depth(ptr->leftchild),Depth(ptr->rightchild)) + 1;
}
//Z型打印
void ZLevelOrder(BtNode* ptr)
{
if (ptr == NULL) return;
stack<BtNode*> ast;
stack<BtNode*> bst;
ast.push(ptr);
while (!ast.empty() || !bst.empty())
{
while (!ast.empty())
{
ptr = ast.top(); ast.pop();
cout << ptr->data;
if (ptr->leftchild != NULL)
{
bst.push(ptr->leftchild);
}
if (ptr->rightchild != NULL)
{
bst.push(ptr->rightchild);
}
}
while (!bst.empty())
{
ptr = bst.top(); bst.pop();
cout << ptr->data;
if (ptr->rightchild != NULL)
{
ast.push(ptr->rightchild);
}
if (ptr->leftchild != NULL)
{
ast.push(ptr->leftchild);
}
}
}
}
//判断是否是满二叉树
bool Is_FullBiary(BtNode* ptr)
{
if (ptr == NULL) return true;//空树是满二叉树
bool tag = true;
queue<BtNode*> aqu;
queue<BtNode*> bqu;
int s = 1;
aqu.push(ptr);
while (!aqu.empty() || !bqu.empty())
{
if (aqu.size() != s)
{
tag = false;
break;
}
while (!aqu.empty())
{
ptr = aqu.front(); aqu.pop();
if (ptr->leftchild != NULL)
{
bqu.push(ptr->leftchild);
}
if (ptr->rightchild != NULL)
{
bqu.push(ptr->rightchild);
}
}
s += s;
if (s != bqu.size())
{
tag = false;
break;
}
while (!bqu.empty())
{
ptr = bqu.front(); bqu.pop();
if (ptr->leftchild != NULL)
{
aqu.push(ptr->leftchild);
}
if (ptr->rightchild != NULL)
{
aqu.push(ptr->rightchild);
}
}
s += s;
}
return tag;
}
int main()
{
char ps[] = { "ABCDEFGH" };
char is[] = { "CBEDFAGH" };
char ls[] = { "CEFDBHGA" };
int n = strlen(ps);
BinaryTree root = NULL;
//root = CreateBinartTreePI(ps, is, n);
root = CreateBinartTreeIL(is, ls, n);
NicePreOrder(root);
cout << endl;
StkNiceInOrder(root);
cout << endl;
StkNicePastOrder(root);
cout << endl;
LevelOrder(root);
cout << endl;
cout << "节点个数:" << Count(root) << endl;
cout << "树的深度为:" << Depth(root) << endl;
ZLevelOrder(root);
cout << endl;
if (Is_FullBiary(root)) { cout << "是" << endl; }
else { cout << "不是" << endl; }
return 0;
}
265-C++ 二叉树代码
最新推荐文章于 2024-07-26 12:28:02 发布