二叉树的遍历 java

结点类:

public class BinaryTreeNode {
	private int data;
	private BinaryTreeNode left;
	private BinaryTreeNode right;

	public BinaryTreeNode() {
	}
}

二叉树的插入:

// 插入节点
public void insert(int key, int value) {
	// 没有根结点,直接新建
	if (root == null) {
		root = new BinaryNode(key, value);
	}
	BinaryNode currentNode = root;
	BinaryNode parentNode = root;
	boolean isLeftChild = true;
	while (currentNode != null) { // 找到右子树或左子树为空 的结点 ,准备插入
		parentNode = currentNode;// 每次都保存下当前的父结点
		if (key < currentNode.key) { // 比父结点小
			currentNode = currentNode.left; // 右结点设为当前结点
			isLeftChild = true;// 确定是右子树
		} else {// 比父结点大
			currentNode = currentNode.right; // 左子树设为当前结点
			isLeftChild = false;
		}
	}
	BinaryNode newNode = new BinaryNode(key, value); // 找到位置后插入。
	if (isLeftChild) {
		parentNode.left = newNode;
	} else {
		parentNode.right = newNode;
	}
}

 

二叉树的遍历:

public class BinaryTree {

	// 前序遍历递归的方式
	public void preOrder(BinaryTreeNode root) {
		if (root != null) {
			System.out.println(root.getData() + " ");
			preOrder(root.getLeft());
			preOrder(root.getRight());
		}
	}

	// 前序遍历非递归的方式1
	public void preOrderNoRecursive(BinaryTreeNode root) {
		Stack<BinaryTreeNode> stack = new Stack<>();
		stack.push(root);// 进栈
		while (!stack.isEmpty()) {
			BinaryTreeNode newtree = stack.pop();
			System.out.println(newtree.getData() + " ");
			if (newtree.getRight() != null) {
				stack.push(newtree.getRight());// 进栈
			}
			if (newtree.getLeft() != null) {
				stack.push(newtree.getLeft());
			}
		}
	}

	// 前序遍历非递归的方式2
	public void preOrderNoRecursive2(BinaryTreeNode root) {
		Stack<BinaryTreeNode> stack = new Stack<>();
		while (root != null || !stack.isEmpty()) { // 左孩子压栈
			if (root != null) {
				stack.push(root);
				System.out.println(root.getData() + " ");
				root = root.getLeft();
			} else {
				root = stack.pop();
				root = root.getRight();
			}
		}
	}

	// 中序遍历递归的方式
	public void inOrder(BinaryTreeNode root) {
		if (root != null) {
			inOrder(root.getLeft());
			System.out.println(root.getData() + " ");
			inOrder(root.getRight());
		}
	}

	// 中序遍历非递归
	public void inOrderNoRecursive(BinaryTreeNode root) {
		Stack<BinaryTreeNode> stack = new Stack<>();
		while (!stack.isEmpty() || root != null) {
			if (root != null) {
				stack.push(root);
				root = root.getLeft(); // 先把左子树全部压入栈中
			} else {
				root = stack.pop(); // 出栈并访问
				System.out.println(root.getData() + " ");
				root = root.getRight();
			}
		}
	}

	// 后序遍历递归的方式
	public void portOrder(BinaryTreeNode root) {
		if (root != null) {
			portOrder(root.getLeft());
			portOrder(root.getRight());
			System.out.println(root.getData());
		}
	}

	// 后序遍历非递归--双栈法
	public void portOrderNoRecursive(BinaryTreeNode root) {
		Stack<BinaryTreeNode> stack = new Stack<>();
		Stack<BinaryTreeNode> output = new Stack<>();// 构造一个中间栈来存储逆后序遍历的结果
		while (root != null || !stack.isEmpty()) {
			if (root != null) {
				output.push(root);
				stack.push(root);
				root = root.getRight();
			} else {
				root = stack.pop();
				root = root.getLeft();
			}
		}
		System.out.println(output.size());
		while(output.size()>0) {
			System.out.println(output.pop()+" ");
		}
	}

	// 层序遍历的方式
	public void levelOrder(BinaryTreeNode root) {
		BinaryTreeNode temp;
		// 先进先出
		Queue<BinaryTreeNode> queue = new LinkedList<BinaryTreeNode>();

		queue.offer(root);// 入队
		while (!queue.isEmpty()) {
			temp = queue.poll();// 队头出队
			System.out.println(temp.getData() + " ");
			if (temp.getLeft() != null) {
				queue.offer(temp.getLeft());// 左入队
			}
			if (temp.getRight() != null) {
				queue.offer(temp.getRight());// 右入队
			}
		}
	}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值