用java實現二叉樹遍歷

2021-06-02 09:37:07 字數 749 閱讀 6854

public class binarytree 

class treenode

public string getdata()

public void setdata(string data)

public treenode getleftnode()

public void setleftnode(treenode leftnode)

public treenode getrightnode()

public void setrightnode(treenode rightnode)

} public static void main(string args)

public treenode init()

public void xianiterator(treenode node)

if (node.getrightnode() != null)

} public void zhongiterator(treenode node)

this.printnode(node);

if (node.getrightnode() != null)

} public void houiterator(treenode node)

if (node.getrightnode() != null)

this.printnode(node);

}}

JAVA 二叉樹遍歷

二叉樹的定義如下 public class treenode 遞迴的版本很簡單,下面僅列出非遞迴的版本。先序遍歷 public void searchpreorder treenode root while root null if root.left null else root s.pop 中序...

java 實現二叉樹的遍歷

1 定義乙個節點類 public class node public node int data public node int data,node left,node right public intgetdata public void setdata int data public node ...

二叉樹層次遍歷(用佇列實現)

核心思想 首先根節點入隊,若佇列非空則做迴圈,若根節點有左右孩子,則左右孩子入隊,第乙個節點出隊,迴圈直到隊列為空。ifndef btree h define btree h typedef char btdatatype typedef struct binarytreenodebtnode vo...