Binary tree operation (Java language) __java

Source: Internet
Author: User
Basic Concepts Tree

In addition to the root node, each node has only one parent node, and the root node does not have a parent node. In addition to leaf nodes, all nodes have one or more nodes, and leaf nodes do not have child nodes. The parent node and child nodes are connected by a pointer. Two fork Tree

The so-called binary tree is a special structure of the tree, in the binary tree each node can have a maximum of two child nodes.



A binary tree has the following properties: There are at most 2^ (n-1) elements on the nth layer of the non-empty binary tree. A two-forked tree with a depth of H has at most 2^h-1 nodes. data structure Definition

public class Btnode {
    int val;//value
    Btnode left;    Zoozi tree
    Btnode right;   Right subtree public
    Btnode () {

    } public
    btnode (int val) {
        this.val = val;
    }
    Public Btnode (int Val, Btnode left, Btnode right) {
        this.val = val;
        This.left = left;
        This.right = right;
    }




two fork tree Operation Traversal

Iterates through all nodes of the tree and accesses only once. According to the location of the root node is divided into the first sequence traversal, the middle sequence traversal, sequential traversal. Pre-sequence traversal: First access to the root node, then access to Zoozi node, the last access to the right node in the sequence traversal: first access to the Zoozi node, and then access the root node, the last access to the right child node after the following traversal: first access to the Zoozi node, and then access to the right child node, the last access to root node

The binary tree as shown in the following figure



Pre-sequence Traversal: 10 6 4 2 8 14 12 16
In-sequence traversal: 2 4 6 8 10 12 14 16
Subsequent traversal: 2 4 8 6 code to implement the pre-sequence traversal

/**
     * Pre-sequence traversal
     * First access to the root node, and then access the Zoozi node, the last access to the right child node
     * @param root
     /public
    void preorder (Btnode root) {
        /. First access to root node
        System.out.print (root.val+ "");
        if (root.left!=null) {
            preorder (root.left);
        }
        if (root.right!=null) {
            preorder (root.right);
        }
    }
in-sequence traversal
/**
     * Sequence Traversal
     * First access to the Zoozi node, then access the root node, the last access to the right child node
     * @param root
     /public
    void Middleorder (Btnode root) { c22/>//first access to the Zoozi node
        if (root.left!=null) {
            middleorder (root.left);
        }
        System.out.print (root.val+ "");
        if (root.right!=null) {
            middleorder (root.right);
        }
    }
Subsequent traversal
/**
     * Subsequent traversal
     * First access to Zoozi node, then access to the right child node, the last access to the root node
     * @param root
     /public
    void Lastorder (Btnode root) {

        if ( Root.left!=null) {
            lastorder (root.left);
        }
        if (root.right!=null) {
            lastorder (root.right);
        }
        System.out.print (root.val+ "");
    }
Hierarchy Traversal
/**
     * Level traversal
     * @param root
    /public void Layerorder (Btnode root) {
        if (root==null) {
            return;
        }
        Queue, advanced first out
        queue<btnode> queue = new linkedlist<> ();
        Queue.add (root);
        while (!queue.isempty ()) {

            Btnode node = Queue.poll ();

            System.out.print (node.val+ "");

            if (node.left!=null) {
                queue.add (node.left);
            }
            if (node.right!=null) {
                queue.add (node.right);
            }
        }
    }
get the height of the binary tree
/**
     * Get the height of the binary tree
     * @param root
     * @return
     /public
    int getheight (Btnode root) {
        if (root==null) { return
            0;
        }
        int left = getheight (root.left);  
        int right = GetHeight (root.right);

        int h = (left>right?left:right) +1;

        return h;  
    }
get the number of binary tree nodes
/**
     * Gets the number of binary tree nodes
     * @param root
     * @return
     /public
    int GetCount (Btnode root) {
        if (root== NULL) {return
            0;
        }
        return GetCount (Root.left) +getcount (root.right) +1;
    }
To determine whether two binary trees are equal
/**
     * Determine if two binary trees are equal
     * @param root1
     * @param root2
     * @return/Public
    Boolean equals (Btnode root1 , Btnode Root2) {
        if (root1==null && root2==null) | | Root1.val = = root2.val) {return
            true;
        }

        if (Equals (Root1.left, root2.left) && equals (Root1.right, root2.right)) {return
            true;
        }

        return false;
    }
Test Code
        The two-forked tree btnode node8 = new Btnode (2) in the above structure;
        Btnode node4 = new Btnode (4, node8, NULL);

        Btnode node5 = new Btnode (8);

        Btnode Node2 = new Btnode (6, Node4, NODE5);
        Btnode node6 = new Btnode (12);

        Btnode Node7 = new Btnode (16);

        Btnode node3 = new Btnode (n, Node6, node7);

        Btnode root = new Btnode (ten, Node2, NODE3);
        System.out.println ("Pre-sequence Traversal:");

        Preorder (root);

        System.out.println ("--------------");
        SYSTEM.OUT.PRINTLN ("Sequence Traversal:");

        Middleorder (root);

        System.out.println ("--------------");
        SYSTEM.OUT.PRINTLN ("Subsequent traversal:");

        Lastorder (root);

        System.out.println ("--------------");
        SYSTEM.OUT.PRINTLN ("Hierarchy traversal:");

        Layerorder (root);
        System.out.println ("--------------");

        System.out.println ("Number of nodes:" +getcount (Root));
        System.out.println ("--------------"); System.out.println ("Height:" +getheight (Root));

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.