Resolve current project encounters multi-fork Tree Second Edition

Source: Internet
Author: User
Tags addall

The second edition mainly addresses the following issues:

1. Do not specify the root node

2. Cannot have more than one root node

Directly on the code:

 PackageCOM.ARCHIVE.CPR;/*** Tree node *@authorXingxing_li **/ Public classTreeNode {PrivateString Bizid; PrivateString refname;  PublicString Getbizid () {returnBizid; }     Public voidSetbizid (String bizid) { This. Bizid =Bizid; }     PublicString Getrefname () {returnrefname; }     Public voidsetrefname (String refname) { This. refname =refname; } @Override Public inthashcode () {StringBuffer sb=NewStringBuffer (100);         Sb.append (Bizid). Append (refname); returnsb.tostring (). Hashcode (); } @Override Public Booleanequals (Object obj) {if(objinstanceofTreeNode) {TreeNode Tmpnode=(TreeNode) obj; if(NULL!=Tmpnode) {                if( This. Bizid.equals (Tmpnode.getbizid ()) && This. Refname.equals (Tmpnode.getrefname ()))return true ; }            return false; }        return Super. Equals (obj); }        }
 PackageCOM.ARCHIVE.CPR;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.LinkedList;Importjava.util.List;ImportJava.util.Queue;ImportJava.util.Stack;/*** Multi-fork tree structure *@authorXingxing_li **/ Public classManytree {PrivateTreeNode node; PrivateList<manytree> childNodes =NewArraylist<manytree>(); Private StaticManytree root =NewManytree ();//Virtual one root node//Private Boolean isRoot = false;         PublicManytree () {}/*** Insert a node into a multi-fork tree *@param_node inserted node *@param_pnode Parent Node*/     Public voidAdd (TreeNode _node,treenode _pnode) {if(NULL==_pnode) {            //If the parent node is NULL, add it directly to the root nodeManytree Tmpmanytree =NewManytree ();            Tmpmanytree.setnode (_node);        Root.getchildnodes (). Add (Tmpmanytree); }Else {            //find the parent node of PnodeManytree Manytree =Findparentmanytree (_pnode); //If the parent node is NULL, add it directly to the root node            if(NULL==Manytree) {Manytree Tmpmanytree=NewManytree ();                Tmpmanytree.setnode (_pnode);                Root.getchildnodes (). Add (Tmpmanytree); /*manytree tmpManyTree1 = new Manytree ();                Tmpmanytree1.setnode (_node);                Tmpmanytree.getchildnodes (). Add (TMPMANYTREE1); */Add (_node,_pnode); }Else {                //If the parent node is found, then the node node is added to the Lookup parent nodeManytree Tmpmanytree =NewManytree ();                Tmpmanytree.setnode (_node);            Manytree.getchildnodes (). Add (Tmpmanytree); }                    }    }        /*** Get the reverse order set of the leaf nodes of the multi-fork tree to the parameter nodes *@paramManytree *@return     */     PublicList<treenode>getreversesequence () {List<TreeNode> Lsttreenode =NewArraylist<treenode> (20); if(Root.getchildnodes (). Size () = = 0) {Lsttreenode.add ( This. GetNode ()); returnLsttreenode; } Stack<TreeNode> stack =NewStack<treenode>(); Queue<ManyTree> queue =NewLinkedlist<manytree>() ;                Queue.addall (Root.getchildnodes ()); Manytree Tmptreenode=NULL ; List<ManyTree> tmpchildnodes =NULL ;  while(!Queue.isempty ()) {Tmptreenode=Queue.remove ();            Stack.add (Tmptreenode.getnode ()); Tmpchildnodes=tmptreenode.getchildnodes (); if(Tmpchildnodes.size () >0) {queue.addall (tmpchildnodes); }} TreeNode node=NULL ;  while(!Stack.isempty ()) {Node=Stack.pop (); if(!lsttreenode.contains (node)) lsttreenode.add (node); }        returnLsttreenode; }        //Locate the Manytree node on which the Pnode resides    Privatemanytree findparentmanytree (TreeNode _pnode) {if(node = =_pnode) {            return  This ; } manytree Manytree=Findmanytree (Root.getchildnodes (), _pnode); returnManytree; }    //Recursive lookup    PrivateManytree Findmanytree (list<manytree>_lstmanytree,treenode _pnode) {Iterator<ManyTree> iter =_lstmanytree.iterator (); Manytree Manytree=NULL ; Manytree Rtnmanytree=NULL;  while(Iter.hasnext ()) {Manytree=Iter.next (); if(_pnode.hashcode () = =Manytree.getnode (). Hashcode ()) {Rtnmanytree=Manytree; returnRtnmanytree; }Else {                //because of the deep recursion, the return value is a bit special, and the value of the call is combined with the value of the previous layer call .Manytree RTN =Findmanytree (Manytree.getchildnodes (), _pnode); Rtnmanytree= (NULL!=RTN)?Rtn:rtnmanytree; }        }        returnRtnmanytree; }     PublicTreeNode GetNode () {returnnode; }     Public voidSetnode (TreeNode node) { This. Node =node; }         PublicList<manytree>getchildnodes () {returnChildNodes; }     Public voidSetchildnodes (list<manytree>childNodes) {         This. ChildNodes =ChildNodes; } @Override Public inthashcode () {return  This. Node.hashcode (); }        }
example of the caller: PackageCOM.ARCHIVE.CPR;ImportJava.util.Iterator;Importjava.util.List; Public classTestmanytree { Public Static voidMain (string[] args) {TreeNode Node1=NewTreeNode (); Node1.setbizid ("1_stock_base"); Node1.setrefname ("BIZ_ID"); Manytree Manytree=NewManytree (); Manytree.add (Node1,NULL); TreeNode Node2=NewTreeNode (); Node2.setbizid ("2_account"); Node2.setrefname ("BIZ_ID"); TreeNode Node3=NewTreeNode (); Node3.setbizid ("3_character"); Node3.setrefname ("BIZ_ID"); TreeNode Node4=NewTreeNode (); Node4.setbizid ("4_detail"); Node4.setrefname ("BIZ_ID"); TreeNode Node5=NewTreeNode (); Node5.setbizid ("5_detail"); Node5.setrefname ("BIZ_ID"); TreeNode Node6=NewTreeNode (); Node6.setbizid ("6_detail"); Node6.setrefname ("BIZ_ID");        Manytree.add (Node2, Node1);        Manytree.add (Node3, Node1);        Manytree.add (Node4, Node1);        Manytree.add (NODE5, NODE3);        Manytree.add (Node6, NODE4);                Manytree.add (NODE5, node6); List<TreeNode> Lsttreenode =manytree.getreversesequence ();  for(Iterator<treenode> iter =lsttreenode.iterator (); Iter.hasnext ();) {System.out.println (Iter.next (). Getbizid ()); } System.out.println ("Constructor complete!"); }}

Resolve current project encounters multi-fork Tree Second Edition

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.