这是要插入的树
代码如下:
package com.stree.me;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
public class MyTree {
MyNode root;
public static void main(String[] args) {
MyTree tree = new MyTree();
tree.insert(new MyNode(40, "A"));
tree.insert(new MyNode(30, "B"));
tree.insert(new MyNode(50, "C"));
tree.insert(new MyNode(20, "D"));
tree.insert(new MyNode(31, "E"));
tree.insert(new MyNode(41, "F"));
tree.insert(new MyNode(60, "G"));
tree.insert(new MyNode(10, "H"));
tree.insert(new MyNode(21, "I"));
tree.insert(new MyNode(32, "J"));
tree.insert(new MyNode(42, "K"));
List<String> list = new ArrayList<>();
List<MyNode> myNode = new ArrayList<>();
myNode.add(tree.root);
// tree.getList2(list, myNode);
tree.getList2(list, myNode);
System.out.println(Arrays.toString(list.toArray()));
}
// 前序遍历 [A, B, D, H, I, E, J, C, F, K, G]
public void getList(List<String> list, MyNode node) {
if (root == null) {
return;
}
MyNode current = node;
if (current != null) {
list.add(node.value);
node = current.leftChild;
getList(list, node);
node = current.rightChild;
getList(list, node);
}
}
// 前序遍历 [A, B, D, H, I, E, J, C, F, K, G]
public void getList_2(List<String> list) {
if (root == null) {
return;
}
Stack<MyNode> list2 = new Stack<>();
list2.push(root);
while (!list2.isEmpty()) {
MyNode currentNode = list2.pop();
list.add(currentNode.value);
if (currentNode.rightChild != null) {
list2.push(currentNode.rightChild);
}
if (currentNode.leftChild != null) {
list2.push(currentNode.leftChild);
}
}
}
// 层次遍历[A, B, C, D, E, F, G, H, I, J, K]
public void getList2_2(List<String> list) {
if (root == null) {
return;
}
LinkedList<MyNode> list2 = new LinkedList<>();
list2.offer(root);
while (!list2.isEmpty()) {
MyNode currentNode = list2.poll();
list.add(currentNode.value);
if (currentNode.leftChild != null) {
list2.offer(currentNode.leftChild);
}
if (currentNode.rightChild != null) {
list2.offer(currentNode.rightChild);
}
}
}
// 层次遍历[A, B, C, D, E, F, G, H, I, J, K]
public void getList2(List<String> list, List<MyNode> nodes) {
if (root == null) {
return;
}
if (nodes != null && !nodes.isEmpty()) {
List<MyNode> nodesChild = new ArrayList<>();
for (MyNode node : nodes) {
if (node != null) {
list.add(node.value);
nodesChild.add(node.leftChild);
nodesChild.add(node.rightChild);
}
}
getList2(list, nodesChild);
}
}
// 中序遍历 结果 [H, D, I, B, E, J, A, F, K, C, G]
public void getList3_2(List<String> list) {
if (root == null) {
return;
}
Stack<MyNode> list2 = new Stack<>();
MyNode node = root;
while (node != null || !list2.isEmpty()) {
// 先插入所有的左节点
while (node != null) {
list2.push(node);
node = node.leftChild;
}
node = list2.pop();
list.add(node.value);
node = node.rightChild;
}
}
// 中序遍历 结果 [H, D, I, B, E, J, A, F, K, C, G]
public void getList3(List<String> list, MyNode nodes) {
if (nodes != null) {
getList3(list, nodes.leftChild);
list.add(nodes.value);
getList3(list, nodes.rightChild);
}
}
// 后续遍历 结果[H, I, D, J, E, B, K, F, G, C, A]
public void getList4(List<String> list, MyNode nodes) {
if (nodes != null) {
getList4(list, nodes.leftChild);
getList4(list, nodes.rightChild);
list.add(nodes.value);
}
}
// 后续遍历 结果[H, I, D, J, E, B, K, F, G, C, A]
public void getList4_2(List<String> list) {
if (root == null) {
return;
}
Stack<MyNode> list2 = new Stack<>();
MyNode node = root;
while (node != null || !list2.isEmpty()) {
// 先插入所有的左节点
while (node != null) {
list2.push(node);
node = node.leftChild;
}
node = list2.pop();
list.add(node.value);
if (!list2.isEmpty() && node == list2.peek().leftChild) {
node = list2.peek().rightChild;
} else {
node = null;
}
}
}
public void insert(MyNode node) {
if (root == null) {
root = node;
} else {
MyNode current = root;
MyNode parent = null;
while (true) {
parent = current;
if (current.key < node.key) {
current = current.rightChild;
if (current == null) {
parent.rightChild = node;
return;
}
} else {
current = current.leftChild;
if (current == null) {
parent.leftChild = node;
return;
}
}
}
}
}
}
class MyNode {
int key;
String value;
MyNode rightChild;
MyNode leftChild;
public MyNode(int key, String value) {
super();
this.key = key;
this.value = value;
}
}