Constructs an array of maxtree (binary tree) _ Two fork Tree

Source: Internet
Author: User

Here are some examples:


The implementation code is as follows:

Import Java.util.Stack;
Import Java.util.HashMap;
     The Maxtree public class maxtree{//Two fork tree node of the constructed array is defined as public static class node{public int value;
     Public Node left;
     
     public Node right;
     Node initialization public node (int data) {this.value=data;  }///construct the Maxtree function of the array public static Node Getmaxtree (int[] arr) {node[] Narr = new Node[arr.length];
		An array of two-fork tree nodes for (int i = 0; I!= arr.length; i++) {narr[i] = new node (arr[i)); stack<node> stack = new stack<node> ()//use stack to find the first number on the left right side larger than itself hashmap<node, node> lbigmap = new Hashma
		P<node, node> ();
		Hashmap<node, node> rbigmap = new Hashmap<node, node> ();
			for (int i = 0; I!= narr.length i++) {Node curnode = narr[i]; 
			while ((!stack.isempty ()) && Stack.peek (). Value < Curnode.value) {Popstacksetmap (stack, lbigmap); a series of operations on the stack
		} stack.push (Curnode); while (!stack.isempty ()) {Popstacksetmap stack, lbigMAP);
			for (int i = narr.length-1 i!=-1; i--) {Node curnode = narr[i]; 
			while ((!stack.isempty ()) && Stack.peek (). Value < Curnode.value) {Popstacksetmap (stack, rbigmap); a series of operations on the stack
		} stack.push (Curnode);
		while (!stack.isempty ()) {Popstacksetmap (stack, rbigmap); Node head = null;
			The Declaration Header node for (int i = 0; I!= narr.length; i++) {Node curnode = narr[i];
			Node left = Lbigmap.get (Curnode);
			Node right = Rbigmap.get (Curnode);
			if (left = null && OK = null) {head = Curnode;
				else if (left = = null) {if (Right.left = null) {right.left = Curnode;
				else {right.right = Curnode;
				} else if (right = = null) {if (Left.left = null) {left.left = Curnode;
				else {left.right = Curnode;
				} else {//Select the left and right few as parent node parent = Left.value < right.value? left:right;
				if (Parent.left = = null) {parent.left = Curnode; else {Parent.right = Curnode;
	}} return head;  A series of operations on//stacks public static void Popstacksetmap (Stack <node>stack,hashmap<node,node>map) {Node

     Popnode=stack.pop ();
     if (Stack.isempty ()) {map.put (popnode,null); else {map.put (Popnode,stack.peek ());//construct two-fork tree operations}//Two-tree first-order traversal public static void PRI
    	Ntpreorder (Node head) {if (head==null) {return;
    	} System.out.print (head.value+ "");  Printpreorder (Head.left);
    Recursive call Traversal binary tree Printpreorder (head.right);
    	The middle sequence of the//two fork tree traverses the public static void Printinorder (Node head) {if (head==null) {return;
    	} printpreorder (Head.left);
    	System.out.print (head.value+ "");
    Printpreorder (Head.right);
      public static void Main (String []args) {int []arr={3,4,5,1,2};
      Node Head=getmaxtree (arr);
      Printpreorder (head);
      System.out.println ();

Printinorder (head);   }
} 


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.