資料結構 樹 二叉樹 前 中 後序查詢

2021-10-04 21:19:56 字數 2079 閱讀 1342

("中序遍歷查詢");

resnode = binarytree.

infixordersearch(5

);if(resnode!=null)

else

system.out.

println

("後序遍歷查詢");

resnode = binarytree.

postordersearch(5

);if(resnode!=null)

else}}

//定義二叉樹

class

binarytree

//前序遍歷查詢

public heronode preordersearch

(int no)

else

}//中序遍歷查詢

public heronode infixordersearch

(int no)

else

}//後序遍歷查詢

public heronode postordersearch

(int no)

else}}

//建立heronode節點

class

heronode

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()

';}/**

* 前序遍歷查詢

*/public heronode preordersearch

(int no)

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

heronode resnode = null;if(

this

.left!=null)

if(resnode !=null)if(

this

.right!=null)

return resnode;

}/**

* 中序遍歷查詢

*/public heronode infixordersearch

(int no)

if(resnode!=null)if(

this

.no == no)if(

this

.right!=null)

return resnode;

}/**

* 後續遍歷查詢

資料結構 二叉樹 前中後序遍歷 查詢 刪除

class treenode override public string tostring 前序遍歷 public void preorder if this right null 中序遍歷 public void infixorder system.out.print this val if t...

二叉樹前中後序演算法

includetypedef struct nodebitnode,bitree void createbitree bitree bitree void preorder bitree root inorder bitree root 中序遍歷二叉樹,root為指向二叉樹 或某一子樹 根結點的指標...

二叉樹前中後序整理

今天刷劍指offer,遇到一題關於二叉樹前中後序的,所以就想整理一下二叉樹前中後序的概念,內容是從別處複製的,不是原創,方便以後自己遺忘了複習用。那麼對上面這個樹進行前中後序遍歷,結果如下 前序遍歷 ab cde 中序遍歷 a b c d e 這裡符號有優先順序就在此處加上了括號 後序遍歷 ab c...