1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
| package BinaryTree;
import java.util.Iterator; import java.util.LinkedList; import java.util.Queue;
public class BiTree{
private int count; private Node root; public int index; class Node {
public Node lchild; public Node rchild; public int data; public Node(int data) { this.data = data; } public Node getLchild() { return lchild; } public void setLchild(Node lchild) { this.lchild = lchild; } public Node getRchild() { return rchild; } public void setRchild(Node rchild) { this.rchild = rchild; } public int getData() { return data; } public void setData(int data) { this.data = data; } } //二叉树的建立 public Node CreateBTree(int[] a){ Node root = null; if(a[index]!='#'){ root = new Node(a[index]); index++; root.setLchild(CreateBTree(a)); index++; root.setRchild(CreateBTree(a)); } return root; } //二叉树节点的删除 public boolean delete(int data) { Node current = root; Node parent = root; boolean isleftchild = false; while(current.data!=data) { parent = current; if(current.data>data) { isleftchild = true; current = current.lchild; }else{ isleftchild = false; current = current.rchild; } if(current == null) { return false; } } //被删除的节点没有子节点 if(current.lchild==null &¤t.rchild==null) { if(current==root) { root = null; }else if(isleftchild) { parent.lchild = null; }else { parent.rchild = null; } return true; } //被删除的节点有一个子节点 if(current.lchild==null&¤t.rchild!=null) { if(root == current) { root = current.rchild; }else if(isleftchild) { parent.lchild = current.rchild; }else { parent.rchild = current.rchild; } }else if(current.lchild!=null&¤t.rchild==null) { if(root == current) { root = current.lchild; }else if(isleftchild) { parent.lchild = current.lchild; }else { parent.rchild = current.lchild; } } //被删除的节点有两个子节点 if(current.lchild != null && current.rchild != null){ //获取删除节点的后继结点 Node successor = getSuccessor(current); if (root == current) { root = successor; } else if (isleftchild) { parent.lchild = successor; } else { parent.rchild = successor; } } return false; } public Node getSuccessor(Node delNode) { Node successorParent = delNode; Node successor = delNode; Node current = delNode.rchild; while (current != null) { successorParent = successor; successor = current; current = current.lchild; } if (successor != delNode.rchild) { successorParent.lchild = successor.rchild; successor.rchild = delNode.rchild; } return successor; } //先序遍历 public void prevOrder(Node root) { if(root == null) { return; } System.out.print(root.getData()+" "); prevOrder(root.getLchild()); prevOrder(root.getRchild()); } //中序遍历 public void inOrder(Node root) { if(root==null) { return; } inOrder(root.getLchild()); System.out.print(root.getData()+" "); inOrder(root.getRchild()); } //后序遍历 public void postOrder(Node root) { if(root==null) { return; } postOrder(root.getLchild()); postOrder(root.getRchild()); System.out.print(root.getData()+" "); } //层序遍历 public void BTreeLevelOrder(){ Node root = this.root; Queue <Node> queue = new LinkedList<Node>(); LinkedList<Node> list = new LinkedList<Node>(); queue.offer(root); while(!queue.isEmpty()){ Node pre = queue.poll(); list.add(pre); if(pre.getLchild()!=null) queue.offer(pre.getLchild()); if(pre.getRchild()!=null) queue.offer(pre.getRchild()); } Iterator<Node> it = list.iterator(); while(it.hasNext()){ Node cur = (Node)it.next(); System.out.print(cur.getData()+" "); } } //获得二叉树的高度 public int getHeight(Node root){ //递归获取 int leftHeight = 0; int rightHeight = 0; if(root==null){ return 0; }else{ leftHeight = getHeight(root.getLchild()); rightHeight = getHeight(root.getRchild()); } return leftHeight >= rightHeight ? ++leftHeight:++rightHeight; //最终高度应是左右子树高度中最大的一个 } //获得二叉树的叶子结点 public int getLeaf(Node root) { if(root == null) { return 0; }else if(root.getLchild()==null&&root.getRchild()==null) { System.out.println("叶子结点:"+root.getData()); return 1; } return getLeaf(root.getLchild())+getLeaf(root.getRchild()); } //获得二叉树某一层的节点 public int getNum(Node root, int deep) { if(deep == 1) { if(root==null) { return 0; }else { System.out.println("结点:"+root.getData()); return 1; } } return getNum(root.getLchild(), deep-1)+getNum(root.getRchild(), deep-1); } //查找某一值结点 public Node search(Node root, int key) { if(root==null) { return null; }else if(root.getData()==key) { return root; } Node left = search(root.getLchild(), key); if(left!=null) { return left; } Node right = search(root.getRchild(), key); if(right!=null) { return right; } return null; } //判断某一结点是否存在 public boolean isNull(Node root) { return root!=null; } //判断一棵树是否是完全二叉树 public boolean isCompleteBTree(){ Node root = this.root; Queue <Node> queue = new LinkedList<Node>(); queue.offer(root); while(!queue.isEmpty()){ Node pre = queue.poll(); if(pre==null) break; queue.offer(pre.getLchild()); queue.offer(pre.getRchild()); } while(!queue.isEmpty()){ Node cur = queue.poll(); if(cur!=null) return false; } return true; } public static void main(String[] args) { BiTree tree = new BiTree(); int[] a = new int[]{1,2,'#',3,'#',4,'#','#',5,6,'#','#','#' }; tree.root = tree.CreateBTree(a); System.out.println("先序遍历:"); tree.prevOrder(tree.root); System.out.println(); System.out.println("中序遍历:"); tree.inOrder(tree.root); System.out.println(); System.out.println("后序遍历:"); tree.postOrder(tree.root); System.out.println(); System.out.println("层序遍历:"); tree.BTreeLevelOrder(); System.out.println(); System.out.println("此二叉树的高度为:"+tree.getHeight(tree.root)); System.out.println("此二叉树的叶子结点数:"+tree.getLeaf(tree.root)); System.out.println("第二层的结点树为:"+tree.getNum(tree.root, 2)); Node order =tree.search(tree.root, 6); System.out.println("查找值为6的结点是否存在:"+tree.isNull(order)); Node order1 = tree.search(tree.root, 7); System.out.println("查找值为7的结点是否存在:"+tree.isNull(order1)); System.out.println("这是一棵完全二叉树吗:"+tree.isCompleteBTree()); } }
|