線索二叉樹

2021-07-24 19:50:19 字數 831 閱讀 6184

線索二叉樹是為了便於訪問乙個結點的前驅和後繼的資訊。它在每個結點上新增了兩個標誌位,用來判斷當前結點到底有沒有左子樹/右子樹,如果沒有,就把其利用起來存前驅/後繼的資訊。同時它也新增了乙個頭節點用來將整個二叉樹聯絡起來。具體實現如下,有大量注釋便於理解。

#include using namespace std;

#include #include #include #include typedef struct node

node, *bintree;

bintree pre = null;

void create(bintree &root) //建立二叉樹(本來指標是不用使用"&"引用符的,但是當指標為null時,函式傳進來的也是指標的乙個拷貝,而不是位址,具體解釋看我之前的部落格c語言指標傳參問題)

}}void inthreading(bintree p) //構造中序線索二叉樹

if(!pre->rchild) //如果前乙個訪問的結點的右子樹為空,那用來存後繼

pre = p; //更新上乙個訪問的結點

inthreading(p->rchild); //線索化右子樹

}}int inorderthreading(bintree &head, bintree root)

return 1;

}int inorder(bintree head) //中序遍歷線索二叉樹

p = p->rchild; //來到p的右子樹,繼續以上過程

}return 1;

}int main()

線索二叉樹

當用二叉鍊錶作為二叉樹的儲存結構時,因為每個結點中只有指向其左 右兒子結點的指標,所以從任一結點出發只能直接找到該結點的左 右兒子。在一般情況下靠它無法直接找到該結點在某種遍歷序下的前驅和後繼結點。如果在每個結點中增加指向其前驅和後繼結點的指標,將降低儲存空間的效率。我們可以證明 在n個結點的二叉鍊...

線索二叉樹

1.線索二叉樹結構和操作定義 threadbintree.h 功能 線索標誌域所有值 typedef enumnodeflag 功能 線索二叉樹結構體 typedef struct threadtreethreadbintree 前驅節點指標 threadbintree previous null ...

線索二叉樹

原始碼 中序線索二叉樹 author 菜鳥 version 2014.7.23 include include include typedef char datatype using namespace std 定義線索二叉樹的結構體 typedef struct nodethreadbitreen...