26 實現線索化功能的二叉樹

2022-09-18 09:30:30 字數 3812 閱讀 8300

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;

}

private heronode root;

//為了實現線索化,需要建立要給指向當前結點的前驅結點的指標

//在遞迴進行線索化時,pre 總是保留前乙個結點

private heronode pre = null;

public void setroot(heronode root)
public void threadednodes()
public void threadedlist() 

//列印當前這個結點

system.out.println(node);

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

while(node.getrighttype() == 1)

//替換這個遍歷的結點

node = node.getright();

}}

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 

}

public static void main(string args)

(C )二叉樹的線索化 線索二叉樹

線索化標誌tag enum pointertag 結點結構 template struct binarytreenodethd 基類迭代器 template struct binarytreeiterator t operator t operator bool operator const sel...

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

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...