二叉树的遍历

这是要插入的树

代码如下:

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;

	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值