Current location - Loan Platform Complete Network - Big data management - DEP big data platform
DEP big data platform
Height of the tree: for a non-empty binary tree, its depth is equal to the maximum depth of the left subtree plus 1.

int Depth(BinTree * T){ int dep 1,dep2

If(T==Null) returns (0);

Else {dep 1 = depth (t->; l child);

Dep2 = depth (t->; rchild);

if(dep 1 & gt; Dep2) returns (dep1+1);

Else returns (dep 2+1); }

Width of the tree: traverse the binary tree layer by layer, adopt a queue Q, let the root node enter the queue, and finally leave the queue. If there are left and right subtrees, the root nodes of the left and right subtrees enter the queue, and so on until the queue is empty.

int Width(BinTree * T){ int front =- 1,rear =- 1;

/* queue initialization */intflag = 0, count = 0, p;

/* pint CountNode (BTNode *t)?

//Total number of nodes {int numif (t = = null) num = 0;

else num = 1+count node(t-& gt; lch)+count node(t-& gt; rch);

Return (number); }void CountLeaf (BTNode *t)?

//Total number of leaf nodes {if (t! = NULL){ if(t-& gt; lch = = NULL & amp& ampt->; rch = = NULL)count++; ?

//global variable countleaf (t->; lch); count leaf(t-& gt; rch); }}。

Extended data

Method:

The algorithm for finding the height of binary tree is based on three kinds of traversal of binary tree, and the current height and the height of the highest known leaf can be recorded through the post-traversal algorithm. When leaves higher than the known height are found, the highest height is refreshed.

The last traversal is the height of the tree. On the algorithm of post-traversal, there is an introduction and reference code in a book about data structure or algorithm.