Java Implementation Two tree structure basic Operation Detail Code

Source: Internet
Author: User

Static Final intMaxLen = 20;//Maximum lengthclassCbttype//Defining a binary tree node type{String data; //element DataCbttype left;//Left Dial hand tree node referenceCbttype right;//Right Child tree node reference}cbttype inittree ()//Initialize binary tree{cbttype node; if((node=NewCbttype ())! =NULL){//Request MemorySystem.out.println ("Please enter root node data"); Node.data=Input.next (); Node.left=NULL; Node,right=NULL; if(node!=NULL)        {            returnnode; }        Else        {            return NULL; }    }    return NULL;}voidAddtreenode (Cbttype TreeNode)//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 parent node of this node"); Data=Input.next (); Parent=Treefindnode (Treenode,data); if(parent==NULL) {System.out.prinln ("Parent node not found"); return; } System.out.println (1. Insert to parent node left subtree 2. Insert to parent node right subtree ");  Do{Menusel=Input.neitint (); if(menusel==1| | menusel==2)            {                if(parent==NULL) {System.out.prinln ("Parent node does not exist, please set parent node First"); }                Else                {                    Switch(Menusel) { Case1:                        if(parent.left!=NULL) {System.out.prinln ("Left subtree node is not empty"); }                        Else{parent.left=Pnode; }                         Break;  Case2:                        if(parent.right!=NULL) {System.out.prinln ("Right subtree node is not empty"); }                        Else{parent.right=Pnode; }                         Break; default: System.out.println ("Invalid Parameter"); }            }        } while(menusel!=1&&menusel!=2); }}cbttype Treefindnode (cbttype treenode,string data)//Find Nodes{cbttype ptr; if(treenode==NULL)    {        return NULL; }    Else    {        if(treeNode.data.equals (data)) {returnTreeNode}Else        {            if((Ptr=treefindnode (treenode.left,data))!==NULL)//Recursive left subtree            {                returnptr; }            Else if((Ptr=treefindnode (treenode.right))!==NULL)//Recursive right sub-tree            {                returnptr; }            Else            {                return NULL; }}}}cbttype Treeleftnode (Cbttype treeNode)//Get left subtree{    if(treenode!=NULL)    {        returnTreenode.left; }    Else    {        return NULL; }}cbttype Treerightnode (cbttype treeNode)//Get right subtree{    if(treenode!=NULL)    {        returnTreenode.right; }    Else    {        return NULL; }}intTreeisempty (Cbttype TreeNode)//Judging empty trees{    if(treenode==NULL)    {        return1; }    Else    {        return0; }}intTreedepth (Cbttype TreeNode)//Calculate binary Tree Depth{    intDepleft,depright; if(treenode==NULL)    {        return0; }    Else{depleft=treedepth (Treenode.left);//left dial hand tree depth recursionDepright=treedepth (Treenode.right);//Right sub-tree depth recursion        if(depleft>depright) {            returnDepleft+1;}Else        {            returnDepright+1; }    }}voidCleartree (Cbttype TreeNode)//Empty the binary tree{    if(treenode!=NULL) {cleartree (treenode.left);        Cleartree (Treenode.right); TreeNode=NULL; }}voidTreenodedata (Cbttype p)//Show current node data{SYSTEM.OUT.PRINLN ('%s ', P.data);}voidLeveltree (Cbttype TreeNode)//Hierarchical Traversal{Cbttype p; cbttype[] Q=NewCbttype[maxlen];//Defining Queues    inthead= 0; intTail= 0; if(treenode!=NULL) {Tail= (tail+1)%maxlen;//Calculate circular queue trailing sequence numberQ[tail]=treenode;//Two fork roots are introduced into the queue    }     while(Head!=tail)//queue is not empty for looping{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.left; }    }}voidDlrtree (Cbttype TreeNode)//First Order Traversal{    if(treenode!=NULL) {treenodedata (TreeNode); //displaying node dataDlrtree (Treenode.left);    Dlrtree (Treenode,right); }}voidLdrtree (Cbttype TreeNode)//Middle Sequence Traversal{    if(treenode!=NULL) {ldrtree (treenode.left);        Treenodedata (TreeNode);    Ldrtree (Treenode,right); }}voidLrdtree (Cbttype TreeNode)//Post-post traversal{    if(treenode!=NULL) {lrdtree (treenode.left);        Lrdtree (Treenode.right);    Treenodedata (TreeNode); }}

Java Implementation Two tree structure basic Operation Detail Code

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.