Java implementation Two fork tree and Traverse, delete

Source: Internet
Author: User

Java implementation Two fork tree and Traverse, delete

Personal website: multi-cat movie "Can see the VIP video of the major websites" www.duomao.xyz

Two fork tree is a recursive definition, its nodes have left and right sub-tree points, logically, there are five basic forms of the two-fork Tree: (1) empty binary tree-(a); (2) A two-fork tree with only one root node-(b); (3) Only the left Dial hand tree-(c); (4) Only the right subtree-(d); (5) A fully binary tree- (e). Note: Although there are many similarities between the binary tree and the tree, the binary tree is not a special case of the tree. [1] Type (1) complete binary tree-if set two fork tree height is H, in addition to the H layer, the other layers (1~h-1) of the nodes are reached the maximum number, the H layer has leaf nodes, and leaf nodes are from left to right in order to arrange, this is a complete binary tree. (2) Full two fork tree--except the leaf node each node has left and right cotyledons and leaf nodes are at the bottom of the two fork tree. (3) Balanced binary tree-balanced binary tree is also known as the AVL tree (different from the AVL algorithm), it is a binary sorting tree, and has the following properties: It is an empty tree or its left and right two sub-tree height difference of the absolute value of not more than 1, and about two sub-trees are a balanced binary tree.
The code comment is more detailed, it is no longer necessary to explain.
 Packagecom.niu.test;/*** Created by Administrator on 2017/9/26.*/ Public classAlvintree {PrivateNode Root; //1. Create a two-tree node (three elements: node data, left node, right node)    Private classNode {//sequences        Private intkey; //data stored in the node        PrivateObject data; //left and right sub-tree        PrivateNode Leftnode; PrivateNode Rightnode; Private Booleanisvisted =false; //method of constructing empty         PublicNode () {} PublicNode (intkey, Object data) {             This. data =data;  This. Key =key;  This. Leftnode =NULL;  This. Rightnode =NULL; }    }    //2. Create a root node (constructor)     PublicAlvintree () {root=NewNode (1, "A"); }    //3, create a tree     Public voidcreattree (node root) {node NodeB=NewNode (2, "B"); Node NodeC=NewNode (3, "C"); Node noded=NewNode (4, "D"); Node NodeE=NewNode (5, "E"); Node Nodef=NewNode (6, "F"); Node nodeg=NewNode (7, "G"); Root.leftnode=NodeB; Root.rightnode=NodeC; Root.leftNode.leftNode=noded; Root.leftNode.rightNode=NodeE; Nodec.leftnode=Nodef; Nodef.rightnode=nodeg; /*structure diagram of a B C D E F G-Tree*/    }    //4, the height of the tree (using recursive thinking)     Public intheight () {returnheight (root); }     Public intHeight (node node) {//Empty tree height is 0        if(node = =NULL) {            return0; } Else {            //the height of the left and right sub-trees            intL =height (node.leftnode); intR =height (node.rightnode); //the ternary operator, which side of the tree is high ?            returnL < r? (r + 1): (L + 1); }    }    //5. Number of nodes     Public intsize () {returnsize (root); }     Public intSize (node node) {if(node = =NULL) {            return0; } Else {            //using recursive thinking, as long as there is a child node added 1            return1 + size (node.leftnode) +size (Node.rightnode); }    }    //6. Find the parent node (if the a node is a child of the B node, B is the parent of a)     PublicNode Parent (node node) {return(node = =NULL|| Root = = node)?NULL: Parent (root, node); }    /*from the top down, the first left subtree after the right subtree to look down from the root node to find the parent node up: parent node, index to find the node if you find a parent, return to the parent node, otherwise return null .*/     PublicNode Parent (node up, node index) {//the usual, first judge whether it is empty        if(UP = =NULL) {            return NULL; }        //returns up if the parent node up is a parent        if(Up.leftnode = = Index | | up.rightnode = =index) {            returnUp ; }        //Otherwise, find the left Dial hand node first, then the right child nodeNode Parent; if(Parent = parent (Up.leftnode, index))! =NULL) {            returnparent; } Else {            returnparent (up.rightnode, index); }    }    //7. Return to left child     Publicnode Getleftchild (node node) {return(Node.leftnode! =NULL) ? Node.leftnode:NULL; }    //8. Return to right child     Publicnode Getrightchild (node node) {return(Node.rightnode! =NULL) ? Node.rightnode:NULL; }    //9. Return to the root node     PublicNode Getroot () {returnRoot; }    //10, delete a node (delete node needs to ensure that the sub-nodes under it all deleted     Public voidDelete (node node) {if(Node! =NULL) {Delete (Node.leftnode);            Delete (Node.rightnode); Node=NULL; }    }     Public voidVisit (node node) {node.isvisted=true; System.out.println ("Node" +node.key+ "message:" +node.data); }    //11. Pre-sequence traversal: root node, left node, right node.     Public voidPreorder (node node) {if(node!=NULL) {Visit (node);            Preorder (Node.leftnode);        Preorder (Node.rightnode); }    }    //12, Middle sequence traversal: Zogen right     Public voidinorder (node node) {if(node!=NULL) {inorder (Node.leftnode);            Visit (node);        Inorder (Node.rightnode); }    }    //13. Subsequent traversal     Public voidPostorder (node node) {if(node!=NULL) {postorder (Node.leftnode);            Postorder (Node.rightnode);        Visit (node); }    }     Public Static voidMain (string[] args) {Alvintree alvintree=NewAlvintree ();        Alvintree.creattree (Alvintree.root); System.out.println ("Size:" +alvintree.size ()); System.out.println ("Height:" +alvintree.height ()); }}

Java implementation Two fork tree and Traverse, delete

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.