//二叉树的镜像:反转二叉树
void Minerro(BtNode* ps)
{
if(ps == NULL)
return ;
if(ps->leftchild == NULL && ps->rightchild == NULL)
return ;BtNode * s = ps->leftchild;
ps->leftchild = ps->rightchild;
ps->rightchild = s;if(ps->leftchild != NULL)
{
Minerro(ps->leftchild);
}if(ps->rightchild != NULL)
{
Minerro(ps->rightchild);
}
}
int main()
{
char *str="ABC##DE##F##G#H##";
BinaryTree root = NULL;
root = CreateTree(str);
PreOrder(root);
cout<< endl;
InOrder(root);
cout<< endl;
PastOrder(root);
cout<< endl;
Minerro(root);
PreOrder(root);
cout<< endl;
InOrder(root);
cout<< endl;
PastOrder(root);
cout<< endl;}
//得到二叉树某一结点的下一个结点(判断中序遍历的下一个结点)
/*
1、线索化二叉树:
2、规律:
有右子树 :右子树的最左叶子节点
无右子树为右子树 :其父节点为父节点的父节点的左子树,父节点的父节点则为下一个结点
无右子树 :父节点
BtNode * GetNext(BtNode *p)
{
if(p == NULL)
return NULL;BtNode *s = NULL;
if(p->rightchild != NULL)
{
BtNode * right = p->rightchild;
while(right->leftchild != NULL)
right = right->leftchild;
}
else if(p->parent != NULL)
{
BtNode *sc = p;
BtNode *sp = p-> parent;
while(sp!= NULL && sc = sp->rightchild)
{
sc = sp;
sp = sp->parent;
}
s = sp;
}
return s;
}//对称二叉树 //判断一颗二叉树和它的镜像是否相同 即判断一颗二叉树自身是否对称
bool isequals(BtNode * str1,BtNode * str2);
bool isequal(BtNode * str)
{
return isequals(str,str);
}
bool isequals(BtNode * str1,BtNode * str2)
{
if(str1 == NULL && str2== NULL)
return true;
else if(str1 == NULL || str2== NULL)
return false;if(str1->data != str2->data)
return false;return isequals(str1 ->leftchild ,str2->rightchild)
&& isequals(str1->rightchild ,str2->leftchild);
}int main()
{
ElemType *str="ABC###B#C##";
ElemType *str1="ABC##DE##F##G#H##";
BinaryTree root = CreateTree(str);
BinaryTree root1 = CreateTree(str1);
PreOrder(root);
cout<<endl;
PreOrder(root1);
cout<<endl;
cout<< isequal(root)<< endl;
return 0;
}//二叉树改成双向链表
void Convert(BtNode *p,BtNode **s)
{
if(p ==NULL)return ;
BtNode *c = p;
if(c->leftchild != NULL)
Convert(c->leftchild,s);c->leftchild = *s;
if(s != NULL)
(*s)->rightchild = c;*s = c;
if(c->rightchild != NULL)
Convert(c->rightchild,s);
}
BtNode *Conver(BtNode *p)
{
BtNode *s = NULL;
Convert(p,&s);BtNode *head = s;
while(head !=NULL && head->leftchild!= NULL)
{
head = head->leftchild;
}return head;
}
//带父节点的二叉树的公共父节点
struct TreeNode
{
int m_nValue;
std::vector<TreeNode*> m_vChildren;
};bool GetNodePath(const TreeNode* pRoot, const TreeNode* pNode, list<const TreeNode*>& path)
{
if(pRoot == pNode)
return true;
path.push_back(pRoot);
bool found = false;vector<TreeNode*>::const_iterator i = pRoot->m_vChildren.begin();
while(!found && i < pRoot->m_vChildren.end())
{
found = GetNodePath(*i, pNode, path);
++i;
}
if(!found)
path.pop_back();
return found;
}const TreeNode* GetLastCommonNode(const list<const TreeNode*>& path1, const list<const TreeNode*>& path2)
{
list<const TreeNode*>::const_iterator iterator1 = path1.begin();
list<const TreeNode*>::const_iterator iterator2 = path2.begin();
const TreeNode* pLast = nullptr;
while(iterator1 != path1.end() && iterator2 != path2.end())
{
if(*iterator1 == *iterator2)
pLast = *iterator1;
iterator1++;
iterator2++;
}
return pLast;
}const TreeNode* GetLastCommonParent(const TreeNode* pRoot, const TreeNode* pNode1, const TreeNode* pNode2)
{
if(pRoot == nullptr || pNode1 == nullptr || pNode2 == nullptr)
return nullptr;
list<const TreeNode*> path1;
GetNodePath(pRoot, pNode1, path1);
list<const TreeNode*> path2;
GetNodePath(pRoot, pNode2, path2);
return GetLastCommonNode(path1, path2);
}
二叉树_相关问题
最新推荐文章于 2023-02-05 23:38:19 发布