//查找
int FindIs(char *Is ,int n ,char val)
{
int pos = -1;
for(int i = 0;i<n;++i)
{
if(is[i] == val)
{
pos = i;
break;
}
}
return pos;
}
//中序后序创建二叉树
BtNode *CreateIL(char *is , char *ls , int n)
{
BtNode *s = NULL;
if(n > 0)
{
s = BuyNode();
s->data = ls[n-1];
int pos = FindIs(Is,n,ls[n-1]);
if(pos == -1) exit(EXIT_FAILURE);
s->leftchild = Create(is,ls,pos);
s->rightchild = CreateIL(is+pos+1 ,ls+pos,n-pos-1);
}
return s;
}
BtNode *CreateTreeIL(char *is , char *ls ,int n)
{
BtNode *root = NULL;
if(is != NULL && ls != NULL && n > 0)
{
root = CreateIL(is,ls,n);
}
return root;
}
//判满
bool Is_Full(BtNode *ptr)
{
return (ptr == NULL) && Is_Full(ptr->leftchild) && Is_Full(ptr->rightchild) && Get_Depth(ptr->leftchild) == Get_Depth(ptr->rightchild));
//二叉树结点个数
int GetSize(BtNode *ptr)
{
if(ptr == nullptr) return 0;
else return GetSize(ptr->leftchild) + GetSize(ptr->rightchild) +1;
}
//二叉树的深度
int Get_Depth(BtNode *ptr)
{
if(ptr == nullptr) return 0;
else return std::max(Get_Depth(ptr->leftchild),Get_Depth(ptr->right));
}
//非递归先序遍历
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);
}
}
cout<<endl;
}
//递归中序遍历
void InOrder(BtNode *ptr)
{
if(ptr != NULL)
{
InOrder(ptr->leftchild);
cout<<ptr->data<<" ";
InOrder(ptr->rightchild);
}
}
//非递归中序遍历
void NiceInOrder(BtNode *ptr)
{
if(ptr == NULL) return;
stack<BtNode *> st;
while(!st.empty() || ptr != NULL)
{
while(ptr != NULL)
{
st.push(ptr);
ptr = ptr->leftchild;
}
ptr = st.top();st.pop();
cout<<ptr->data<<" ";
ptr = ptr->rightchild;
}
cout<<endl;
}
//递归后序遍历:左右根
void StkNicePastOrder(BtNode *ptr)
{
if(ptr == NULL) return ;
stack<StkNode> st;
st.push(SktNode(ptr));
while(!st.empty())
{
StkNode node = st.top();st.pop();
if(++node.popnum == 2)
{
cout<<node,pnode->data<<" ";
if(node.pnode->rightchild != NULL)
{
st.push(StkNode(node , pnode->rightchild));
}
else
{
st.push(node);
if(node.popnum == 1 && node.pnode->leftchild != NULL)
{
st.push(StkNode(node,pnide->leftchild));
}
}
}
}
cout<<endl;
}
//非递归后序遍历
void NicePastOrder(BtNode *ptr)
{
if(ptr == NULL) return;
stack<BtNode *> st;
BtNode *tag = NULL;
while(!st.empty() || ptr != NULL)
{
while(ptr != NULL)
{
st.push(ptr);
ptr = ptr->leftchild;
}
ptr = st.top(); st.pop();
if(ptr->rightchild == NULL)
{
cout<<ptr->data<<" ";
tag = ptr;
ptr = NULL;
}
else
{
st.push(ptr);
ptr = ptr->rightchild;
}
}
cout<<endl;
}
//层次遍历
void NiceLevelOrder(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)
{
qu.push(ptr->leftchild);
}
if(ptr->rightchild)
{
qu.push(ptr->rightchild);
}
}
cout<<endl;
}
//Z字型层次遍历
void ZNiceLevelOrder(BtNode *ptr)
{
if(ptr == NULL) return;
stack<BtNode *> lst;
stack<BtNode *> rea;
lst.push(ptr);
while(!lst.empty() || !res.empty())
{
while(!lst.empty())
{
ptr = lst.top();lst.top();
cout<<ptr->data<<" ";
if(ptr->leftchild != NULL)
{
res.push(ptr->leftchild);
}
if(ptr->rightchild != NULL)
{
res,push(ptr->leftchild);
}
}
}
cout<<endl;
}
//按值查找
BtNode *FindValue(BtNode *ptr , ElemType val)
{
if(ptr == NULL || ptr->data == val)
{
return ptr;
}
else
{
BtNode *p = FindValue(ptr->leftchild , val)
if(nullptr == p)
{
p = FindValue(ptr->rightchild , val);
}
return 0;
}
}
//判断是否为满二叉树:两个队列 1,2,4,8 n+=n
bool Is_Full_BinaryTree(BtNode* ptr)
{
bool res = true;
if(ptr == NULL) return res;
queue<BtNode* >qu1 , qu2;
int n = 1;
qu1.push(ptr);
while(!qu1.empty() || !qu2.empty())
{
if(n != qu1.size()) { res = false; break;}
while(!qu1.empty())
{
BtNode* p = qu1.front();qu1.pop();
if(p->leftchild != NULL) qu2.push(p->leftchild);
if(p->rightchild != NULL) qu2.push(p->rightchild);
}
n+=n;
if(n != qu2.size()) { res = false ; break; }
while(!qu2.empty())
{
BtNode*p = qu2.front();qu2.pop();
if(p->leftchild != NULL) qu1.push(p->leftchild);
if(p->rightchild != NULL) qu1.push(p->rightchild);
}
n+=n;
}
return res;
}
//递归判满二叉树
bool Is_Full(BtNode *ptr)
{
return (ptr == NULL) && Is_Full(ptr->leftchild) && Is_Full(ptr->rightchild) && Get_Depth(ptr->leftchild) == Get_Depth(ptr->rightchild));
}
//判断是否为完全二叉树:一个队列
bool Is_Comp_BinaryTree(BtNode* ptr)
{
bool res = true;
if(ptr == NULL) return res;
queue<BtNode *>qu;
qu.push(ptr);
while(!qu.empty())
{
BtNode* p = qu.front();qu.pop();
if(p == NULL) break;
qu.push(p->leftchild);
qu.push(p->leftchild);
}
while(!qu.empty())
{
BtNode* p = qu.front();qu.pop();
if(p != NULL)
{
res = false;
break;
}
}
return res;
}
//路径问题
bool PrintPath(BtNode *ptr)
{
if(ptr == NULL) return;
vec.push_back(ptr->data);
if(ptr->leftchild == NULL && ptr->leftchild == NULL)
{
int sum = 0;
for(int i = 0;i<vec.size();++i)
{
sum += vec[i];
}
if(sum == val)
{
for(int i = 0; i<vec.size();++i)
{
cout<<vec[i]<<" ";
}
cout<<endl;
}
}
PrintPath(ptr->leftchild,val,vec);
PrintPath(ptr->rightchild.val,vec);
}
//非递归中序遍历改双向链表
void CNiceInOrder(BtNode *ptr)
{
if(ptr == NULL) return;
stack<BtNode *> st;
BtNode *head = NULL;
BtNode *nt = NULL;
while(!st.empty() || !ptr.empty())
{
while(ptr != NULL)
{
st.push(ptr);
ptr = ptr->leftchild;
}
ptr = st.top();st.pop();
if(head == NULL)
{
head = ptr;
nt = ptr;
}
else
{
nt->rightchild = ptr;
ptr->leftchild = nt;
nt = ptr;
}
ptr = ptr->leftchild;
return head;
}
//递归中序遍历变双链表
void InOrder_Lt(BtNode *ptr , BtNode *&pre ,BtNode *&head)
{
if(ptr != NULL)
{
InOrder(ptr->leftchild,pre,head);
if(pre != NULL)
{
pre->rightchild = ptr;
ptr->leftchild = pre;
}else
{
head = ptr;
}
pre = ptr;
InOrder(ptr->rightchild,pre,head);
}
}
void InOrder_List(BtNode *ptr)
{
if(ptr != NULL) return NULL;
BtNode *pre = NULL;
BtNode *head = NULL;
InList(ptr ,pre,head);
return head;
}
//中序遍历二叉树改双向链表(倒序)
void RNiceInOrder(BtNode *ptr)
{
if(ptr == NULL) return;
stack<BtNode *> st;
BtNode *head = NULL;
BtNode *nt = NULL;
while(!st.empty() || !ptr.empty())
{
while(ptr != NULL)
{
st.push(ptr);
ptr = ptr->leftchild;
}
ptr = st.top();st.pop();
if(head == NULL)
{
head = ptr;
}
else
{
nt= ptr->rightchild; //
head->leftchild = ptr;
ptr->rightchild = head;
head = ptr;
}
ptr = nt; //记录未被改变之前的结点
return head;
}