Basic operations of binary tree for data collection (implemented in java)

Source: Internet
Author: User

Basic operations of binary tree for data collection (implemented in java)

Summarizes the basic operations of Binary Trees, including first-order traversal, middle-order traversal, recursive and non-recursive forms of post-order traversal (stack implementation), and hierarchical traversal (queue implementation.

Main Code
Import java. util. Queue; import java. util. Stack; import java. util. concurrent. Define blockingqueue;/*** simple binary tree construction ** @ author xdsjs * @ param
  
   
**/Public class BinTree
   
    
{Private BinNode
    
     
RootNode; // root node public BinTree () {} public BinTree (BinNode
     
      
RootNode) {this. rootNode = rootNode;}/*** get the height of the entire tree ** @ return */public int getHeight () {return getHeight (rootNode );} /*** obtain the height of a node using recursion ** @ param binNode * target node * @ return */public int getHeight (BinNode
      
        BinNode) {if (binNode = null) return 0; int I = getHeight (binNode. getLeftNode (); int j = getHeight (binNode. getRightNode (); return I <j? J + 1: I + 1;}/*** recursively get the size (number of knots) of the entire tree ** @ return */public int getSize () {return getSize (rootNode);}/*** get the size (number of knots) of a subtree ** @ param binNode * @ return */public int getSize (BinNode
       
         BinNode) {if (binNode = null) return 0; return 1 + getSize (binNode. getLeftNode () + getSize (binNode. getRightNode ();}/*** determines whether a binary tree is an empty tree ** @ return */public boolean isEmpty () {return rootNode = null ;} /*** get the parent node of a node under the entire tree ** @ param binNode * a node * @ return */public BinNode
        
          GetParent (BinNode
         
           BinNode) {return (binNode = null | binNode = rootNode )? Null: getParent (rootNode, binNode );} /*** recursively obtain the parent node of a node under a subTree ** @ param subTree * A subTree * @ param binNode * a node * @ return */public BinNode
          
            GetParent (BinNode
           
             SubTree, BinNode
            
              BinNode) {if (subTree = null | binNode = null | subTree = binNode | binNode = rootNode) return null; if (subTree. getLeftNode () = binNode | subTree. getRightNode () = binNode) return subTree; BinNode
             
               B; if (getParent (B = subTree. getLeftNode (), binNode )! = Null) return B; return getParent (subTree. getRightNode (), binNode);}/*** get the left child node of a node ** @ param binNode * @ return */public BinNode
              
                GetLeftBinNode (BinNode
               
                 BinNode) {return (binNode = null )? Null: binNode. getLeftNode ();}/*** get the right child node of a node ** @ param binNode * @ return */public BinNode
                
                  GetRightBinNode (BinNode
                 
                   BinNode) {return (binNode = null )? Null: binNode. getRightNode ();} public BinNode
                  
                    GetRoot () {return this. rootNode;}/*** Delete the entire tree */public void deleteTree () {deleteTree (rootNode );} /*** delete a subtree (delete both its left and right subtree) ** @ param binNode */public void deleteTree (BinNode
                   
                     BinNode) {if (binNode = null) return; deleteTree (binNode. getLeftNode (); deleteTree (binNode. getRightNode (); binNode = null;}/*** access a node ** @ param subTree */public void visted (BinNode
                    
                      SubTree) {System. out. println (-- name: + subTree. getData ();}/*** first-order traversal (recursive form) ** @ param subTree */public void preOrderTraverse (BinNode
                     
                       SubTree) {if (subTree = null) return; visted (subTree); preOrderTraverse (subTree. getLeftNode (); preOrderTraverse (subTree. getRightNode ();}/*** ordinal traversal (recursive form) ** @ param subTree */public void middleOrderTraverse (BinNode
                      
                        SubTree) {if (subTree = null) return; middleOrderTraverse (subTree. getLeftNode (); visted (subTree); middleOrderTraverse (subTree. getRightNode ();}/*** post-order traversal (recursive form) ** @ param subTree */public void postOrderTraverse (BinNode
                       
                         SubTree) {if (subTree = null) return; postOrderTraverse (subTree. getLeftNode (); postOrderTraverse (subTree. getRightNode (); visted (subTree);}/*** first-order traversal (non-recursive form, more efficient) ** @ param subTree */public void nrPreOrderTraverse (BinNode
                        
                          SubTree) {if (subTree = null) return; Stack
                         
                           > Stack = new Stack <> (); while (true) {while (subTree! = Null) {visted (subTree); stack. push (subTree); subTree = subTree. getLeftNode ();} if (stack. isEmpty () break; subTree = stack. pop (); subTree = subTree. getRightNode () ;}}/*** ordinal traversal (non-recursive form) ** @ param subTree */public void nrMiddleOrderTraverse (BinNode
                          
                            SubTree) {if (subTree = null) return; Stack
                           
                             > Stack = new Stack <> (); while (true) {while (subTree! = Null) {stack. push (subTree); subTree = subTree. getLeftNode ();} if (stack. isEmpty () break; subTree = stack. pop (); visted (subTree); subTree = subTree. getRightNode () ;}}/*** post-order traversal (non-recursive) ** @ param subTree */public void nrPostOrderTraverse (BinNode
                            
                              SubTree) {if (subTree = null) return; Stack
                             
                               > Stack = new Stack <> (); BinNode
                              
                                LastNode = null; // indicates the last accessed node while (true) {while (subTree! = Null) {stack. push (subTree); subTree = subTree. getLeftNode ();} if (stack. isEmpty () break; subTree = stack. peek (); if (subTree. getRightNode () = null | subTree. getRightNode () = lastNode) {visted (subTree); subTree = stack. pop (); lastNode = subTree; subTree = null;} else {subTree = subTree. getRightNode () ;}}/ *** layered traversal (queue implementation) ** @ param subTree */public void levelTraverse (BinNode
                               
                                 SubTree) {if (subTree = null) return; Queue
                                
                                  > Queue = new LinkedBlockingQueue <> (); queue. add (subTree); while (! Queue. isEmpty () {BinNode
                                 
                                   BinNode = queue. poll (); if (binNode! = Null) {visted (binNode); if (binNode. getLeftNode ()! = Null) queue. add (binNode. getLeftNode (); if (binNode. getRightNode ()! = Null) queue. add (binNode. getRightNode () ;}} public static void main (String [] args) {BinTree
                                  
                                    BinTree = new BinTree <> (new BinNode
                                   
                                     (A);/*** create A binary tree **
                                    
         *           *           A          *     B          C          *  D     E            F         * 
**/BinNode RootNode = binTree. getRoot (); BinNode NodeB = new BinNode <> (B); BinNode NodeC = new BinNode <> (C); BinNode NodeD = new BinNode <> (D); BinNode NodeE = new BinNode <> (E); BinNode NodeF = new BinNode <> (F); rootNode. setLeftNode (nodeB); rootNode. setRightNode (nodeC); nodeB. setLeftNode (nodeD); nodeB. setRightNode (nodeE); nodeC. setRightNode (nodeF); System. out. println (******** first-order traversal (recursive form) *******); binTree. preOrderTraverse (rootNode); System. out. println (******** central order traversal (recursive form) *******); binTree. middleOrderTraverse (rootNode); System. out. println (******** post-order traversal (recursive form) *******); binTree. postOrderTraverse (rootNode); System. out. println (******** first-order traversal (non-recursive form) *******); binTree. nrPreOrderTraverse (rootNode); System. out. println (******** central order traversal (non-recursive form) *******); binTree. nrMiddleOrderTraverse (rootNode); System. out. println (******** post-order traversal (non-recursive form) *******); binTree. nrPostOrderTraverse (rootNode); System. out. println (******** hierarchical traversal (queue implementation) *******); binTree. levelTraverse (rootNode); System. out. println (*********** I am a split line ***********); System. out. println (the height of the binary tree is: + binTree. getHeight (); System. out. println (the size of the binary tree is: + binTree. getSize ());}}
/*** Indicates the tree node class ** @ author xdsjs ** @ param
  
   
* Node data field type */public class BinNode
   
    
{Private BinNode
    
     
LeftNode; // left node private BinNode
     
      
RightNode; // The right node private T dataValue; // data domain public BinNode () {} public BinNode (T dataValue) {super (); this. dataValue = dataValue;} public BinNode (BinNode
      
        LeftNode, BinNode
       
         RightNode, T dataValue) {this. leftNode = leftNode; this. rightNode = rightNode; this. dataValue = dataValue;} public BinNode
        
          GetLeftNode () {return leftNode;} public void setLeftNode (BinNode
         
           LeftNode) {this. leftNode = leftNode;} public BinNode
          
            GetRightNode () {return rightNode;} public void setRightNode (BinNode
           
             RightNode) {this. rightNode = rightNode;} public T getData () {return dataValue;} public void setData (T data) {this. dataValue = data ;}}
           
          
         
        
       
      
     
    
   
  

 

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.