Java Two fork tree implementation code

Source: Internet
Author: User

 Public classTree {PrivateTreeNode root =NULL;  PublicTree () {root=NewTreeNode (1, "A"); }        Private classTreeNode {Private intkey; PrivateString data; Private Booleanisvisted; PrivateTreeNode Leftchild; PrivateTreeNode Rightchild;  PublicTreeNode (intkey, String data) {             This. Key =key;  This. data =data;  This. Leftchild =NULL;  This. Rightchild =NULL;  This. isvisted =false; }    }    /*** Create a binary tree * A * B C * D E F * createbintree: (). * @authorWei Jian *@paramRoot*/     Public voidcreatebintree (TreeNode root) {TreeNode Newnodeb=NewTreeNode (2, "B"); TreeNode Newnodec=NewTreeNode (3, "C"); TreeNode newnoded=NewTreeNode (4, "D"); TreeNode Newnodee=NewTreeNode (5, "E"); TreeNode Newnodef=NewTreeNode (6, "F"); Root.leftchild=Newnodeb; Root.rightchild=Newnodec;//newnodec.rightchild = newnodef;//newnodeb.leftchild = newnoded;//newnodeb.rightchild = Newnodee;Root.rightChild.rightChild =Newnodef; Root.leftChild.leftChild=newnoded; Root.leftChild.rightChild=Newnodee; }         Public BooleanIsEmpty () {returnRoot = =NULL; }    //height of the tree     Public intheight () {returnheight (root); }    //Number of nodes     Public intsize () {returnsize (root); }        Private intheight (TreeNode subtree) {if(Subtree = =NULL)            return0; inti =height (subtree.leftchild); intj =height (subtree.rightchild); returnI < J? J + 1:i + 1; }        Private intsize (TreeNode subtree) {if(Subtree = =NULL)            return0; return1 + size (subtree.leftchild) +size (subtree.rightchild); }    //Return parent Node     PublicTreeNode Parent (TreeNode element) {return(Root = =NULL|| Root = =Element)?NULL: Parent (root, Element); }         PublicTreeNode Parent (TreeNode subtree, TreeNode element) {if(Subtree = =NULL)            return NULL; if(Subtree.leftchild = = Element | | subtree.rightchild = =Element) {            //returns the parent node address            returnsubtree;        } TreeNode p; //look in the left subtree first, and if not found in the left dial hand tree, look in the right subtree        if(p = parent (subtree.leftchild, Element))! =NULL)            returnP//recursive left subtree search        Else            returnparent (subtree.rightchild, Element); }         PublicTreeNode Getleftchildnode (TreeNode element) {returnElement! =NULL? Element.leftchild:NULL; }         PublicTreeNode Getrightchildnode (TreeNode element) {returnElement! =NULL? Element.rightchild:NULL; }         PublicTreeNode Getroot () {returnRoot; }        //when a node is freed, the left and right subtrees of that node are released,//Therefore, a subsequent traversal should be used to release the storage space of the node when a node is accessed     Public voidDistroy (TreeNode subtree) {//Delete a subtree with the root of subtree        if(Subtree! =NULL) {            //Delete left subtreeDistroy (Subtree.leftchild); //Delete Right sub-treeDistroy (Subtree.rightchild); //Delete root nodesubtree =NULL; }    }         Public voidTraverse (TreeNode subtree) {System.out.println ("Key:" + Subtree.key + "--name:" +subtree.data);        Traverse (Subtree.leftchild);    Traverse (Subtree.rightchild); }    //Pre-sequence traversal     Public voidPerorder (TreeNode subtree) {if(Subtree! =NULL) {visted (subtree);            Perorder (Subtree.leftchild);        Perorder (Subtree.rightchild); }    }    //Middle Sequence Traversal     Public voidinorder (TreeNode subtree) {if(Subtree! =NULL) {perorder (subtree.leftchild);            visted (subtree);        Perorder (Subtree.rightchild); }    }    //Post-post traversal     Public voidpostorder (TreeNode subtree) {if(Subtree! =NULL) {perorder (subtree.leftchild);            Perorder (Subtree.rightchild);        visted (subtree); }    }    //Mirror symmetry of the two-fork tree     Public voidMirror (TreeNode subtree) {if(Subtree = =NULL)            return; TreeNode node=NewTreeNode (Subtree.key, subtree.data); Node.leftchild=Subtree.leftchild; Subtree.leftchild=Subtree.rightchild; Subtree.rightchild=Node.leftchild;        Mirror (Subtree.leftchild);    Mirror (Subtree.rightchild); }         Public voidvisted (TreeNode subtree) {subtree.isvisted=true; System.out.println ("Key:" + Subtree.key + "---data:" +subtree.data); }    //Test     Public Static voidMain (string[] args) {Tree bt=NewTree ();                Bt.createbintree (Bt.root); System.out.println (Bt.size ()+ "---size"); System.out.println (Bt.height ()+ "---height"); System.out.println ("************** Pre-order traversal ************");        Bt.perorder (Bt.root); System.out.println ("************ Mirror symmetry * * pre-sequence traversal ************");        Bt.mirror (Bt.root); Bt.perorder (bt.root);//System.out.println ("************** in sequence traversal ************");//Bt.inorder (bt.root);//        //System.out.println ("************** post-Traversal ************");//Bt.postorder (bt.root);    }    }

Java Two fork tree implementation code

Related Article

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.