Binary tree (8)----The first binary tree K-layer node and the binary part K-leaf node layer, recursive and non-recursive

Source: Internet
Author: User

1. Binary definition

typedef struct BTREENODEELEMENT_T_ {    void *data;} btreenodeelement_t;typedef struct Btreenode_t_ {    btreenodeelement_t *m_pelemt;    struct Btreenode_t_    *m_pleft;    struct btreenode_t_    *m_pright;} btreenode_t;


2. Find the number of nodes in the K-layer of the binary tree


(1) Recursive mode:

Given root node Proot:

Suppose proot is empty, or the number of layers Kthlevel <= 0. is either an empty tree or a non-requirement. then returns 0;

Assuming that the proot is not empty and the layer kthlevel==1 at this point, then Proot is one of the K-tier nodes, then 1 is returned;

Assume that the proot is not empty. And at this time the number of layers Kthlevel > 1. Then the number of Proot Zuozi (KTHLEVEL-1) layer nodes and the Proot right subtree (KthLevel-1) layer nodes must be required at this time.


int  getbtreekthlevelnodestotal (btreenode_t *proot, int kthlevel) {    if (proot = = NULL | | Kthlevel <= 0)        return 0;    if (proot! = NULL && Kthlevel = = 1)        return 1;    Return (Getbtreekthlevelnodestotal (Proot->m_pleft, KTHLEVEL-1) + getbtreekthlevelnodestotal (Proot->m_pright, KTHLEVEL-1));}


(2) Non-recursive mode

With queue implementations:

int Getkthlevelnodestotal (btreenode_t *proot, unsigned int kthlevel) {    if (proot = = NULL)        return 0;    Queue <btreenode_t *>  que;    Que.push (proot);    int curlevelnodestotal = 0;    int curlevel = 0;       while (!que.empty ()) {        ++curlevel;//current number of layers        curlevelnodestotal = Que.size ();        if (Curlevel = = kthlevel)//Assume that the number of layers equals the given number of layers break            ;        int cntnode = 0;        while (Cntnode < curlevelnodestotal) {//The next layer node is enqueued            ++cntnode;            Proot = Que.front ();            Que.pop ();            if (proot->m_pleft! = NULL)                que.push (proot->m_pleft);            if (proot->m_pright! = NULL)                que.push (proot->m_pright);}    }        while (!que.empty ())        que.pop ();    if (Curlevel = = kthlevel)        return curlevelnodestotal;    return 0;  Suppose Kthlevel is greater than the depth of the tree}





3, find the binary tree K-Layer leaf node points

(1) Recursive method

Given node Proot:

If the proot is empty, or if the number of layers Kthlevel <= 0, the empty tree or the layer number is illegal, then 0 is returned;

Suppose Proot is not empty, and when the layer is kthlevel==1, it is necessary to infer whether it is a leaf node:

Assuming that all proot are empty, then Proot is one of the leaf nodes of the K-layer. then returns 1;

Assuming that one of the proot trees is present, the proot is not a leaf node. then returns 0;

Assuming that proot is not empty, and at this point the layer kthlevel > 1, it is necessary to return the Saozi right subtree node of the KTHLEVEL-1 layer.


int Getbtreekthlevelleafnodestotal (btreenode_t *proot, int kthlevel) {    if (proot = = NULL | | Kthlevel <= 0)        return 0;    if (proot! = null && Kthlevel = = 1) {        if (Proot->m_pleft = = NULL && Proot->m_pright = = null) 
   return 1;        else            return 0;    }    Return (Getbtreekthlevelleafnodestotal (  proot->m_pleft,  KthLevel-1) + getbtreekthlevelleafnodestotal ( Proot->m_pright, KthLevel-1));}


(2) Non-recursive mode

With queue implementation

int Getkthlevelnodestotal (btreenode_t *proot, unsigned int kthlevel) {if (Proot = = NULL) return 0;    Queue <btreenode_t *> que;    Que.push (Proot);    int curlevelnodestotal = 0;       int curlevel = 0;        while (!que.empty ()) {++curlevel;//Current number of layers Curlevelnodestotal = Que.size ();        if (Curlevel = = kthlevel)//Assume that the number of layers equals the given number of layers break;        int cntnode = 0;            while (Cntnode < curlevelnodestotal) {//The next layer node is enqueued ++cntnode;            Proot = Que.front ();            Que.pop ();            if (proot->m_pleft! = NULL) Que.push (proot->m_pleft);        if (proot->m_pright! = NULL) Que.push (proot->m_pright);        }} if (curlevel = = kthlevel) {int cntnode = 0;        int leafnodes = 0;                while (Cntnode < curlevelnodestotal) {++cntnode;                Proot = Que.front ();                               Que.pop (); if (PROOT-&GT;M_PLEFT = = NULL && Proot->m_pright = = null) leafnodes++; } return leafnodes;  Returns the number of leaf nodes} return 0; Suppose the kthlevel is greater than the depth of the tree}

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Binary tree (8)----The first binary tree K-layer node and the binary part K-leaf node layer, recursive and non-recursive

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.