[算法] 六、 二叉树

二叉树

  1. 二叉树的先序、中序、后续遍历,包括递归和非递归方式

    import java.util.Stack;
    /**
     * 
     * 实现二叉树的先序、中序、后序遍历,包括递归方式和非递归方式
     *
     */
    public class Code_01_PreInPosTraversal {
    
    	public static class Node {
    		public int value;
    		public Node left;
    		public Node right;
    
    		public Node(int data) {
    			this.value = data;
    		}
    	}
    	//递归方式先序
    	public static void preOrderRecur(Node head) {
    		if (head == null) {
    			return;
    		}
    		System.out.print(head.value + " ");
    		preOrderRecur(head.left);
    		preOrderRecur(head.right);
    	}
    	//递归方式中序
    	public static void inOrderRecur(Node head) {
    		if (head == null) {
    			return;
    		}
    		inOrderRecur(head.left);
    		System.out.print(head.value + " ");
    		inOrderRecur(head.right);
    	}
    	//递归方式后序
    	public static void posOrderRecur(Node head) {
    		if (head == null) {
    			return;
    		}
    		posOrderRecur(head.left);
    		posOrderRecur(head.right);
    		System.out.print(head.value + " ");
    	}
    	//非递归方式先序遍历
    	public static void preOrderUnRecur(Node head) {
    		System.out.print("pre-order: ");
    		if (head != null) {
    			Stack<Node> stack = new Stack<Node>();
    			stack.add(head);
    			while (!stack.isEmpty()) {
    				head = stack.pop();
    				System.out.print(head.value + " ");
    				if (head.right != null) {
    					stack.push(head.right);
    				}
    				if (head.left != null) {
    					stack.push(head.left);
    				}
    			}
    		}
    		System.out.println();
    	}
    	//非递归方式中序遍历
    	public static void inOrderUnRecur(Node head) {
    		System.out.print("in-order: ");
    		if (head != null) {
    			Stack<Node> stack = new Stack<Node>();
    			while (!stack.isEmpty() || head != null) {
    				if (head != null) {
    					stack.push(head);
    					head = head.left;
    				} else {
    					head = stack.pop();
    					System.out.print(head.value + " ");
    					head = head.right;
    				}
    			}
    		}
    		System.out.println();
    	}
    	//非递归方式后序遍历(双栈实现)
    	public static void posOrderUnRecur1(Node head) {
    		System.out.print("pos-order: ");
    		if (head != null) {
    			Stack<Node> s1 = new Stack<Node>();
    			Stack<Node> s2 = new Stack<Node>();
    			s1.push(head);
    			while (!s1.isEmpty()) {
    				head = s1.pop();
    				s2.push(head);
    				if (head.left != null) {
    					s1.push(head.left);
    				}
    				if (head.right != null) {
    					s1.push(head.right);
    				}
    			}
    			while (!s2.isEmpty()) {
    				System.out.print(s2.pop().value + " ");
    			}
    		}
    		System.out.println();
    	}
    	//炫技版后序遍历非递归
    	public static void posOrderUnRecur2(Node h) {
    		System.out.print("pos-order: ");
    		if (h != null) {
    			Stack<Node> stack = new Stack<Node>();
    			stack.push(h);
    			Node c = null;
    			while (!stack.isEmpty()) {
    				c = stack.peek();
    				if (c.left != null && h != c.left && h != c.right) {
    					stack.push(c.left);
    				} else if (c.right != null && h != c.right) {
    					stack.push(c.right);
    				} else {
    					System.out.print(stack.pop().value + " ");
    					h = c;
    				}
    			}
    		}
    		System.out.println();
    	}
    
    	public static void main(String[] args) {
    		Node head = new Node(5);
    		head.left = new Node(3);
    		head.right = new Node(8);
    		head.left.left = new Node(2);
    		head.left.right = new Node(4);
    		head.left.left.left = new Node(1);
    		head.right.left = new Node(7);
    		head.right.left.left = new Node(6);
    		head.right.right = new Node(10);
    		head.right.right.left = new Node(9);
    		head.right.right.right = new Node(11);
    
    		// recursive
    		System.out.println("==============recursive==============");
    		System.out.print("pre-order: ");
    		preOrderRecur(head);
    		System.out.println();
    		System.out.print("in-order: ");
    		inOrderRecur(head);
    		System.out.println();
    		System.out.print("pos-order: ");
    		posOrderRecur(head);
    		System.out.println();
    
    		// unrecursive
    		System.out.println("============unrecursive=============");
    		preOrderUnRecur(head);
    		inOrderUnRecur(head);
    		posOrderUnRecur1(head);
    		posOrderUnRecur2(head);
    
    	}
    
    }
    
    

    总结:

    • 递归方式相对容易理解。与此同时,递归方式是函数栈,可以通过手动压栈出栈(也就是非递归方式)来实现。
    • 递归方式对每个节点其实遍历了3次,第一次遍历到就输出是先序遍历;第二次遍历到输出是中序遍历;第三次遍历到输出是后序遍历。
    • 非递归方式中后序遍历的顺序是“左右中”,可以通过“中右左”(和非递归先序遍历的算法类似),再利用第二个栈实现逆序。

    非递归方式先序遍历、中序遍历、后序遍历需要掌握。

  2. 二叉树的后继节点

    /**
     * 
     * 在二叉树中找到一个节点的后继节点
     *  该结构比普通二叉树节点结构多了一个指向父节点的parent指针。假
    	设有一 棵Node类型的节点组成的二叉树,树中每个节点的parent指针
    	都正确地指向 自己的父节点,头节点的parent指向null。只给一个在
    	二叉树中的某个节点 node,请实现返回node的后继节点的函数。在二
    	叉树的中序遍历的序列中, node的下一个节点叫作node的后继节点。
     */
    public class Code_03_SuccessorNode {
    
    	public static class Node {
    		public int value;
    		public Node left;
    		public Node right;
    		public Node parent;
    
    		public Node(int data) {
    			this.value = data;
    		}
    	}
    
    	public static Node getSuccessorNode(Node node) {
    		if (node == null) {
    			return node;
    		}
    		if (node.right != null) {
    			return getLeftMost(node.right);
    		} else {//右节点非空
    			Node parent = node.parent;
    			while (parent != null && parent.left != node) {
    				node = parent;
    				parent = node.parent;
    			}
    			return parent;
    		}
    	}
    	//右节点为空则找最左节点
    	public static Node getLeftMost(Node node) {
    		if (node == null) {
    			return node;
    		}
    		while (node.left != null) {
    			node = node.left;
    		}
    		return node;
    	}
    
    	public static void main(String[] args) {
    		Node head = new Node(6);
    		head.parent = null;
    		head.left = new Node(3);
    		head.left.parent = head;
    		head.left.left = new Node(1);
    		head.left.left.parent = head.left;
    		head.left.left.right = new Node(2);
    		head.left.left.right.parent = head.left.left;
    		head.left.right = new Node(4);
    		head.left.right.parent = head.left;
    		head.left.right.right = new Node(5);
    		head.left.right.right.parent = head.left.right;
    		head.right = new Node(9);
    		head.right.parent = head;
    		head.right.left = new Node(8);
    		head.right.left.parent = head.right;
    		head.right.left.left = new Node(7);
    		head.right.left.left.parent = head.right.left;
    		head.right.right = new Node(10);
    		head.right.right.parent = head.right;
    
    		Node test = head.left.left;
    		System.out.println(test.value + " next: " + getSuccessorNode(test).value);
    		test = head.left.left.right;
    		System.out.println(test.value + " next: " + getSuccessorNode(test).value);
    		test = head.left;
    		System.out.println(test.value + " next: " + getSuccessorNode(test).value);
    		test = head.left.right;
    		System.out.println(test.value + " next: " + getSuccessorNode(test).value);
    		test = head.left.right.right;
    		System.out.println(test.value + " next: " + getSuccessorNode(test).value);
    		test = head;
    		System.out.println(test.value + " next: " + getSuccessorNode(test).value);
    		test = head.right.left.left;
    		System.out.println(test.value + " next: " + getSuccessorNode(test).value);
    		test = head.right.left;
    		System.out.println(test.value + " next: " + getSuccessorNode(test).value);
    		test = head.right;
    		System.out.println(test.value + " next: " + getSuccessorNode(test).value);
    		test = head.right.right; // 10's next is null
    		System.out.println(test.value + " next: " + getSuccessorNode(test));
    	}
    
    }
    

    总结:

    • 如果该节点有右子树,那么节点的后继节点是右子树中最左的节点;
    • 如果该节点没有右子树,那么该节点和它的父亲节点逐步向上回溯,直到该节点是其父亲节点的左孩子,那么原节点的后继节点就是当前节点的父亲节点。

    拓展:如何求节点的前驱节点:

    • 如果该节点有左子树,那么节点的前驱节点是左子树中最右的节点;
    • 如果该节点没有左子树,那么该节点和它的父亲节点逐步向上回溯,直到该节点是其父亲节点的右孩子,那么原节点的前驱节点就是当前节点的父亲节点。
  3. 二叉树的序列化和反序列化

    package class_04;
    
    import java.util.LinkedList;
    import java.util.Queue;
    /**
     * 
     * 二叉树的序列化和反序列化
     *
     */
    public class Code_04_SerializeAndReconstructTree {
    
    	public static class Node {
    		public int value;
    		public Node left;
    		public Node right;
    
    		public Node(int data) {
    			this.value = data;
    		}
    	}
    	//二叉树的序列化
    	public static String serialByPre(Node head) {
    		if (head == null) {
    			return "#!";
    		}
    		String res = head.value + "!";
    		res += serialByPre(head.left);
    		res += serialByPre(head.right);
    		return res;
    	}
    
    	public static Node reconByPreString(String preStr) {
    		String[] values = preStr.split("!");
    		Queue<String> queue = new LinkedList<String>();
    		for (int i = 0; i != values.length; i++) {
    			queue.offer(values[i]);
    		}
    		return reconPreOrder(queue);
    	}
    
    	public static Node reconPreOrder(Queue<String> queue) {
    		String value = queue.poll();
    		if (value.equals("#")) {
    			return null;
    		}
    		Node head = new Node(Integer.valueOf(value));
    		head.left = reconPreOrder(queue);
    		head.right = reconPreOrder(queue);
    		return head;
    	}
    	//按层序列化
    	public static String serialByLevel(Node head) {
    		if (head == null) {
    			return "#!";
    		}
    		String res = head.value + "!";
    		Queue<Node> queue = new LinkedList<Node>();
    		queue.offer(head);
    		while (!queue.isEmpty()) {
    			head = queue.poll();
    			if (head.left != null) {
    				res += head.left.value + "!";
    				queue.offer(head.left);
    			} else {
    				res += "#!";
    			}
    			if (head.right != null) {
    				res += head.right.value + "!";
    				queue.offer(head.right);
    			} else {
    				res += "#!";
    			}
    		}
    		return res;
    	}
    
    	public static Node reconByLevelString(String levelStr) {
    		String[] values = levelStr.split("!");
    		int index = 0;
    		Node head = generateNodeByString(values[index++]);
    		Queue<Node> queue = new LinkedList<Node>();
    		if (head != null) {
    			queue.offer(head);
    		}
    		Node node = null;
    		while (!queue.isEmpty()) {
    			node = queue.poll();
    			node.left = generateNodeByString(values[index++]);
    			node.right = generateNodeByString(values[index++]);
    			if (node.left != null) {
    				queue.offer(node.left);
    			}
    			if (node.right != null) {
    				queue.offer(node.right);
    			}
    		}
    		return head;
    	}
    
    	public static Node generateNodeByString(String val) {
    		if (val.equals("#")) {
    			return null;
    		}
    		return new Node(Integer.valueOf(val));
    	}
    
    	// for test -- print tree
    	public static void printTree(Node head) {
    		System.out.println("Binary Tree:");
    		printInOrder(head, 0, "H", 17);
    		System.out.println();
    	}
    
    	public static void printInOrder(Node head, int height, String to, int len) {
    		if (head == null) {
    			return;
    		}
    		printInOrder(head.right, height + 1, "v", len);
    		String val = to + head.value + to;
    		int lenM = val.length();
    		int lenL = (len - lenM) / 2;
    		int lenR = len - lenM - lenL;
    		val = getSpace(lenL) + val + getSpace(lenR);
    		System.out.println(getSpace(height * len) + val);
    		printInOrder(head.left, height + 1, "^", len);
    	}
    
    	public static String getSpace(int num) {
    		String space = " ";
    		StringBuffer buf = new StringBuffer("");
    		for (int i = 0; i < num; i++) {
    			buf.append(space);
    		}
    		return buf.toString();
    	}
    
    	public static void main(String[] args) {
    		Node head = null;
    		printTree(head);
    
    		String pre = serialByPre(head);
    		System.out.println("serialize tree by pre-order: " + pre);
    		head = reconByPreString(pre);
    		System.out.print("reconstruct tree by pre-order, ");
    		printTree(head);
    
    		String level = serialByLevel(head);
    		System.out.println("serialize tree by level: " + level);
    		head = reconByLevelString(level);
    		System.out.print("reconstruct tree by level, ");
    		printTree(head);
    
    		System.out.println("====================================");
    
    		head = new Node(1);
    		printTree(head);
    
    		pre = serialByPre(head);
    		System.out.println("serialize tree by pre-order: " + pre);
    		head = reconByPreString(pre);
    		System.out.print("reconstruct tree by pre-order, ");
    		printTree(head);
    
    		level = serialByLevel(head);
    		System.out.println("serialize tree by level: " + level);
    		head = reconByLevelString(level);
    		System.out.print("reconstruct tree by level, ");
    		printTree(head);
    
    		System.out.println("====================================");
    
    		head = new Node(1);
    		head.left = new Node(2);
    		head.right = new Node(3);
    		head.left.left = new Node(4);
    		head.right.right = new Node(5);
    		printTree(head);
    
    		pre = serialByPre(head);
    		System.out.println("serialize tree by pre-order: " + pre);
    		head = reconByPreString(pre);
    		System.out.print("reconstruct tree by pre-order, ");
    		printTree(head);
    
    		level = serialByLevel(head);
    		System.out.println("serialize tree by level: " + level);
    		head = reconByLevelString(level);
    		System.out.print("reconstruct tree by level, ");
    		printTree(head);
    
    		System.out.println("====================================");
    
    		head = new Node(100);
    		head.left = new Node(21);
    		head.left.left = new Node(37);
    		head.right = new Node(-42);
    		head.right.left = new Node(0);
    		head.right.right = new Node(666);
    		printTree(head);
    
    		pre = serialByPre(head);
    		System.out.println("serialize tree by pre-order: " + pre);
    		head = reconByPreString(pre);
    		System.out.print("reconstruct tree by pre-order, ");
    		printTree(head);
    
    		level = serialByLevel(head);
    		System.out.println("serialize tree by level: " + level);
    		head = reconByLevelString(level);
    		System.out.print("reconstruct tree by level, ");
    		printTree(head);
    
    		System.out.println("====================================");
    
    	}
    }
    
    
  4. 判断一棵二叉树是否是平衡二叉树

    package class_04;
    /**
     * 
     * 判断一棵二叉树是否是平衡二叉树
     *
     */
    public class Code_06_IsBalancedTree {
    
    	public static class Node {
    		public int value;
    		public Node left;
    		public Node right;
    
    		public Node(int data) {
    			this.value = data;
    		}
    	}
    
    	public static boolean isBalance(Node head) {
    		boolean[] res = new boolean[1];
    		res[0] = true;
    		getHeight(head, 1, res);
    		return res[0];
    	}
    
    	public static int getHeight(Node head, int level, boolean[] res) {
    		if (head == null) {
    			return level;
    		}
    		int lH = getHeight(head.left, level + 1, res);
    		if (!res[0]) {
    			return level;
    		}
    		int rH = getHeight(head.right, level + 1, res);
    		if (!res[0]) {
    			return level;
    		}
    		if (Math.abs(lH - rH) > 1) {
    			res[0] = false;
    		}
    		return Math.max(lH, rH);
    	}
    
    	public static void main(String[] args) {
    		Node head = new Node(1);
    		head.left = new Node(2);
    		head.right = new Node(3);
    		head.left.left = new Node(4);
    		head.left.right = new Node(5);
    		head.right.left = new Node(6);
    		head.right.right = new Node(7);
    
    		System.out.println(isBalance(head));
    
    	}
    
    }
    
    
  5. 判断一棵树是否是搜索二叉树、完全二叉树

    当且仅当二叉树中序遍历是升序,则是搜索二叉树(不考虑相等的节点)

    非递归中序遍历二叉树判断搜索二叉树相对于递归方式中序遍历更简单一些。

package class_04;

import java.util.LinkedList;
import java.util.Queue;
/**
 * 
 * 判断一棵树是否是搜索二叉树
 * 判断一棵树是否是完全二叉树
 *
 */
public class Code_07_IsBSTAndCBT {

	public static class Node {
		public int value;
		public Node left;
		public Node right;

		public Node(int data) {
			this.value = data;
		}
	}
	//判断一棵树是否是搜索二叉树
	public static boolean isBST(Node head) {
		if (head == null) {
			return true;
		}
		boolean res = true;
		Node pre = null;
		Node cur1 = head;
		Node cur2 = null;
		// Moris遍历(比较难)
		while (cur1 != null) {
			cur2 = cur1.left;
			if (cur2 != null) {
				while (cur2.right != null && cur2.right != cur1) {
					cur2 = cur2.right;
				}
				if (cur2.right == null) {
					cur2.right = cur1;
					cur1 = cur1.left;
					continue;
				} else {
					cur2.right = null;
				}
			}
			if (pre != null && pre.value > cur1.value) {
				res = false;
			}
			pre = cur1;
			cur1 = cur1.right;
		}
		return res;
	}
	//判断一棵树是否是完全二叉树
	public static boolean isCBT(Node head) {
		if (head == null) {
			return true;
		}
		Queue<Node> queue = new LinkedList<Node>();
		boolean leaf = false;
		Node l = null;
		Node r = null;
		queue.offer(head);
		while (!queue.isEmpty()) {
			head = queue.poll();
			l = head.left;
			r = head.right;
			if ((leaf && (l != null || r != null)) || (l == null && r != null)) {
				return false;
			}
			if (l != null) {
				queue.offer(l);
			}
			if (r != null) {
				queue.offer(r);
			} else {
				leaf = true;
			}
		}
		return true;
	}

	// for test -- print tree
	public static void printTree(Node head) {
		System.out.println("Binary Tree:");
		printInOrder(head, 0, "H", 17);
		System.out.println();
	}

	public static void printInOrder(Node head, int height, String to, int len) {
		if (head == null) {
			return;
		}
		printInOrder(head.right, height + 1, "v", len);
		String val = to + head.value + to;
		int lenM = val.length();
		int lenL = (len - lenM) / 2;
		int lenR = len - lenM - lenL;
		val = getSpace(lenL) + val + getSpace(lenR);
		System.out.println(getSpace(height * len) + val);
		printInOrder(head.left, height + 1, "^", len);
	}

	public static String getSpace(int num) {
		String space = " ";
		StringBuffer buf = new StringBuffer("");
		for (int i = 0; i < num; i++) {
			buf.append(space);
		}
		return buf.toString();
	}

	public static void main(String[] args) {
		Node head = new Node(4);
		head.left = new Node(2);
		head.right = new Node(6);
		head.left.left = new Node(1);
		head.left.right = new Node(3);
		head.right.left = new Node(5);

		printTree(head);
		System.out.println(isBST(head));
		System.out.println(isCBT(head));

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值