打印二叉树的边界节点

Posted 三名狂客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了打印二叉树的边界节点相关的知识,希望对你有一定的参考价值。


public class PrintEdgeNodes 
   
	//二叉树节点的定义
	public static class Node 
		public int value;
		public Node left;
		public Node right;

		public Node(int data) 
			this.value = data;
		
	
   //按照标准一,打印节点
	public static void printEdge1(Node head) 
		if (head == null) 
			return;
		
		int height = getHeight(head, 0);
		Node[][] edgeMap = new Node[height][2];
		setEdgeMap(head, 0, edgeMap);
		// print left edge
		for (int i = 0; i != edgeMap.length; i++) 
			System.out.print(edgeMap[i][0].value + " ");
		
		// print leaf node but not in map
		printLeafNotInMap(head, 0, edgeMap);
		// print right edge but not left edge
		for (int i = edgeMap.length - 1; i != -1; i--) 
			if (edgeMap[i][0] != edgeMap[i][1]) 
				System.out.print(edgeMap[i][1].value + " ");
			
		
		System.out.println();
	

	public static int getHeight(Node h, int l) 
		if (h == null) 
			return l;
		
		return Math.max(getHeight(h.left, l + 1), getHeight(h.right, l + 1));
	

	public static void setEdgeMap(Node h, int l, Node[][] edgeMap) 
		if (h == null) 
			return;
		
		edgeMap[l][0] = edgeMap[l][0] == null ? h : edgeMap[l][0];
		edgeMap[l][1] = h;
		setEdgeMap(h.left, l + 1, edgeMap);
		setEdgeMap(h.right, l + 1, edgeMap);
	

	public static void printLeafNotInMap(Node h, int l, Node[][] m) 
		if (h == null) 
			return;
		
		if (h.left == null && h.right == null && h != m[l][0] && h != m[l][1]) 
			System.out.print(h.value + " ");
		
		printLeafNotInMap(h.left, l + 1, m);
		printLeafNotInMap(h.right, l + 1, m);
	
  
	//按照标准二打印节点
	public static void printEdge2(Node head) 
		if (head == null) 
			return;
		
		System.out.print(head.value + " ");
		if (head.left != null && head.right != null) 
			printLeftEdge(head.left, true);
			printRightEdge(head.right, true);
		 else 
			printEdge2(head.left != null ? head.left : head.right);
		
		System.out.println();
	

	public static void printLeftEdge(Node h, boolean print) 
		if (h == null) 
			return;
		
		if (print || (h.left == null && h.right == null)) 
			System.out.print(h.value + " ");
		
		printLeftEdge(h.left, print);
		printLeftEdge(h.right, print && h.left == null ? true : false);
	

	public static void printRightEdge(Node h, boolean print) 
		if (h == null) 
			return;
		
		printRightEdge(h.left, print && h.right == null ? true : false);
		printRightEdge(h.right, print);
		if (print || (h.left == null && h.right == null)) 
			System.out.print(h.value + " ");
		
	

	public static void main(String[] args) 
		Node head = new Node(1);
		head.left = new Node(2);
		head.right = new Node(3);
		head.left.right = new Node(4);
		head.right.left = new Node(5);
		head.right.right = new Node(6);
		head.left.right.left = new Node(7);
		head.left.right.right = new Node(8);
		head.right.left.left = new Node(9);
		head.right.left.right = new Node(10);
		head.left.right.right.right = new Node(11);
		head.right.left.left.left = new Node(12);
		head.left.right.right.right.left = new Node(13);
		head.left.right.right.right.right = new Node(14);
		head.right.left.left.left.left = new Node(15);
		head.right.left.left.left.right = new Node(16);

		printEdge1(head);
		printEdge2(head);

	




以上是关于打印二叉树的边界节点的主要内容,如果未能解决你的问题,请参考以下文章

左神算法书籍《程序员代码面试指南》——3_02打印二叉树的边界节点★★

二叉树的边界打印

二叉树的下一个节点

求上到下打印出二叉树的每个节点三种题型

二叉树刷题篇(完) 二叉搜索树的修剪构造与转换

打印二叉树的所有最右节点