print binary tree boundary

63 篇文章 0 订阅

顺时针的print binary tree boundary, 就是从根开始,先打右边界,再打叶子,最后打左边界。

package tree;

class Node {
	int data;
	Node left, right;

	Node(int item) {
		data = item;
		left = right = null;
	}
}

class PrintBoundary {
	Node root;
	StringBuilder sb = new StringBuilder();

	// A simple function to print leaf nodes of a binary tree
	void printLeaves(Node node) {
		if (node != null) {

			printLeaves(node.right);
			// Print it if it is a leaf node
			if (node.left == null && node.right == null) {
				System.out.print(node.data + " ");
				sb.append(node.data + " ");
			}
			printLeaves(node.left);
		}
	}

	// A function to print all left boundry nodes, except a leaf node.
	// Print the nodes in TOP DOWN manner
	void printBoundaryLeft(Node node) {
		if (node != null) {
			if (node.left != null) {

				// to ensure top down order, print the node
				// before calling itself for left subtree
				System.out.print(node.data + " ");
				sb.append(node.data + " ");
				printBoundaryLeft(node.left);
			} else if (node.right != null) {
				System.out.print(node.data + " ");
				sb.append(node.data + " ");
				printBoundaryLeft(node.right);
			}

			// do nothing if it is a leaf node, this way we avoid
			// duplicates in output
		}
	}

	// A function to print all right boundry nodes, except a leaf node
	// Print the nodes in BOTTOM UP manner
	void printBoundaryRight(Node node) {
		if (node != null) {
			if (node.right != null) {
				// to ensure bottom up order, first call for right
				// subtree, then print this node
				printBoundaryRight(node.right);
				System.out.print(node.data + " ");
				sb.append(node.data + " ");
			} else if (node.left != null) {
				printBoundaryRight(node.left);
				System.out.print(node.data + " ");
				sb.append(node.data + " ");
			}
			// do nothing if it is a leaf node, this way we avoid
			// duplicates in output
		}
	}

	// A function to do boundary traversal of a given binary tree
	void printBoundary(Node node) {
		if (node != null) {
			System.out.print(node.data + " ");
			sb.append(node.data + " ");
			// Print the right boundary in bottom-up manner
			printBoundaryRight(node.right);

			printLeaves(node.right);
			// Print all leaf nodes
			printLeaves(node.left);

			// Print the left boundary in top-down manner.
			printBoundaryLeft(node.left);
		}
	}

	// Driver program to test above functions
	public static void main(String args[]) {
		PrintBoundary tree = new PrintBoundary();
		tree.root = new Node(20);
		tree.root.left = new Node(8);
		tree.root.left.left = new Node(4);
		tree.root.left.right = new Node(12);
		tree.root.left.right.left = new Node(10);
		tree.root.left.right.right = new Node(14);
		tree.root.right = new Node(22);
		tree.root.right.right = new Node(25);
		tree.printBoundary(tree.root);
		System.out.println();
		System.out.println(tree.sb.toString());
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值