Find the maximum topological structure in a two-fork tree with the condition of searching binary tree __ two fork Tree

Source: Internet
Author: User


Import java.util.*; 
		Find the maximum topology of a two-fork tree conforming to the search binary tree condition public class maxsearchtreetuo{//two fork tree node definition public static class node{public int value;
		Public Node left;

		public Node right;
		public Node (int data) {this.value=data;
		}///Get maximum topology size public static int bsttopsize (Node head) {if (head==null) {return 0;
		int Max=maxtopo (head,head);
		Max=math.max (Bsttopsize (head.left), Max);

        Max=math.max (Bsttopsize (head.right), Max);
	return Max; }//Get the maximum topological substructure of a node public static int Maxtopo (node H,node n) {if H!=null&&n!=null&&isbstnode (H,n,n.valu
		e)) {return Maxtopo (h,n.left) +maxtopo (h,n.right) +1;
     
	return 0;
		///Determine if the topology is a node public static Boolean Isbstnode for topological substructure (node H,node n,int value) {if (H==null) {return false;
		} if (H==n) {return true;

	Return Isbstnode (H.value>value?h.left:h.right,n,value);  }//Method two uses the topology contribution records public static class record {the public int l; Zoozi Tree contribution value public inT R;
			Right subtree contribution public record (int. left, int right) {THIS.L = left;
		THIS.R = right;
		} public static int bstTopoSize2 (Node head) {map<node, record> Map = new Hashmap<node, record> ();
	Return Posorder (head, map);
		public static int Posorder (Node h, Map<node, record> Map) {if (H = = null) {return 0;
		int ls = posorder (h.left, map);
		int rs = Posorder (h.right, map);
		Modifymap (H.left, H.value, map, true);
		Modifymap (H.right, H.value, map, false);
		Record LR = Map.get (h.left);
		Record rr = Map.get (h.right); int LBST = LR = null?
		0:LR.L + LR.R + 1; int RBST = RR = = null?
		0:RR.L + RR.R + 1;
		Map.put (H, new record (Lbst, rBST));
	Return Math.max (Lbst + rBST + 1, Math.max (LS, RS)); public static int Modifymap (Node n, int v, Map<node, Record> m, Boolean s) {if (n = = NULL | | (!m.containskey (n)))
		{return 0;
		The Record R = M.get (n); if ((S && n.value > V) | | ((!s) && N.value < v))
			{M.remove (n);
		Return R.L + R.R + 1;
			else {int minus = Modifymap (S. N.right:n.left, V, M, s);
			if (s) {R.R = R.r-minus;
			else {r.l = R.l-minus;
			} m.put (n, R);
		return minus;
         } public static void Main (String []args) {//system.out.println ("Hello");
          Node Node=new node (12);
          Node.left=new Node (10);
          Node.right=new Node (13);
          Node.left.left=new Node (4);
          Node.left.right=new Node (14);
          Node.right.left=new Node (20);
          Node.right.right=new Node (16);
          Node.left.left.left=new Node (2);
          Node.left.left.right=new Node (5);
          Node.left.right.left=new Node (11);

          Node.left.right.right=new Node (15);
          System.out.println (bsttopsize (node));

	System.out.println (BstTopoSize2 (node)); }
}


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.