您的位置:首页 > 理论基础 > 数据结构算法

好久没有复习数据结构了 简单复习树转双链表

2016-06-19 18:57 423 查看
创建一个结构体

C语言风格 写起来比较舒服

void CreateBitree(BsTreeNode * &pRoot,int * &arr);
void change(BsTreeNode * node,BsTreeNode * &pTail);

struct BsTreenode
{
int m_value;
BsTreenode *m_left;
BsTreenode *m_right;
};
void main()
{

BsTreeNode *pRoot=NULL;
BsTreeNode*pTail = NULL;
int arr[] = { 10 ,6 ,4 ,0, 0, 8, 0, 0, 14, 12, 0, 0, 16, 0, 0 };
int *ptr = arr;
CreateBitree(pRoot, ptr);

change(pRoot,pTail);
while (NULL != pTail)
{
cout << pTail->m_nvalue<<" ";
pTail = pTail->m_pLeft;
}

cout << endl;

system("pause");

}
void CreateBitree(BsTreeNode * &pRoot,int * &arr)
{
int dat;
dat = *arr;
++arr;
if (dat == 0)
{
pRoot = NULL;
}
else
{

pRoot = new BsTreeNode;
pRoot->m_nvalue = dat;
CreateBitree(pRoot->m_pLeft, arr);
CreateBitree(pRoot->m_Right, arr);
}
}

void change(BsTreeNode * node,BsTreeNode * &pTail)
{
if (node)
{
change(node->m_pLeft, pTail);
if (pTail)
{
pTail->m_Right = node;
}
node->m_pLeft = pTail;
pTail = node;
change(node->m_Right, pTail);
}

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: