Java data structure and algorithm---binary tree

Source: Internet
Author: User

Principle from Baidu Encyclopedia recommended data Demo URL: https://www.cs.usfca.edu/~galles/visualization/BST.html

One, what is two fork tree

Each node of a binary tree has a maximum of two subtrees trees (no nodes with a degree greater than 2), and the subtree of the binary tree has left and right points, and the order cannot be reversed. The first layer of the binary tree has a maximum of 2 (i-1) sub-nodes; The two-tree with a depth of K has a maximum of 2 K-squares and then 1 nodes (which do not knock so as to describe it in words); for any binary tree T, if its terminal node is n0 and the node number of 2 is N2, then n0=n2+1.

Two or two cross-tree classification:

1, full two fork tree: all nodes except leaf nodes have two sub-nodes.

The nature of the tree full of two forks:
1) A tree depth of H, the maximum number of layers is k, the depth is the same as the maximum number of layers, k=h;
2) The first layer of the tree, the number of leaf nodes of the layer is 2k;
3) The number of nodes in the K-layer is 2 (k-1) the second party.
4) The number of summary points is 2 of the K-square minus 1, and the total number of nodes must be odd.

2, complete binary tree: If set two fork tree depth is H, in addition to the H layer, the other layers (1~ (h-1) layer) The number of nodes reached the maximum, the H layer all the nodes are continuously concentrated on the leftmost, this is a complete binary tree.

The complete binary tree is characterized by:
1) Only allow the last layer to have the vacancy node and vacancy on the right, that is, the leaf node can only appear on the two layers with the highest level;
2) for any node, if the right subtree has a depth of J, the depth of its left subtree must be J or j+1. A point of 1 is only 1 or 0.

Full two fork tree must be completely binary tree, complete binary tree is not necessarily full of two fork tree.

Implementation of three or two-fork tree in data structure

Binary tree is implemented and used in the general data structure according to binary sort tree. Binary sort tree: Also known as the binary search tree, also known as the binary searching tree.

The binary sort tree is either an empty tree or a two-fork tree with the following properties:
(1) The Joz tree is not empty, then the value of all nodes on the left subtree is less than or equal to the value of its root node;
(2) If the right subtree is not empty, the value of all nodes on the right subtree is greater than or equal to the value of its root node;
(3) The left and right sub-trees are also two-fork sorting trees respectively;

1. Data structure of binary sort tree node

Private Static classNode<e>{        Privatee e;//data for the current node        PrivateNode<e> Leftnode;//current node left child node        PrivateNode<e> Rightnode;//right child node of current node         PublicNode (e E, node<e> Leftnode, node<e>Rightnode) {            Super();  This. E =e;  This. Leftnode =Leftnode;  This. Rightnode =Rightnode; }        }

2. Inserting nodes

If it is an empty tree (no nodes exist), it is inserted directly.
If it is not an empty tree, it finds the node from the root node, which is the parent node of the new node, and when the parent node finds it, determines whether the new node is on the left node or on the right node, depending on the value of the new node.

     Public voidInsert (e e) {Node<E> node=NewNode<e> (E,NULL,NULL); if(root==NULL) {root=node; }Else{Node<E> fnode=Root; Node<E> Parentnode=root;//the parent node to find             while(true) {parentnode=FNode; if(Comparetoe (E,FNODE.E)) {FNode=Fnode.leftnode; if(fnode==NULL) {Parentnode.leftnode=node;  Break; }                }Else{FNode=Fnode.rightnode; if(fnode==NULL) {Parentnode.rightnode=node;  Break; } }}} Size++; }    //just a numerical comparison is achieved.    Private BooleanComparetoe (E a,e b) {Integer A1=(Integer) A; Integer B1=(Integer) b; returna1<B1; }

3. Find nodes

Search from the root node, if the node value to find is smaller than the parent node value, then check the left child node, otherwise check the right child node, until the discovery, if not present, return null

     PublicNode<e>Find (e e) {if(root.e==e) {returnRoot; } Node<E> fnode=Root;  while(true) {            if(Comparetoe (E,FNODE.E)) {FNode=Fnode.leftnode; }Else {                if(Fnode.e.equals (e)) {returnFNode; } FNode=Fnode.rightnode; }            if(fnode==NULL) {                return NULL; }        }    }

4, Binary tree traversal mode

A, the first order traversal traversal rule Access node, access the node's left subtree, access the node's right subtree->20->24 (each node is the rule)

 Public void pretraversaltree (node<e> Node) {        if(node!=null) {            Node.display ();            Pretraversaltree (Node.leftnode);            Pretraversaltree (Node.rightnode);        }    }

Results: e:23 e:20 e:19 e:21 e:22 e:24 e:23 e:25 e:30

B, Middle sequence traversal: Traversal rules first traverse the left subtree, then the node, and finally traverse the node right subtree->23->24 (each node is the rule)

 Public void centraversaltree (node<e> Node) {        if(node!=null) {            Centraversaltree (Node.leftnode);            Node.display ();            Centraversaltree (Node.rightnode);        }    }

Results: e:19 e:20 e:21 e:22 e:23 e:23 e:24 e:25 e:30

C, the subsequent traversal traversal rules first traverse the left subtree, will then traverse the node right subtree, the last node,->24->23 (each node is the rule)

 Public void afttraversaltree (node<e> Node) {        if(node!=null) {            Afttraversaltree (Node.leftnode);            Afttraversaltree (Node.rightnode);            Node.display ();                    }    }

Results: e:19 e:22 e:21 e:20 e:23 e:30 e:25 e:24 e:23

Full code

 PackageCom.jalja.org.algorithm; Public classMytree<e> {    PrivateNode<e> Root;//root node    Private intsize=0;//number of nodes in the tree     Publicmytree () {}Private Static classNode<e>{        Privatee e;//data for the current node        PrivateNode<e> Leftnode;//current node left child node        PrivateNode<e> Rightnode;//right child node of current node         PublicNode (e E, node<e> Leftnode, node<e>Rightnode) {            Super();  This. E =e;  This. Leftnode =Leftnode;  This. Rightnode =Rightnode; }         Public voiddisplay () {System.out.print ("E:" +e); }            }    //if it is an empty tree (no nodes exist), it is inserted directly. //if it is not an empty tree, it finds the node from the root node, which is the parent node of the new node, and when the parent node finds it, determines whether the new node is on the left node or on the right node, depending on the value of the new node.      Public voidInsert (e e) {Node<E> node=NewNode<e> (E,NULL,NULL); if(root==NULL) {root=node; }Else{Node<E> fnode=Root; Node<E> Parentnode=root;//the parent node to find             while(true) {parentnode=FNode; if(Comparetoe (E,FNODE.E)) {FNode=Fnode.leftnode; if(fnode==NULL) {Parentnode.leftnode=node;  Break; }                }Else{FNode=Fnode.rightnode; if(fnode==NULL) {Parentnode.rightnode=node;  Break; } }}} Size++; }    //just a numerical comparison is achieved.    Private BooleanComparetoe (E a,e b) {Integer A1=(Integer) A; Integer B1=(Integer) b; returna1<B1; }    //Search from the root node, if the node value to find is smaller than the parent node value, then check the left child node, otherwise check the right child node, until the discovery, if not present, return null     PublicNode<e>Find (e e) {if(root.e==e) {returnRoot; } Node<E> fnode=Root;  while(true) {            if(Comparetoe (E,FNODE.E)) {FNode=Fnode.leftnode; }Else {                if(Fnode.e.equals (e)) {returnFNode; } FNode=Fnode.rightnode; }            if(fnode==NULL) {                return NULL; }        }    }     Public voidPretraversaltree (node<e>node) {        if(node!=NULL) {node.display ();            Pretraversaltree (Node.leftnode);        Pretraversaltree (Node.rightnode); }    }         Public voidCentraversaltree (node<e>node) {        if(node!=NULL) {centraversaltree (Node.leftnode);            Node.display ();        Centraversaltree (Node.rightnode); }    }         Public voidAfttraversaltree (node<e>node) {        if(node!=NULL) {afttraversaltree (Node.leftnode);            Afttraversaltree (Node.rightnode);                    Node.display (); }    }             Public Static voidMain (string[] args) {mytree<Integer> mytree=NewMytree<integer>(); Mytree.insert (23); Mytree.insert (20); Mytree.insert (24); Mytree.insert (19); Mytree.insert (21st); Mytree.insert (23); Mytree.insert (25); Mytree.insert (22); Mytree.insert (30); Mytree.afttraversaltree (Mytree.find (23)); }    }
View Code

Java data structure and algorithm---binary tree

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.