二叉树学习总结(Java实现)
最近学习二叉树,做一些总结,内容包括:
1.创建二叉树
2.递归方法:先序 中序 后序
3.非递归:先序 中序 后序
4.宽度优先搜索
5.寻找父节点
如果有错误,还请及时指出,谢谢!
1 import java.util.LinkedList; 2 import java.util.Scanner; 3 import java.util.Stack; 4 5 public class BinaryTreeStudy { 6 7 public static void main(String[] args) { 8 BinTree tree = new BinTree(); 9 tree.create(); 10 11 tree.preOrderWithRecusion(); 12 tree.inOrderWithRecusion(); 13 tree.postOrderWithRecusion(); 14 15 tree.preOrderWithoutRecusion(); 16 tree.inOrderWithoutRecusion(); 17 tree.postOrderWithoutRecusion(); 18 19 tree.levelOrader(); 20 21 tree.searchParent(5); 22 23 } 24 } 25 26 class Node { 27 int data; 28 Node leftChild; 29 Node rightChild; 30 } 31 32 class Item { 33 Node node; 34 boolean tag = false; 35 } 36 37 class BinTree { 38 Node root; 39 Scanner scanner = new Scanner(System.in); 40 41 public BinTree() { 42 root = null; 43 } 44 45 public void create() { 46 System.out.println("请输入根节点,-1表示节点为空"); 47 root = createTree(); 48 } 49 50 public Node createTree() { 51 Node subRoot; 52 int data = scanner.nextInt(); 53 if (data == -1) { 54 subRoot = null; 55 } else { 56 subRoot = new Node(); 57 subRoot.data = data; 58 System.out.println("请输入" + data + "的左子树"); 59 subRoot.leftChild = createTree(); 60 System.out.println("请输入" + data + "的右子树"); 61 subRoot.rightChild = createTree(); 62 } 63 64 return subRoot; 65 } 66 67 public void preOrderWithRecusion() { 68 System.out.println("递归:先序输出二叉树"); 69 preOrderWithRecusion(root); 70 System.out.println(); 71 } 72 73 // 递归:先序 74 private void preOrderWithRecusion(Node root) { 75 if (root != null) { 76 System.out.print(root.data); 77 preOrderWithRecusion(root.leftChild); 78 preOrderWithRecusion(root.rightChild); 79 } 80 } 81 82 public void inOrderWithRecusion() { 83 System.out.println("递归:中序输出二叉树"); 84 inOrderWithRecusion(root); 85 System.out.println(); 86 } 87 88 // 递归:中序 89 private void inOrderWithRecusion(Node root) { 90 if (root != null) { 91 inOrderWithRecusion(root.leftChild); 92 System.out.print(root.data); 93 inOrderWithRecusion(root.rightChild); 94 } 95 } 96 97 public void postOrderWithRecusion() { 98 System.out.println("递归:后序输出二叉树"); 99 postOrderWithRecusion(root); 100 System.out.println(); 101 102 } 103 104 // 递归:后序 105 private void postOrderWithRecusion(Node root) { 106 if (root != null) { 107 postOrderWithRecusion(root.leftChild); 108 postOrderWithRecusion(root.rightChild); 109 System.out.print(root.data); 110 } 111 } 112 113 public void preOrderWithoutRecusion() { 114 System.out.println("非递归:先序输出二叉树"); 115 preOrderWithoutRecusion(root); 116 System.out.println(); 117 } 118 119 // 非递归:先序 120 private void preOrderWithoutRecusion(Node subRoot) { 121 Stack<Node> stack = new Stack<Node>(); 122 Node pointer = subRoot; 123 124 while (pointer != null) { 125 System.out.print(pointer.data); 126 127 if (pointer.rightChild != null) { 128 stack.push(pointer.rightChild); 129 } 130 131 if (pointer.leftChild != null) { 132 pointer = pointer.leftChild; 133 } else { 134 if (!stack.isEmpty()) { 135 pointer = stack.pop(); 136 } else { 137 pointer = null; 138 } 139 } 140 141 } 142 143 } 144 145 public void inOrderWithoutRecusion() { 146 System.out.println("非递归:中序输出二叉树"); 147 inOrderWithoutRecusion(root); 148 System.out.println(); 149 } 150 151 // 非递归:中序 152 private void inOrderWithoutRecusion(Node subRoot) { 153 Stack<Node> stack = new Stack<Node>(); 154 Node pointer = subRoot; 155 156 while (pointer != null || !stack.isEmpty()) { 157 if (pointer != null) { 158 stack.push(pointer); 159 pointer = pointer.leftChild; 160 } else { 161 pointer = stack.pop(); 162 System.out.print(pointer.data); 163 pointer = pointer.rightChild; 164 } 165 } 166 } 167 168 public void postOrderWithoutRecusion() { 169 System.out.println("非递归:后序输出二叉树"); 170 postOrderWithoutRecusion(root); 171 System.out.println(); 172 } 173 174 // 非递归:后序 175 private void postOrderWithoutRecusion(Node subRoot) { 176 boolean LEFT = false; 177 boolean RIGHT = true; 178 Stack<Item> stack = new Stack<Item>(); 179 Node pointer = subRoot; 180 while (pointer != null || !stack.isEmpty()) { 181 Item item = null; 182 while (pointer != null) { 183 item = new Item(); 184 item.node = pointer; 185 item.tag = LEFT; 186 stack.push(item); 187 pointer = pointer.leftChild; 188 } 189 190 item = stack.pop(); 191 pointer = item.node; 192 193 if (item.tag == LEFT) { 194 item.tag = RIGHT; 195 stack.push(item); 196 pointer = pointer.rightChild; 197 } else { 198 System.out.print(pointer.data); 199 pointer = null; 200 } 201 202 } 203 } 204 205 public void levelOrader() { 206 System.out.println("宽度优先:"); 207 levelOrader(root); 208 System.out.println(); 209 } 210 211 // 宽度优先搜索二叉树 212 private void levelOrader(Node subRoot) { 213 LinkedList<Node> nodes = new LinkedList<Node>(); 214 215 Node pointer = subRoot; 216 if (pointer != null) { 217 nodes.offer(pointer); 218 } 219 220 while (!nodes.isEmpty()) { 221 pointer = nodes.poll(); 222 System.out.print(pointer.data); 223 224 if (pointer.leftChild != null) { 225 nodes.offer(pointer.leftChild); 226 } 227 228 if (pointer.rightChild != null) { 229 nodes.offer(pointer.rightChild); 230 } 231 232 } 233 234 } 235 236 public void searchParent(int data) { 237 System.out.println("搜索" + data + "的父节点:"); 238 Node parent = searchParent(root, data); 239 System.out.println(parent.data); 240 } 241 242 // 查找父节点 243 private Node searchParent(Node subRoot, int data) { 244 if (subRoot == null) { 245 return null; 246 } 247 248 if (subRoot.leftChild != null && subRoot.leftChild.data == data) { 249 return subRoot; 250 } 251 252 if (subRoot.rightChild != null && subRoot.rightChild.data == data) { 253 return subRoot; 254 } 255 256 Node pointer = null; 257 if ((pointer = searchParent(subRoot.leftChild, data)) != null) { 258 return pointer; 259 } 260 261 if ((pointer = searchParent(subRoot.rightChild, data)) != null) { 262 return pointer; 263 } 264 265 return null; 266 } 267 268 }
非递归搜索父节点:
1 public void searchParentWithoutRecusion(int data) { 2 System.out.println("非递归搜索" + data + "的父节点:"); 3 Node parent = searchParentWithoutRecusion(root, data); 4 System.out.println(parent.data); 5 } 6 7 private Node searchParentWithoutRecusion(Node subRoot, int data) { 8 Node pointer = subRoot; 9 Stack<Node> nodes = new Stack<Node>(); 10 nodes.push(null); 11 while (pointer != null) { 12 if (pointer.leftChild != null && pointer.leftChild.data == data) { 13 return pointer; 14 } 15 16 if (pointer.rightChild != null && pointer.rightChild.data == data) { 17 return pointer; 18 } 19 20 if (pointer.rightChild != null) { 21 nodes.push(pointer.rightChild); 22 } 23 24 if (pointer.leftChild != null) { 25 pointer = pointer.leftChild; 26 } else { 27 pointer = nodes.pop(); 28 } 29 } 30 31 return null; 32 }