資料結構之樹的基本操作(java版本)
阿新 • • 發佈:2019-01-31
本部落格來自慕課網《資料結構探險之樹篇》,慕課網主講老師使用C++實現的,這裡我將其改為java實現,
以下是對程式碼的幾點說明:
二叉樹:所有節點的度都小於等於2
二叉樹的遍歷:根據訪問根的順序:前序、中序、後序。二叉樹陣列實現:
左孩子下標 = 父節點下標2 + 1;
右孩子下標 = 父節點下標2 + 2;
父節點下標 = (孩子節點下標 - 1) / 2;
測試二叉樹的資料如下:
一共是三個類,一個是節點Node類,一個是樹Tree類,還有一個是測試數的類,三個類的程式碼如下所示:
Node類:
package tree; public class Node { public int index; public int data; public Node LChild; public Node RChild; public Node PChild; public Node() { index=0; data=0; LChild=null; RChild=null; PChild=null; } public Node SearchNode(int nodeIndex) { if(this.index==nodeIndex) { return this; } Node temp=null; if(this.LChild!=null) { if(this.LChild.index==nodeIndex) { return this.LChild; }else{ temp=this.LChild.SearchNode(nodeIndex); if(temp!=null) return temp; } } if(this.RChild!=null) { if(this.RChild.index==nodeIndex) { return this.RChild; }else{ temp=this.RChild.SearchNode(nodeIndex); return temp; } } return null; } void DeleteNode() { if(this.LChild!=null) this.LChild.DeleteNode(); if(this.RChild!=null) this.RChild.DeleteNode(); if(this.PChild!=null) { if(this.PChild.LChild==this) { this.PChild.LChild=null; } if(this.PChild.RChild==this) { this.PChild.RChild=null; } } } /** * 前序遍歷 */ public void PreorderTraversal() { System.out.println("index="+index+" data="+data); if(this.LChild!=null) { this.LChild.PreorderTraversal(); } if(this.RChild!=null) { this.RChild.PreorderTraversal(); } } /** * 中序遍歷 */ public void InorderTraversal() { if(this.LChild!=null) { this.LChild.InorderTraversal(); } System.out.println("index="+index+" data="+data); if(this.RChild!=null) { this.RChild.InorderTraversal(); } } /** * 後序遍歷 */ public void PostorderTraversal() { if(this.LChild!=null) { this.LChild.PostorderTraversal(); } if(this.RChild!=null) { this.RChild.PostorderTraversal(); } System.out.println("index="+index+" data="+data); } }
Tree類:
package tree; public class Tree { public Node pRoot; public Tree() { pRoot=new Node(); } /** * 搜尋節點 * @param nodeIndex * @return */ public Node SearchNode(int nodeIndex) { return pRoot.SearchNode(nodeIndex); } /** * 新增節點 * @param nodeIndex * @param direction * @param node * @return */ public boolean AddNode(int nodeIndex,int direction,Node node) { Node tempNode=SearchNode(nodeIndex); if(tempNode==null) return false; Node tNode=new Node(); if(tNode==null) { //申請記憶體失敗 return false; } tNode.index=node.index; tNode.data=node.data; tNode.PChild=tempNode; if(direction==0)//插入到左節點 { tempNode.LChild=tNode; } if(direction==1)//插入到右節點 { tempNode.RChild=tNode; } return true; } boolean DeleteNode(int nodeIndex,Node node) { Node temp=SearchNode(nodeIndex); if(temp==null) return false; if(node!=null) { node.data=temp.data; } temp.DeleteNode(); return true; } public void DistroyTree() { //pRoot.DeleteNode(); DeleteNode(0, null); } /** * 前序遍歷 */ public void PreorderTraversal() { pRoot.PreorderTraversal(); } /** * 中序遍歷 */ public void InorderTraversal() { pRoot.InorderTraversal(); } /** * 後序遍歷 */ public void PostorderTraversal() { pRoot.PostorderTraversal(); } }
測試類:
package tree; public class TestTree { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub /* * 0(0) * * 5(1) 8(2) * * 2(3) 6(4) 9(5) 7(6) * * 前序:0526897-0134256 * 中序:2560982-3140526 * 後序:2650978-3415620 */ Tree tree=new Tree(); Node node1=new Node(); node1.index=1; node1.data=5; Node node2=new Node(); node2.index=2; node2.data=8; Node node3=new Node(); node3.index=3; node3.data=2; Node node4=new Node(); node4.index=4; node4.data=6; Node node5=new Node(); node5.index=5; node5.data=9; Node node6=new Node(); node6.index=6; node6.data=7; tree.AddNode(0, 0, node1); tree.AddNode(0, 1, node2); tree.AddNode(1, 0, node3); tree.AddNode(1, 1, node4); tree.AddNode(2, 0, node5); tree.AddNode(2, 1, node6); tree.DeleteNode(2, null); //tree.DeleteNode(5, null); //tree.PreorderTraversal(); //tree.InorderTraversal(); tree.PostorderTraversal(); } }