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); }
}