Find the successor node of a node in the binary tree __ Developer

Source: Internet
Author: User

Find the successor node of a node in the binary tree public class getnextnode{//node definition public static class node{public int value;
		Public Node left;
		public Node right;

		public Node parent;
		public Node (int data) {this.value=data;
		}///Given a node to obtain its successor node public static node Getnextnode (node node) {//if (node==null) {return node;
		//If there is a subtree, then the successor node is the leftmost node in the right subtree if (node.right!=null) {return getleftmost (node.right);
			The}//node is moved up to find the parent node else{nodes parent=node.parent;
				while (Parent!=null&&parent.left!=node) {node=parent;
			Parent=node.parent;
		return to parent;
		The 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) {/** constructed two-fork Tree 6 3 9 1
		4 8 2 5 7/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:" + getnextnode (test). Value);
		Test = Head.left.left.right;
		System.out.println (Test.value + "Next:" + getnextnode (test). Value);
		Test = Head.left;
		System.out.println (Test.value + "Next:" + getnextnode (test). Value);
		Test = Head.left.right; System.out.println (Test.value + "nExt: "+ getnextnode (test). Value);
		Test = Head.left.right.right;
		System.out.println (Test.value + "Next:" + getnextnode (test). Value);
		Test = head;
		System.out.println (Test.value + "Next:" + getnextnode (test). Value);
		Test = Head.right.left.left;
		System.out.println (Test.value + "Next:" + getnextnode (test). Value);
		Test = Head.right.left;
		System.out.println (Test.value + "Next:" + getnextnode (test). Value);
		Test = Head.right;
		System.out.println (Test.value + "Next:" + getnextnode (test). Value); Test = Head.right.right;
	The ' s next is null System.out.println (Test.value + "Next:" + getnextnode (test)); }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.