//
此代码为
多叉树转化为
二叉树并且二叉树转化为多叉树的代码
#include
"stdafx.h"
#include
#include
//
树的结点分布信息
struct
SCreateNormalTree
{
int
nData
;
int
nPrev
;
// -1
为跟结点
};
const
static
int
_NORMAL_TREE_NODE_NUM_
= 9;
SCreateNormalTree
createNormalTreeMap
[
_NORMAL_TREE_NODE_NUM_
];
void
CreateNormalTreeMap
()
{
createNormalTreeMap
[0].
nData
= 5;
createNormalTreeMap
[0].
nPrev
= -1;
createNormalTreeMap
[1].
nData
= 3;
createNormalTreeMap
[1].
nPrev
= 5;
createNormalTreeMap
[2].
nData
= 4;
createNormalTreeMap
[2].
nPrev
= 5;
createNormalTreeMap
[3].
nData
= 2;
createNormalTreeMap
[3].
nPrev
= 5;
createNormalTreeMap
[4].
nData
= 8;
createNormalTreeMap
[4].
nPrev
= 3;
createNormalTreeMap
[5].
nData
= 10;
createNormalTreeMap
[5].
nPrev
= 3;
createNormalTreeMap
[6].
nData
= 11;
createNormalTreeMap
[6].
nPrev
= 3;
createNormalTreeMap
[7].
nData
= 12;
createNormalTreeMap
[7].
nPrev
= 2;
createNormalTreeMap
[8].
nData
= 100;
createNormalTreeMap
[8].
nPrev
= 5;
}
//
树的结点分布信息
//
多叉树
struct
SNormalTreeNode
{
int
nData
;
//
元素的值
SNormalTreeNode
*
pParent
;
//
双亲结点
std
::
vector
<
SNormalTreeNode
*>
vecChildNode
;
//
孩子结点
};
struct
SBinaryTreeNode
//
二叉孩子兄弟链表
{
int
nData
;
//
元素的值
SBinaryTreeNode
*
pParent
;
//
双亲结点
SBinaryTreeNode
*
pFirstChild
;
//
孩子结点
(
左孩子
)
SBinaryTreeNode
*
pSibling
;
//
兄弟结点
(
右兄弟
)
};
SNormalTreeNode
*
InitializeNormalTree
( );
SNormalTreeNode
*
InsertValueToNormalTree
(
SNormalTreeNode
*
pRoot
,
int
nValue
,
int
nParent
);
SNormalTreeNode
*
FindNodeByValue
(
SNormalTreeNode
*
pRoot
,
int
nValue
);
void
TravarseNormalTree
(
SNormalTreeNode
*
pRoot
);
//
多叉树转化为二叉树
SBinaryTreeNode
*
GetBinary
(
SNormalTreeNode
*
pNormalTree
);
void
ConvertNormalToBinary
(
SBinaryTreeNode
*
pBinaryTree
,
SNormalTreeNode
*
pNormalTree
);
void
InOrderBinaryTree
(
SBinaryTreeNode
*
pBinaryTree
);
//
同时将二叉树转化为多叉树
SNormalTreeNode
*
GetNormalTreeFromBinary
(
SBinaryTreeNode
*
pBinaryTree
);
void
ConvertFromBinaryToNormal
(
SBinaryTreeNode
*
pBinaryTree
,
SNormalTreeNode
*
pNormalTree
);
int
_tmain
(
int
argc
,
_TCHAR
*
argv
[])
{
CreateNormalTreeMap
();
SNormalTreeNode
*
pNormalTree
=
InitializeNormalTree
();
TravarseNormalTree
(
pNormalTree
);
SBinaryTreeNode
*
pBinaryTree
=
GetBinary
(
pNormalTree
);
InOrderBinaryTree
(
pBinaryTree
);
SNormalTreeNode
*
pConvertNormal
=
GetNormalTreeFromBinary
(
pBinaryTree
);
TravarseNormalTree
(
pConvertNormal
);
system
(
"pause"
);
return
0;
}
SNormalTreeNode
*
InitializeNormalTree
()
//
多叉树
InitializeNormalTree()
{
SNormalTreeNode
*
pNormalTree
=
NULL
;
for
(
int
i
= 0;
i
<
_NORMAL_TREE_NODE_NUM_
; ++
i
)
{
pNormalTree
=
InsertValueToNormalTree
(
pNormalTree
,
createNormalTreeMap
[
i
].
nData
,
createNormalTreeMap
[
i
].
nPrev
);
}
return
pNormalTree
;
}
SNormalTreeNode
*
InsertValueToNormalTree
(
SNormalTreeNode
*
pRoot
,
int
nValue
,
int
nParent
)
{
if
( !
pRoot
)
{
if
(
nParent
!= -1 )
{
return
NULL
;
}
}
if
(
nParent
== -1 )
{
pRoot
=
NULL
;
pRoot
=
new
SNormalTreeNode
;
pRoot
->
nData
=
nValue
;
pRoot
->
pParent
=
NULL
;
}
else
{
if
( !
pRoot
)
{
return
NULL
;
}
SNormalTreeNode
*
pParent
=
FindNodeByValue
(
pRoot
,
nParent
);
SNormalTreeNode
*
pNewNode
=
new
SNormalTreeNode
;
pNewNode
->
nData
=
nValue
;
pNewNode
->
pParent
=
pParent
;
pParent
->
vecChildNode
.
push_back
(
pNewNode
);
}
return
pRoot
;
}
SNormalTreeNode
*
FindNodeByValue
(
SNormalTreeNode
*
pRoot
,
int
nValue
)
{
if
( !
pRoot
)
return
NULL
;
if
(
pRoot
->
nData
==
nValue
)
return
pRoot
;
std
::
vector
<
SNormalTreeNode
*>::
iterator
iBegin
=
pRoot
->
vecChildNode
.
begin
();
std
::
vector
<
SNormalTreeNode
*>::
iterator
iEnd
=
pRoot
->
vecChildNode
.
end
();
for
( ;
iBegin
!=
iEnd
; ++
iBegin
)
{
SNormalTreeNode
*
pChildNode
= (*
iBegin
);
if
(
pChildNode
)
{
if
(
pChildNode
=
FindNodeByValue
(
pChildNode
,
nValue
) )
return
pChildNode
;
}
}
return
NULL
;
}
void
TravarseNormalTree
(
SNormalTreeNode
*
pRoot
)
{
if
( !
pRoot
)
return
;
std
::
cout
<<
"
当前结点为
: "
<<
pRoot
->
nData
<<
std
::
endl
;
std
::
cout
<<
"
子结点为
: "
;
std
::
vector
<
SNormalTreeNode
*>::
iterator
iBegin
=
pRoot
->
vecChildNode
.
begin
();
std
::
vector
<
SNormalTreeNode
*>::
iterator
iEnd
=
pRoot
->
vecChildNode
.
end
();
for
( ;
iBegin
!=
iEnd
; ++
iBegin
)
{
SNormalTreeNode
*
pChild
= (*
iBegin
);
if
(
pChild
)
std
::
cout
<<
" "
<<
pChild
->
nData
;
}
std
::
cout
<<
std
::
endl
;
iBegin
=
pRoot
->
vecChildNode
.
begin
();
for
( ;
iBegin
!=
iEnd
; ++
iBegin
)
{
SNormalTreeNode
*
pChild
= (*
iBegin
);
if
(
pChild
)
TravarseNormalTree
(
pChild
);
}
}
SBinaryTreeNode
*
GetBinary
(
SNormalTreeNode
*
pNormalTree
)
{
if
( !
pNormalTree
)
return
NULL
;
//
根结点
SBinaryTreeNode
*
pBinaryNewNode
=
new
SBinaryTreeNode
;
pBinaryNewNode
->
nData
=
pNormalTree
->
nData
;
pBinaryNewNode
->
pParent
=
NULL
;
pBinaryNewNode
->
pSibling
=
NULL
;
pBinaryNewNode
->
pFirstChild
=
NULL
;
ConvertNormalToBinary
(
pBinaryNewNode
,
pNormalTree
);
return
pBinaryNewNode
;
}
void
ConvertNormalToBinary
(
SBinaryTreeNode
*
pBinaryNode
,
SNormalTreeNode
*
pNormalTree
)
{
if
( !
pNormalTree
|| !
pBinaryNode
)
return
;
SBinaryTreeNode
*
pFirstChild
=
NULL
;
std
::
vector
<
SNormalTreeNode
*>::
iterator
iLeftChild
=
pNormalTree
->
vecChildNode
.
begin
();
std
::
vector
<
SNormalTreeNode
*>::
iterator
iEnd
=
pNormalTree
->
vecChildNode
.
end
();
if
(
iLeftChild
==
iEnd
)
return
;
else
{
SNormalTreeNode
*
pNormalSubTree
= (*
iLeftChild
);
pFirstChild
=
new
SBinaryTreeNode
;
pFirstChild
->
nData
=
pNormalSubTree
->
nData
;
pFirstChild
->
pFirstChild
=
NULL
;
pFirstChild
->
pParent
=
pBinaryNode
;
pFirstChild
->
pSibling
=
NULL
;
pBinaryNode
->
pFirstChild
=
pFirstChild
;
ConvertNormalToBinary
(
pFirstChild
,
pNormalSubTree
);
}
std
::
vector
<
SNormalTreeNode
*>::
iterator
iSiblingBegin
=
iLeftChild
+ 1;
if
(
iSiblingBegin
==
iEnd
)
return
;
else
{
SBinaryTreeNode
*
pLinkNode
=
pFirstChild
;
for
( ;
iSiblingBegin
!=
iEnd
; ++
iSiblingBegin
)
{
SNormalTreeNode
*
pSubBeginNormal
= (*
iSiblingBegin
);
SBinaryTreeNode
*
pNewBinaryNode
=
new
SBinaryTreeNode
;
pNewBinaryNode
->
nData
=
pSubBeginNormal
->
nData
;
pNewBinaryNode
->
pFirstChild
=
NULL
;
pNewBinaryNode
->
pParent
=
pLinkNode
;
pNewBinaryNode
->
pSibling
=
NULL
;
pLinkNode
->
pSibling
=
pNewBinaryNode
;
ConvertNormalToBinary
(
pNewBinaryNode
,
pSubBeginNormal
);
pLinkNode
=
pNewBinaryNode
;
}
}
}
void
InOrderBinaryTree
(
SBinaryTreeNode
*
pBinaryTree
)
{
if
( !
pBinaryTree
)
return
;
InOrderBinaryTree
(
pBinaryTree
->
pFirstChild
);
SBinaryTreeNode
*
pParent
=
pBinaryTree
->
pParent
;
if
(
pParent
==
NULL
)
std
::
cout
<<
" Root Node "
;
else
{
if
(
pParent
->
pFirstChild
==
pBinaryTree
)
std
::
cout
<<
" ParentNode "
<<
pParent
->
nData
<<
" Left Child"
;
else
std
::
cout
<<
" ParentNode "
<<
pParent
->
nData
<<
" Right Child"
;
}
std
::
cout
<<
" node Value is"
<<
pBinaryTree
->
nData
<<
std
::
endl
;
InOrderBinaryTree
(
pBinaryTree
->
pSibling
);
}
SNormalTreeNode
*
GetNormalTreeFromBinary
(
SBinaryTreeNode
*
pBinaryTree
)
{
if
( !
pBinaryTree
)
return
NULL
;
SNormalTreeNode
*
pRootNode
=
new
SNormalTreeNode
;
pRootNode
->
nData
=
pBinaryTree
->
nData
;
pRootNode
->
pParent
=
NULL
;
ConvertFromBinaryToNormal
(
pBinaryTree
->
pFirstChild
,
pRootNode
);
return
pRootNode
;
}
void
ConvertFromBinaryToNormal
(
SBinaryTreeNode
*
pBinaryTree
,
SNormalTreeNode
*
pNormalTree
)
{
if
( !
pBinaryTree
|| !
pNormalTree
)
return
;
do
{
SNormalTreeNode
*
pNormalBegin
=
new
SNormalTreeNode
;
pNormalBegin
->
nData
=
pBinaryTree
->
nData
;
pNormalBegin
->
pParent
=
pNormalTree
;
pNormalTree
->
vecChildNode
.
push_back
(
pNormalBegin
);
ConvertFromBinaryToNormal
(
pBinaryTree
->
pFirstChild
,
pNormalBegin
);
pBinaryTree
=
pBinaryTree
->
pSibling
;
}
while
(
pBinaryTree
);
}
转化前的多叉树
转化后的二叉树
二叉树 再次 转化为 多叉树