Tree height, depth, number of layers, and three traversal modes

Source: Internet
Author: User

Height of the tree:

When there is only one root node, the height is 0.

Height of the calculation tree
int Depth (node node)
{
if (node = = NULL)
return-1;
int l = depth (node->left);
int r = Depth (node->right);

Return (L < R)? (r+1):(l+1);//When there is only one root node, the height is -1+1=0
}

Number of layers:

The height of the tree is at the bottom of the 1th layer (some books are defined as the No. 0 layer), and then up

Depth of tree:

A complete binary tree is a two-fork tree: Except for the last layer, the number of nodes on each layer reaches the maximum, and only a few nodes on the right are missing on the last layer.

The depth of a complete binary tree with n nodes is [log2n]+1

Example: A complete binary tree has 64 nodes with a depth of [log2 (2^6)]+1=7

Summary: The depth of the tree is related to the number of nodes, the depth of the tree is log at 2, the number of nodes n is the top, and then the results are added to 1 value.

Number of layers:

The depth of the tree is at the top of the 1th layer (some books are defined as the No. 0 layer), and in turn

Summarize:

From the master's examination outline 38 pages: The depth of the tree is from the root node (its depth is 1) from top to bottom by layer, and the height is from the leaf node (its height is 1) from bottom to top up. Although the depth and height of the tree, but specific to a tree node, its depth and height are not the same.

My understanding is that the depth of non-root non-leaf nodes is from the root node number to it, the height is from the number of leaf nodes to it.

Layer I has 2^ (i-1) nodes

The number of root layer is 0, the number of layers and depth of the tree, the height is the number of layers +1. The number of layers is the path length of the node to the root node.

If there are 3 layers, then the number of the last layer of nodes is 2 of 2 is 4, then the total layer is 2 3 times minus 1, get 7

------------------below are three ways to traverse----------------------------------------------------

First-order Traversal:

The first sequence traversal is the root node is the first to traverse, and then traverse the left node, and then traverse the right node, and the left side of the non-recursive traversal, will not traverse the right node

A->b->c->d->e->g->f

Middle Sequence Traversal:

The middle sequence traversal is the second traversal of the root node, that is, traversing the left node first, then traversing the middle root node, and then traversing the right node.

C->b->e->g->d->f->a

Post-post traversal:

The post-sequential traversal is the root node's last traversal, that is, traversing the left node, then traversing the right node, and finally traversing the root node.

C->g->e->f->d->b->a

Hierarchical traversal:

A hierarchical traversal is a layer of traversal
A->b->c->d->e->f->g

Tree height, depth, number of layers, and three traversal modes

Related Article

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.