TreeNode&BinaryTree二叉树
1/3,定义节点对象
1 package demo022801; 2 3 public class TreeNode { 4 int num; 5 TreeNode leftNode; 6 TreeNode rightNode; 7 8 public TreeNode(int num){ 9 this.num=num; 10 } 11 12 public TreeNode getLeftNode() { 13 return leftNode; 14 } 15 16 public void setLeftNode(TreeNode leftNode) { 17 this.leftNode = leftNode; 18 } 19 20 public TreeNode getRightNode() { 21 return rightNode; 22 } 23 24 public void setRightNode(TreeNode rightNode) { 25 this.rightNode = rightNode; 26 } 27 //递归遍历,前序 28 public void frontShow(){ 29 System.out.println(num); 30 if (leftNode!=null){ 31 leftNode.frontShow(); 32 } 33 if(rightNode!=null){ 34 rightNode.frontShow(); 35 } 36 } 37 38 public void middleShow(){ 39 if (leftNode!=null){ 40 leftNode.frontShow(); 41 } 42 System.out.println(num); 43 if(rightNode!=null){ 44 rightNode.frontShow(); 45 } 46 } 47 48 public void afterShow(){ 49 if (leftNode!=null){ 50 leftNode.afterShow(); 51 } 52 if(rightNode!=null){ 53 rightNode.afterShow(); 54 } 55 System.out.println(num); 56 } 57 58 public TreeNode frontSearch(int num){ 59 TreeNode target = null; 60 //对比当前节点 61 if(this.num==num){ 62 return this; 63 //当前节点不是 64 }else { 65 //查找左节点 66 if (leftNode!=null){ 67 target = leftNode.frontSearch(num); 68 } 69 if(target!=null){ 70 return target; 71 } 72 //查找右节点 73 if (rightNode!=null){ 74 target = rightNode.frontSearch(num); 75 } 76 } 77 return target; 78 } 79 80 public void delete(int num){ 81 //定义一个temp 82 TreeNode parent = this; 83 //检查左儿子 84 if (parent.leftNode != null && parent.leftNode.num == num){ 85 parent.leftNode = null; 86 return; 87 } 88 //检查右儿子 89 if (parent.rightNode != null && parent.rightNode.num == num){ 90 parent.rightNode = null; 91 return; 92 } 93 94 //重新给temp赋值 95 parent = leftNode; 96 //检查左孙子,调用递归 97 if (parent != null){ 98 parent.delete(num); 99 } 100 101 //重新给temp赋值,调用递归 102 parent = rightNode; 103 //检查右孙子 104 if (parent != null){ 105 parent.delete(num); 106 } 107 } 108 }
2/2,定义二叉树对象
1 package demo022801; 2 3 public class BinaryTree { 4 TreeNode root; 5 6 public TreeNode getRoot() { 7 return root; 8 } 9 10 public void setRoot(TreeNode root) { 11 this.root = root; 12 } 13 14 //成员方法全部调用节点方法 15 public void frontShow(){ 16 root.frontShow(); 17 } 18 19 public void middleShow(){ 20 root.middleShow(); 21 } 22 23 public void afterShow(){ 24 root.afterShow(); 25 } 26 27 public TreeNode frontSearch(int num){ 28 return root.frontSearch(num); 29 } 30 31 public void delete(int num){ 32 if (root.num == num){ 33 root = null; 34 }else { 35 root.delete(num); 36 } 37 } 38 }
3/3调用demo
1 package demo022801; 2 3 public class BinaryTreeDemo { 4 public static void main(String[] args) { 5 //创建一个对象,一棵树 6 BinaryTree binTree = new BinaryTree(); 7 //创建一个根节点 8 TreeNode root = new TreeNode(1); 9 //给树对象赋值 10 binTree.setRoot(root); 11 //创建左右两个子节点 12 TreeNode rootL = new TreeNode(2); 13 TreeNode rootR = new TreeNode(3); 14 //将创建的子节点的引用传递给根节点 15 root.setLeftNode(rootL); 16 root.setRightNode(rootR); 17 //为第二层的左节点创建两个子节点 18 rootL.setLeftNode(new TreeNode(4)); 19 rootL.setRightNode(new TreeNode(5)); 20 //为第二层的右节点创建两个子节点 21 rootR.setLeftNode(new TreeNode(6)); 22 rootR.setRightNode(new TreeNode(7)); 23 //三种遍历方式 24 binTree.frontShow(); 25 binTree.middleShow(); 26 binTree.afterShow(); 27 //三种查找方式 28 TreeNode result = binTree.frontSearch(5); 29 30 31 //删除一个节点 32 binTree.delete(5); 33 34 } 35 }

浙公网安备 33010602011771号