資料結構與演算法之二叉樹查詢

2021-10-16 12:01:54 字數 2188 閱讀 4880

二叉樹-查詢指定節點

要求1)請編寫前序查詢,中序查詢和後序查詢的方法。

2)並分別使用三種查詢方式,查詢 herono = 5 的節點

3)並分析各種查詢方式,分別比較了多少次

思路分析**

**實現

public

class

treedemo

}class

binarytree

//前序遞迴遍歷

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

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

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

}//遞迴前序查詢

public heronode preordersearch

(int no)

heronode res=null;if(

this

.left!=null)

if(res!=null)if(

this

.right!=null)

return res;

}//遞迴中序查詢

public heronode infixordersearch

(int no)

if(res!=null)if(

this

.no==no)if(

this

.right!=null)

return res;

}//遞迴後續查詢

public heronode postordersearch

(int no)

if(res!=null)if(

this

.right!=null)if(

this

.no==no)

return res;

}}

資料結構與演算法之二叉樹

樹同時具有鍊錶和陣列的優點,關於樹的術語有 根 樹頂端的節點 葉子節點 沒有子節點的節點 樹那個節點所對應的資料結構 節點物件類,包含資料 public class node 將資料插到樹中 public void inser int id,double dd public boolean dele...

資料結構與演算法之二叉樹

陣列的優缺點 鍊錶的優缺點 缺點 在進行查詢時,效率仍然較低,需要從頭節點開始遍歷,時間複雜度為o n 樹的優點 能提高資料儲存和讀取的效率,比如利用二叉搜尋樹,既可以保證資料的查詢速度,同時也可以保證資料的插入,刪除,修改的速度。樹的常用術語 結合示意圖理解 樹的基本性質 二叉樹 每個節點最多只能...

資料結構與演算法之二叉查詢樹

對一般容器的查詢,我們可以按順序遍歷,找到符合要求的元素就返回 對於元素是有序的容器,可以使用二分查詢等方法查詢,減少操作的時間複雜度.容易知道,一般查詢的平均時間複雜度是o n 二分查詢的平均時間複雜度是o logn 什麼是二叉查詢樹?根結點的左子樹的結點都小 大 於根結點,根結點的右子樹的結點都...