水平遍歷二叉樹 程式碼中包含前序、中序、後序和水平遍歷四種
水平遍歷二叉樹要求一層一層從上往下從左往右遍歷,例如:
上面二叉樹的遍歷順序為:2、4、5、1、3
思路:利用佇列先進先出的性質
1、將根節點放入佇列
2、while迴圈佇列,只要佇列不為空,就取出第一個節點。獲取資料
3、將第二步取出的節點的左子節點和右子節點分別放入佇列
程式碼:
1、建立node節點
public class TreeNode { private double date;// 資料 private TreeNode lNode;// 左子樹 private TreeNode rNode;// 右子樹 public TreeNode() { } public TreeNode(double data) { this.date = data; } public double getDate() { return date; } public void setDate(double date) { this.date = date; } public TreeNode getlNode() { return lNode; } public void setlNode(TreeNode lNode) { this.lNode = lNode; } public TreeNode getrNode() { return rNode; } public void setrNode(TreeNode rNode) { this.rNode = rNode; } }
2、構造二叉樹,並遍歷,這裡包含了,前序,中序,後序已經水平遍歷
import java.util.LinkedList; import java.util.List;
public class BinTree { private TreeNode rootNode;
public void insertNode(double data) { TreeNode newNode = new TreeNode(data); if (rootNode == null) { rootNode = new TreeNode(); rootNode.setDate(data); } else { TreeNode currentNode = rootNode; TreeNode parentNode = null; while (true) { parentNode = currentNode; if (data > currentNode.getDate()) { currentNode = currentNode.getrNode(); if (currentNode == null) { parentNode.setrNode(newNode); return; } } else { currentNode = currentNode.getlNode(); if (currentNode == null) { parentNode.setlNode(newNode); return; } }
}
} }
/** * 前序遍歷 * * @param treeNode */ public void preErgodic(TreeNode treeNode) { System.out.println(treeNode.getDate()); if (treeNode.getlNode() != null) { preErgodic(treeNode.getlNode()); } if (treeNode.getrNode() != null) { preErgodic(treeNode.getrNode()); } }
/** * 中序遍歷 * * @param treeNode */ public void inErgodic(TreeNode treeNode) { if (treeNode.getlNode() != null) { inErgodic(treeNode.getlNode()); } System.out.println(treeNode.getDate()); if (treeNode.getrNode() != null) { inErgodic(treeNode.getrNode()); } }
/** * 後序遍歷 * * @param treeNode */ public void aftErgodis(TreeNode treeNode) { if (treeNode.getlNode() != null) { aftErgodis(treeNode.getlNode()); } if (treeNode.getrNode() != null) { aftErgodis(treeNode.getrNode()); } System.out.println(treeNode.getDate()); }
public TreeNode getRootNode() { return rootNode; }
public void setRootNode(TreeNode rootNode) { this.rootNode = rootNode; } /** * 水平遍歷 * @param treeNode */ public void horenzital(TreeNode treeNode) { LinkedList<TreeNode> list = new LinkedList<TreeNode>(); list.add(treeNode); while (list.size() > 0) { TreeNode currentNode = list.removeFirst(); if (currentNode != null) { System.out.println(currentNode.getDate()); } if (currentNode.getlNode() != null) { list.addLast(currentNode.getlNode()); } if (currentNode.getrNode() != null) { list.addLast(currentNode.getrNode()); } } }
}
測試程式碼:
public static void main(String[] args) { BinTree binTree = new BinTree(); binTree.insertNode(23); binTree.insertNode(1); binTree.insertNode(29); binTree.insertNode(34); binTree.insertNode(22); binTree.insertNode(56); binTree.insertNode(15); binTree.insertNode(18); binTree.insertNode(46); binTree.insertNode(78); binTree.insertNode(26); binTree.horenzital(binTree.getRootNode()); }
結果: