To find the depth of the binary tree and to determine whether it is a balanced binary tree

Source: Internet
Author: User
Tags diff
To find the two-fork tree depth:

Recursive method: From the root node in order to traverse the binary tree, return to the Saozi right subtree of the larger depth, plus 1 is the root node depth. C + + code implementation:

typedef struct treenode{
    int data;
    struct treenode* leftchild;
    struct treenode* rightchild;

} TreeNode, *bitree;

int treemaxdepth (Bitree proot) {

    if (proot = =  NULL) {return
        0;
    }

    int left = treemaxdepth (proot->leftchild);
    int right = Treemaxdepth (proot->rightchild);

    Return to left > right? left+1:right+1;

}
width of two fork tree:

To solve the width of the binary tree using the sequence traversal of the binary tree, the nodes of each layer are saved in the queue, and the width of the current layer is recorded with Curwidth, nextwidth the width of the next layer. Each time the current layer of the node out of the team, curwidth–, its children join the queue, nextwidth++, with width to save the largest width. C + + code is as follows:

typedef struct treenode{
    int data;
    struct treenode* leftchild;
    struct treenode* rightchild;

} TreeNode, *bitree;

int Widthofbitree (Bitree proot) {
    if (proot = = NULL) {return
        0;
    }

    int curwidth = 1;
    int nextwidth = 0;
    int width = 1;
    queue<bitree> node;

    Node.push (proot);

    while (!node.empty ()) {while
        (curwidth!=0) {
            bitree tmp = Node.front ();
            Node.pop ();
            if (tmp->leftchild!= NULL) {
                nextwidth++;
            }
            if (tmp->rightchild!= NULL) {
                nextwidth++;
            }
            curwidth--;
        }
        if (nextwidth>width) {
            width = nextwidth;
        }
        Curwidth = Nextwidth;
        nextwidth = 0;
    }
    return width;
}
Determine whether it is a balanced binary tree:

Balanced binary tree: The depth difference between the left and right subtree of each node is less than 1. The two-fork tree depth algorithm is used to determine whether the left and right subtree of each node satisfies the condition of the balanced binary tree. algorithm One: C + + code implementation

BOOL Isbalance (Bitree proot) {
    if (proot = =  NULL) {return
        true;
    }

    int left = treemaxdepth (proot->leftchild);
    int right = Treemaxdepth (proot->rightchild);

    int diff = left-right;
    if (diff <-1 | | | diff > 1) {return
        false;
    }

    Return Isbalance (Proot->leftchild) && isbalance (proot->rightchild);
}

But the appeal algorithm is not efficient enough to iterate through the nodes repeatedly. According to the thought of traversing the binary tree, we can design a more efficient algorithm, each traversal to a node to record the depth of the current node, so there is no need to repeat the traversal, only need linear time to determine whether the depth of the left and right subtree to meet the balance of the binary tree conditions. algorithm Two: C + + code implementation:

bool isbalance_fast (Bitree proot, int& pdepth) {if (Proot = NULL) {pdepth = 0;
    return true;
    int left, right; if (Isbalance_fast (Proot->leftchild, left) && isbalance_fast (Proot->rightchild, right) {int diff = l
        Eft-right;
            if (diff >=-1 && diff <=1) {pdepth = left > right left + 1:right + 1;
        return true;
return false; }

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.