線索二叉樹

2022-03-13 07:22:25 字數 863 閱讀 4478

我們在對二叉樹進行遍歷的時候會產生乙個遍歷序列。而序列中的每個節點有前驅節點或後繼節點。線索二叉樹是在構建樹的時候為每個節點新增乙個前驅節點的指標和後繼節點的指標。這樣做是為了加快查詢節點前驅和後繼的速度。

二叉樹的線索化通過是對於某個節點,若無左子樹則令lchild指向其前驅節點,若無右子樹則令rchild指向其後繼節點。並且需要增加兩個標誌位來標誌其左右指標是指向孩子還是指向前驅和後繼。

下面使用中序遍歷對二叉樹進行線索化處理。

public

void

inclue(node p, node pre)

if(pre != null && pre.rchild == null

) pre =p;

inclue(p.rchild, pre);

}}

呼叫方式

public

static

void

main(string args) ;

bitree bitree = new

bitree(num);

system.out.println("線索化二叉樹");

bitree.inclue(bitree.root,

null

); }

查詢中序序列中的第乙個節點

//

獲取中序序列中的第乙個節點

public

node firstnode(node p)

查詢某個節點的後繼

//

求某個節點p的後繼節點

public

node nextnode(node p)

線索二叉樹

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