输入两棵二叉树A和B,判断B是不是A的子结构 || 二叉树的按层打印与之字形打印 || 将一棵二叉搜索树转化为排序的双向链表 || 判断一棵二叉树是不是对称二叉树

14 篇文章 0 订阅
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class BinaryTree {
	
	private Node root;
	
	private Node lastNodeInList = null;
	
	public BinaryTree(int val) {
		root = new Node(val);
	}
	
	/**
	 * 输入两棵二叉树A和B,判断B是不是A的子结构
	 * @param root1
	 * @param root2
	 * @return
	 */
	public static boolean hasSubTree(Node root1, Node root2) {
		boolean result = false;
		if (root1 != null && root2 != null) {
			if (root1.value == root2.value)
				result = doesTree1HasTree2(root1, root2);
			if (!result)
				result = hasSubTree(root1.left, root2);
			if (!result)
				result = hasSubTree(root1.right, root2);
		}
		return result;
	}

	private static boolean doesTree1HasTree2(Node root1, Node root2) {
		if (root2 == null)//这个必须放前面,不然出错
			return true;
		if (root1 == null)
			return false;
		
		if (root1.value != root2.value)
			return false;
		
		return doesTree1HasTree2(root1.left, root2.left) &&
				doesTree1HasTree2(root1.right, root2.right);
	}
	
	public void printByLevel() {
		if (root == null)
			return;
		
		List<Node> list = new LinkedList<Node>();
		Node node = root;
		list.add(node);
		while (!list.isEmpty()) {
			node = list.get(0);
			System.out.print(node.value + "  ");
			if (node.left != null)
				list.add(node.left);
			if (node.right != null)
				list.add(node.right);
			list.remove(0);
		}
	}
	
	/**
	 * 把二叉树打印成多行
	 */
	public void printMultiByLevel() {
		if (root == null)
			return;
		
		int nextLevel = 0;
		int toBePrint = 1;
		List<Node> list = new LinkedList<Node>();
		Node node = root;
		list.add(node);
		while (!list.isEmpty()) {
			node = list.get(0);
			System.out.print(node.value + "  ");
			if (node.left != null) {
				list.add(node.left);
				nextLevel++;
			}
			if (node.right != null) {
				list.add(node.right);
				nextLevel++;
			}
			list.remove(0);
			toBePrint--;
			if (toBePrint == 0) {
				System.out.println();
				toBePrint = nextLevel;
				nextLevel = 0;
			}
		}
	}
	
	/**
	 * 按之字形顺序打印二叉树
	 */
	public void printByZigzag() {
		if (root == null)
			return;
		
		int nextLevel = 0;
		int toBePrint = 1;
		Stack<Node> stack1 = new Stack<>();
		Node node = root;
		stack1.push(node);
		int level = 1;
		Stack<Node> stack2 = new Stack<>();
		
		while (!stack1.isEmpty() || !stack2.isEmpty()) {
			if ((level & 0x1) != 0) {
				node = stack1.lastElement();
				System.out.print(node.value + "  ");
				if (node.left != null) {
					stack2.push(node.left);
					nextLevel++;
				}
				if (node.right != null) {
					stack2.push(node.right);
					nextLevel++;
				}
				stack1.pop();
				if (stack1.isEmpty())
					level++;
				
			} else {
				node = stack2.lastElement();
				System.out.print(node.value + "  ");
				if (node.right != null) {
					stack1.push(node.right);
					nextLevel++;
				}
				if (node.left != null) {
					stack1.push(node.left);
					nextLevel++;
				}
				stack2.pop();
				if (stack2.isEmpty())
					level++;
			}
			
			toBePrint--;
			if (toBePrint == 0) {
				System.out.println();
				toBePrint = nextLevel;
				nextLevel = 0;
			}
		}
	}
	
	/**
	 * 判断一棵二叉树是不是对称二叉树
	 * @return
	 */
	public boolean isSymmetrical() {
		return isSymmetrical(root, root);
	}
	
	private boolean isSymmetrical(Node root1, Node root2) {
		if (root1 == null && root2 == null)
			return true;
		if (root1 == null || root2 == null)
			return false;
		if (root1.value != root2.value)
			return false;
		return isSymmetrical(root1.left, root2.right) 
				&& isSymmetrical(root1.right, root2.left);
	}
	
	
	/**
	 * 将一棵二叉搜索树转化为排序的双向链表(不能创建新的结点)
	 * 参考网站:http://blog.csdn.net/ljianhui/article/details/22338405
	 * @param root
	 * @return
	 */
	public Node convert() {
		convert(root);
		//返回到头结点
		Node headNode = lastNodeInList;
		while (headNode != null && headNode.left != null)
			headNode = headNode.left;
		return headNode;
	}

	private void convert(Node node) {
		if (node == null)
			return;
		
		Node current = node;
		if (current.left != null)
			convert(node.left);
		
		current.left = lastNodeInList;
		if (lastNodeInList != null)
			lastNodeInList.right = current;
		lastNodeInList = current;
		
		if (current.right != null)
			convert(node.right);
	}

	public static void main(String[] args) {
		BinaryTree tree = new BinaryTree(8);
		Node node1 = new Node(8);
		Node node2 = new Node(7);
		Node node3 = new Node(9);
		Node node4 = new Node(2);
		Node node5 = new Node(4);
		Node node6 = new Node(7);
		tree.root.left = node1;
		tree.root.right = node2;
		node1.left = node3;
		node1.right = node4;
		node4.left = node5;
		node4.right = node6;
		tree.printByLevel();
		System.out.println();
		
		BinaryTree sub = new BinaryTree(8);
		Node node7 = new Node(9);
		Node node8 = new Node(2);
		sub.root.left = node7;
		sub.root.right = node8;
		sub.printByLevel();
		System.out.println();
		
		System.out.println(hasSubTree(tree.root, sub.root));
		
		tree.printMultiByLevel();
		System.out.println();
		sub.printMultiByLevel();
		System.out.println();
		tree.printByZigzag();
		
		BinaryTree zigzag = new BinaryTree(1);
		Node n2 = new Node(2);
		Node n3 = new Node(3);
		Node n4 = new Node(4);
		Node n5 = new Node(5);
		Node n6 = new Node(6);
		Node n7 = new Node(7);
		Node n8 = new Node(8);
		Node n9 = new Node(9);
		Node n10 = new Node(10);
		Node n11 = new Node(11);
		Node n12 = new Node(12);
		Node n13 = new Node(13);
		Node n14 = new Node(14);
		Node n15 = new Node(15);
		zigzag.root.left = n2;
		zigzag.root.right = n3;
		n2.left = n4;
		n2.right = n5;
		n3.left = n6;
		n3.right = n7;
		n4.left = n8;
		n4.right = n9;
		n5.left = n10;
		n5.right = n11;
		n6.left = n12;
		n6.right = n13;
		n7.left = n14;
		n7.right = n15;
		System.out.println();
		zigzag.printByZigzag();
		System.out.println();
		zigzag.printByLevel();
		System.out.println();
		zigzag.printMultiByLevel();
		System.out.println();
		System.out.println(zigzag.isSymmetrical());
		
		BinaryTree sym = new BinaryTree(8);
		Node s1 = new Node(6);
		Node s2 = new Node(6);
		Node s3 = new Node(5);
		Node s4 = new Node(7);
		Node s5 = new Node(7);
		Node s6 = new Node(5);
		sym.root.left = s1;
		sym.root.right = s2;
		s1.left = s3;
		s1.right = s4;
		s2.left = s5;
		s2.right = s6;
		System.out.println(sym.isSymmetrical());
		
		BinaryTree sort = new BinaryTree(10);
		Node so1 = new Node(6);
		Node so2 = new Node(14);
		Node so3 = new Node(4);
		Node so4 = new Node(8);
		Node so5 = new Node(12);
		Node so6 = new Node(16);
		sort.root.left = so1;
		sort.root.right = so2;
		so1.left = so3;
		so1.right = so4;
		so2.left = so5;
		so2.right = so6;
		sort.printMultiByLevel();
		System.out.println();
		Node ret = sort.convert();
		while (ret != null) {
			System.out.print(ret.value + "  ");
			ret = ret.right;
		}
		
	}
}

class Node {
	int value;
	Node left;
	Node right;
	
	public Node(int value) {
		this.value = value;
	}
	
	public Node(int value, Node left, Node right) {
		this.value = value;
		this.left = left;
		this.right = right;
	}
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值