資料結構之線索二叉樹

2021-09-24 14:41:52 字數 4267 閱讀 1359

應用案例說明:將下面的二叉樹,進行中序線索二叉樹。中序遍歷的數列為

}//定義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 int getrighttype()

public void setrighttype(int righttype)

public heronode(int no, string name)

public int getno()

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;}}

資料結構之 線索二叉樹

2 線索二叉樹 實現 對於一顆二叉樹而言 如下圖所示 每次我們想要查詢二叉樹裡面的乙個節點的時候,我們都必須遍歷這棵樹,以中序遍歷為例,遍歷的順序為4251637,當我們遍歷到標號為5這個節點的時候,如果有個指標指向5的前乙個節點2或者後乙個節點1的話,那麼我們跳轉到2或者1的效率肯定會大大的提公升...

線索二叉樹 資料結構

按照教材進行中序二叉樹線索化 線索化就是就將二叉樹的多出來的n 1個鏈域用做指向前驅和後繼用,前驅後繼指的是按中序遍歷二叉樹產生的前驅和後繼 ltag 0 有左孩子 ltag 1 無左孩子,指向前驅 rtag 0 有右孩子 rtag 1 無右孩子,指向後繼 例子 構造二叉樹 形如abc de fg ...

資料結構(線索二叉樹)

線索二叉樹的前序,中序,後序 typedef struct nodenode 前序線索二叉樹 參照中序即可 void prethread node p,node pre if pre null pre rchild null pre p if p ltag 0 prethread p lchild,...