树的Z型打印
判断是否为满二叉树
将中序遍历改为双向链表
#include<stdio.h>
#include<iostream>
#include<assert.h>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define END '#'
typedef char Elemtype;
typedef struct BtNode
{
BtNode* leftchild;
BtNode* rightchild;
Elemtype data;
}BtNode,*BinaryTree;
int FindPos(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* BuyNode()
{
BtNode* s = (BtNode*)malloc(sizeof(BtNode));
if (s == NULL) exit(1);
memset(s, 0, sizeof(BtNode));
return s;
}
BtNode* CreateIP(const char* is, const char* Is, int n)
{
BtNode* s = NULL;
if (n > 0)
{
s = BuyNode();
s->data = Is[n - 1];
int pos = FindPos(is, n, Is[n - 1]);
s->leftchild = CreateIP(is, Is, pos);
s->rightchild = CreateIP(is + pos + 1, Is + pos, n - pos - 1);
}
return s;
}
BtNode* CreateBTIP(const char* is, const char* Is, int n)//中序后序创建
{
if (is == NULL || Is == NULL || n < 1)return NULL;
else return CreateIP(is, Is, n);
}
void PrintZ(BtNode* ptr)
{
if (ptr == NULL)return;
stack<BtNode*> sta;
stack<BtNode*> stb;
sta.push(ptr);
while (!sta.empty() || !stb.empty())
{
while (!sta.empty())
{
ptr = sta.top(); sta.pop();
cout << ptr->data << " ";
if (ptr->leftchild != NULL)
{
stb.push(ptr->leftchild);
}
if (ptr->rightchild != NULL)
{
stb.push(ptr->rightchild);
}
}
while (!stb.empty())
{
ptr = stb.top(); stb.pop();
cout << ptr->data << " ";
if (ptr->rightchild != NULL)
{
sta.push(ptr->rightchild);
}if (ptr->leftchild != NULL)
{
sta.push(ptr->leftchild);
}
}
}
cout << endl;
}
int Getsize(BtNode* ptr)
{
if (ptr == NULL)return 0;
else return Getsize(ptr->leftchild) + Getsize(ptr->rightchild) + 1;
}
int GetDepth(BtNode* ptr)
{
if (ptr == NULL)return 0;
else return max(GetDepth(ptr->leftchild), GetDepth(ptr->rightchild)) + 1;
}
bool IsFullBiTree(BtNode* ptr)//验证是否是满二叉树
{
if (ptr == NULL)return true;
bool res = true;
queue<BtNode*> qu;
qu.push(ptr);
int n= 1;
while (!qu.empty())
{
if (n != qu.size())
{
res = false;
break;
}
int i = 0;
while (i < n && !qu.empty())
{
ptr = qu.front(); qu.pop();
++i;
if (ptr->leftchild != NULL)qu.push(ptr->leftchild);
if (ptr->rightchild != NULL)qu.push(ptr->rightchild);
}
if (i < n)
{
res = false;
break;
}
n += n;
}
return res;
}
bool IsComBinTree(BtNode* ptr)//判断是否是完全二叉树
{
bool res = true;
if (ptr == NULL)return true;
queue<BtNode*> qu;
qu.push(ptr);
while (!qu.empty())
{
ptr = qu.front();qu.pop();
if (ptr == NULL)break;
qu.push(ptr->leftchild);
qu.push(ptr->rightchild);
}
while (!qu.empty())
{
ptr = qu.front(); qu.pop();
if (ptr !=NULL)
{
res = false;
break;
}
}
return res;
}
BtNode* NiceList(BtNode* ptr)//中序改造而来的中序遍历成双向链表
{
if (ptr == NULL)return NULL;
BtNode* head = NULL;
BtNode* pre = NULL;
stack<BtNode*> st;
while (ptr != NULL || !st.empty())
{
while (ptr != NULL)
{
st.push(ptr);
ptr = ptr->leftchild;
}
ptr = st.top();
st.pop();
if (head == NULL)
{
head = ptr;
pre = ptr;
}
else
{
pre->rightchild = ptr;
ptr->leftchild = pre;
pre = ptr;
}
ptr = ptr->rightchild;
}
return head;
}
void PreOrder(BtNode* ptr)//先序
{
if (ptr != NULL)
{
printf("%c", ptr->data);
PreOrder(ptr->leftchild);
PreOrder(ptr->rightchild);
}
}
int main()
{
const char ps[] = "ABCDEFGH";
const char is[] = "CBEDFAGH";
const char ls[] = "CEFDBHGA";
int n = sizeof(ps) / sizeof(ps[0])-1;//strlen(ps)
BinaryTree root = NULL;
root = CreateBTIP(is, ls, n);
PreOrder(root);
PrintZ(root);
root = NiceList(root);
return 0;
}