Java implements chained storage for two-fork trees

Source: Internet
Author: User

Java implements chained storage for two-fork trees

 PackageCOM.FXR. Two chain-tree storage;ImportJava.util.Scanner; Public classTree {Static Final intMaxLen = 20; StaticScanner input =NewScanner (system.in);        Cbttype Inittree () {cbttype node; if(node =NewCbttype ())! =NULL) {System.out.println ("Please enter data for a root node:"); Node.data=Input.next (); Node.left=NULL; Node.right=NULL; if(Node! =NULL)            {                returnnode; }Else{                return NULL; }        }        return NULL; }    //Adding nodes    voidAddtreenode (Cbttype TreeNode)//implementation of adding nodes{Cbttype pnode,parent;        String data; intMenusel; if((Pnode =NewCbttype ())! =NULL) {System.out.println ("Input binary tree node data"); Pnode.data=Input.next (); Pnode.left=NULL; Pnode.right=NULL; System.out.println ("Enter data for the node's parent node:"); Data=Input.next (); Parent= Treefindnode (Treenode,data);//find the node that made the data            if(Parent = =NULL)//If the parent node is not found{System.out.println ("Parent node not found"); Pnode=NULL; return; } System.out.println ("1. Add the node to the left subtree--2. Add a node to the right sub-tree");  Do{                //Enter the selectionMenusel =Input.nextint (); if(Menusel = = 1 | | menusel = = 2)                {                    if(Parent = =NULL) {System.out.println ("The parent node does not exist, please set the parent node first"); }Else{                        Switch(Menusel) { Case1://add to left node                            if(Parent.left! =NULL) {System.out.println ("Left subtree node is not empty"); }Else{parent.left=Pnode; }                             Break;  Case2://Add to right node                            if(Parent.right! =NULL) {System.out.println ("Right subtree node is not empty"); }Else{parent.right=Pnode; }                             Break; default: System.out.println ("Invalid Parameter");                                                            }                                                                    }                } } while(Menusel! = 1 && Menusel! = 2); }    }    Privatecbttype Treefindnode (cbttype treeNode, String data) {//finding the implementation of a nodeCbttype ptr; if(TreeNode = =NULL)        {            return NULL; }Else{            if(treeNode.data.equals (data)) {returnTreeNode; }Else{                //Recursive lookup to left and right subtrees, respectively                if(ptr = Treefindnode (treenode.left,data))! =NULL )                {                    returnptr; }Else if(ptr = Treefindnode (treenode.right,data))! =NULL)                {                    returnptr; }Else{                    return NULL; }            }        }            }    //Get left subtreeCbttype Treeleftnode (Cbttype treeNode)//Get left subtree    {        if(TreeNode! =NULL)        {            returnTreenode.left; }Else{            return NULL; }    }    //Get right subtreeCbttype Treerightnode (Cbttype treeNode)//Get left subtree    {        if(TreeNode! =NULL)        {            returnTreenode.right; }Else{            return NULL; }    }        //Determine if the tree is empty    intTreeisempty (Cbttype TreeNode)//Judging empty trees    {        if(TreeNode! =NULL)        {            return0; }Else{            return1; }    }            //calculate the depth of a binary tree    inttreedepth (Cbttype treeNode) {intDeptleft,deptright; if(TreeNode = =NULL)        {            return0; }Else{deptleft= Treedepth (Treenode.left);//left dial hand tree depth (recursive invocation)Deptright =treedepth (treenode.right);//Right subtree depth (recursive invocation)                    if(Deptleft >deptright) {                returnDeptleft+1; }Else{                returnDeptright+1; }                                        }    }    //Empty the binary tree    voidCleartree (Cbttype treeNode) {if(TreeNode! =NULL) {cleartree (treenode.left);//Empty left subtreeCleartree (Treenode.right);//empty Right sub-treeTreeNode =NULL;//frees the memory occupied by the current node        }    }    //displaying data for a node    voidtreenodedata (Cbttype p) {System.out.printf ('%s ', P.data); }    //Traverse by Layer    voidLeveltree (Cbttype treeNode) {Cbttype p; Cbttype[]q=NewCbttype[maxlen];//define a sequential stack        intHead = 0,tail = 0; if(TreeNode! =NULL)//if the reference to the first team is not empty{Tail= (tail+1)%maxlen;//Calculate loop Queue tail numberQ[tail] = TreeNode;//referencing a two-fork tree root into a queue                    }                //queue is not empty for looping         while(Head! =tail) {Head= (head+1)%MaxLen; P=Q[head];            Treenodedata (P); if(P.left! =NULL) {Tail= (tail+1)%MaxLen; Q[tail]=P.left; }                        if(P.right! =NULL) {Tail= (tail+1)%MaxLen; Q[tail]=P.right; }                    }                            }    //First Order Traversal    voidDlrtree (Cbttype treeNode) {if(TreeNode! =NULL) {treenodedata (TreeNode);            Dlrtree (Treenode.left);                                Dlrtree (Treenode.right); }    }        //Middle Sequence Traversal        voidLdrtree (Cbttype treeNode) {if(TreeNode! =NULL) {dlrtree (treenode.left);                                Treenodedata (TreeNode);                                            Dlrtree (Treenode.right); }        }            //Post-post traversal                voidLrdtree (Cbttype treeNode) {if(TreeNode! =NULL) {dlrtree (treenode.left);                                                Dlrtree (Treenode.right);                                                                                                                    Treenodedata (TreeNode); }                }        }

Java implements chained storage for two-fork trees

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.