[題目分析]在二叉樹上建立三叉鏈表若二叉樹已用二叉鏈表表示則可象題那樣給每個結點加上指向雙親的指針(根結點的雙親指針為空)至於刪除元素值為x的結點以及以x為根的子樹與題完全一樣請參照題下面給出建立用三叉鏈表表示的二叉樹的算法二叉樹按完全二叉樹格式輸入對非完全二叉樹要補上虛結點按完全二叉樹雙親和子女存儲下標間的關系完成雙親和子女間指針的鏈接#是輸入結束標志$是虛結點標志
BiTree creat()/ /生成三叉鏈表的二叉樹(題目給出PASCAL定義下面的用類C書寫)
{BiTree pQ[]root; //Q是二叉樹結點指針的一維數組容量足夠大
char ch; int rear=; //一維數組最後元素的下標
scanf(&ch);
while(ch!=#)
{p=null;
if(ch!=$){p=(BiTree)malloc(sizeof(nodetp));
p>data=ch; p>lchild=p>rchild=null; }
Q[++rear]=p; //元素或虛結點
if(p){if(rear==) {root=p;root>parent=null; } //根結點
else{Q[rear]>parent=Q[rear/]; /雙親結點和子女結點用指針鏈上
if (rear%==) Q[rear/]>lchild=Q[rear]; else Q[rear/]>rchild=Q[rear];
}
scanf(%c&ch);
}//while
return(root); }//結束creat
[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
From:http://tw.wingwit.com/Article/program/sjjg/201311/23710.html