線索化二叉樹

2021-10-21 23:37:57 字數 4382 閱讀 3538

基本介紹

public

class

threadedbinarytreedemo

}//定義threadedbinarytree 實現了線索化功能的二叉樹

class

threadedbinarytree

//過載一把threadednodes方法

public

void

threadednodes()

//遍歷線索化二叉樹的方法

public

void

threadedlist()

//列印當前這個結點

system.out.

println

(node)

;//如果當前結點的右指標指向的是後繼結點,就一直輸出

while

(node.

getrighttype()

==1)//替換這個遍歷的結點

node = node.

getright()

;}}//編寫對二叉樹進行中序線索化的方法

/** *

* @param node 就是當前需要線索化的結點

*/public

void

threadednodes

(heronode node)

//(一)先線索化左子樹

threadednodes

(node.

getleft()

);//(二)線索化當前結點[有難度]

//處理當前結點的前驅結點

//以8結點來理解

//8結點的.left = null , 8結點的.lefttype = 1

if(node.

getleft()

== null)

//處理後繼結點

if(pre != null && pre.

getright()

== null)

//!!! 每處理乙個結點後,讓當前結點是下乙個結點的前驅結點

pre = node;

threadednodes

(node.

getright()

);}//刪除結點

public

void

delnode

(int no)

else

}else

}//前序遍歷

public

void

preorder()

else

}//中序遍歷

public

void

infixorder()

else

}//後序遍歷

public

void

postorder()

else

}//前序遍歷

public heronode preordersearch

(int no)

else

}//中序遍歷

public heronode infixordersearch

(int no)

else

}//後序遍歷

public heronode postordersearch

(int no)

else}}

//先建立heronode 結點

class

heronode

public

void

setlefttype

(int lefttype)

public

intgetrighttype()

public

void

setrighttype

(int righttype)

public

heronode

(int no, string name)

public

intgetno()

public

void

setno

(int no)

public string getname()

public

void

setname

(string name)

public heronode getleft()

public

void

setleft

(heronode left)

public heronode getright()

public

void

setright

(heronode right)

@override

public string tostring()

//遞迴刪除結點

//1.如果刪除的節點是葉子節點,則刪除該節點

//2.如果刪除的節點是非葉子節點,則刪除該子樹

public

void

delnode

(int no)

//3.如果當前結點的右子結點不為空,並且右子結點 就是要刪除結點,就將this.right= null ;並且就返回(結束遞迴刪除)if(

this

.right != null &&

this

.right.no == no)

//4.我們就需要向左子樹進行遞迴刪除if(

this

.left != null)

//5.則應當向右子樹進行遞迴刪除if(

this

.right != null)

}//編寫前序遍歷的方法

public

void

preorder()

//遞迴向右子樹前序遍歷if(

this

.right != null)

}//中序遍歷

public

void

infixorder()

//輸出父結點

system.out.

println

(this);

//遞迴向右子樹中序遍歷if(

this

.right != null)

}//後序遍歷

public

void

postorder()

if(this

.right != null)

system.out.

println

(this);

}//前序遍歷查詢

/** *

* @param no 查詢no

* @return 如果找到就返回該node ,如果沒有找到返回 null

*/public heronode preordersearch

(int no)

//1.則判斷當前結點的左子節點是否為空,如果不為空,則遞迴前序查詢

//2.如果左遞迴前序查詢,找到結點,則返回

heronode resnode = null;if(

this

.left != null)

if(resnode != null)

//1.左遞迴前序查詢,找到結點,則返回,否繼續判斷,

//2.當前的結點的右子節點是否為空,如果不空,則繼續向右遞迴前序查詢if(

this

.right != null)

return resnode;

}//中序遍歷查詢

public heronode infixordersearch

(int no)

if(resnode != null)

system.out.

println

("進入中序查詢");

//如果找到,則返回,如果沒有找到,就和當前結點比較,如果是則返回當前結點if(

this

.no == no)

//否則繼續進行右遞迴的中序查詢if(

this

.right != null)

return resnode;

}//後序遍歷查詢

public heronode postordersearch

(int no)

if(resnode != null)

//如果左子樹沒有找到,則向右子樹遞迴進行後序遍歷查詢if(

this

.right != null)

if(resnode != null)

system.out.

println

("進入後序查詢");

//如果左右子樹都沒有找到,就比較當前結點是不是if(

this

.no == no)

return resnode;

}}

線索化二叉樹以及遍歷線索化二叉樹

1.線索二叉樹基本介紹 n個結點的二叉鍊錶中含有n 1 公式 2n n 1 n 1 個空指標域。利用二叉鍊錶中的空指標域,存放指向該結點在某種遍歷次序下的前驅和後繼結點的指標 這種附加的指標稱為 線索 這種加上了線索的二叉鍊錶稱為線索鍊錶,相應的二叉樹稱為線索二叉樹 threaded binaryt...

線索化二叉樹

define crt secure no warnings 1 includeusing namespace std enum pointertag 列舉 其結構如下 void prevorderthreading 前序 void postorderthreading 後序 void inorder...

線索化二叉樹

二叉樹是一種非線性結構,遍歷二叉樹幾乎都是通過遞迴或者用棧輔助實現非遞迴的遍歷。用二叉樹作為儲存結構時,取到乙個節點,只能獲取節點的左孩子和右孩子,不能直接得到節點的任一遍歷序列的前驅或者後繼。為了儲存這種在遍歷中需要的資訊,我們利用二叉樹中指向左右子樹的空指標來存放節點的前驅和後繼資訊.二叉樹的結...