简体   繁体   中英

A count function that counts the leaf nodes of a height balanced tree

I'm writing a function that counts the leaf nodes of a height balanced tree using struct and pointers. The function takes 3 arguments: the tree, pointer to an array and the maximum depth of the tree. The length of the array is the maximum depth. When function is called the array is initialized to zero. The function recursively follows the tree structure, keeping track of the depth, and increments the right counter whenever it reaches a leaf. The function does not follow any pointer deeper than maxdepth. The function returns 0 if there was no leaf at depth greater than maxdepth, and 1 if there was some pointer togreater depth. What is wrong with my code. Thanks.

typedef int object;
typedef int key;
typedef struct tree_struct { key key;
   struct tree_struct *left;
   struct tree_struct *right;
   int           height; 
} tree_n;


int count_d (tree_n *tr, int *count, int mdepth)
{
   tree_n *tmp;
   int i;
   if (*(count + 0) == NULL){
      for (i =0; i<mdepth; i++){
         *(count + i) = 0;
      }
   }

   while (medepth != 0)
   {
      if (tr == NULL)  return;
      else if ( tree-> left == NULL || tree->right == NULL){
         return (0);
      }

      else {
         tmp = tr;
         *(count + 0) = 1;
         int c = 1;
         while(tmp->left != NULL && tmp->right != NULL){
            if(tmp-> left){
               *(count + c) = 2*c;
               tmp = tmp->left;
               return count_d(tmp, count , mdepth);
            }
            else if(tmp->right){
               *(count + c + 1) = 2*c + 1;
               tmp = tmp->right;
               return count_d(tmp,count, mdepth);

            }
            c++;
            mpth--;
         }
      }
   }

What is wrong with my code

One thing I noticed is that you are missing return in the recursive calls.

return count_d(tmp, count , mdepth);
// ^^^ Missing 

There are two such calls. Make sure to add return to both of them.

Disclaimer: Fixing this may not fix all your problems.

Correct Function To Insert,Count All Nodes and Count Leaf Nodes

#pragma once
typedef int itemtype;
#include<iostream>
typedef int itemtype;
#include<iostream>
#include<conio.h>
#include<string>

using namespace std;

class Node
{
public:
    Node* left;
    Node* right;
    itemtype data;
};

class BT
{
private:
    int count = 0;
    Node* root;
    void insert(itemtype d, Node* temp);//Override Function
public:
    BT();//Constructor
    bool isEmpty();
    Node* newNode(itemtype d);
    Node* getroot();
    void insert(itemtype d);//Function to call in main
    int countLeafNodes(Node * temp);
    int countAllNodes();//to count all nodes
}
BT::BT()//constructor
{
    root = NULL;
}
bool BT::isEmpty()
{
    if (root == NULL)
        return true;
    else
        return false;
}
Node* BT::newNode(itemtype d)
{
    Node* n = new Node;
    n->left = NULL;
    n->data = d;
    n->right = NULL;
    return n;
}
void BT::insert(itemtype d)//Function to call in main
{
    if (isEmpty())
    {
        Node* temp = newNode(d);
        root = temp;
    }
    else
    {
        Node* temp = root;
        insert(d, temp);
    }
    count++;//to count number of inserted nodes
}
void BT::insert(itemtype d, Node* temp)//Private Function which is overrided
{
    if (d <= temp->data)
    {
        if (temp->left == NULL)
        {
            Node* n = newNode(d);
            temp->left = n;
        }
        else
        {
            temp = temp->left;
            insert(d, temp);
        }
    }
    else
    {
        if (temp->right == NULL)
        {
            temp->right = newNode(d);
        }
        else
        {
            temp = temp->right;
            insert(d, temp);
        }
    }

}
int BT::countAllNodes()
{ return count; }
int BT::countLeafNodes(Node* temp)
{
    int leaf = 0;
    if (temp == NULL)
        return leaf;
    if (temp->left == NULL && temp->right == NULL)
        return ++leaf;
    else
    {
        leaf = countLeafNodes(temp->left) + countLeafNodes(temp->right);
        return leaf;
    }
}
void main()
{
    BT t;
    t.insert(7);
    t.insert(2);
    t.insert(3);
    t.insert(15);
    t.insert(11);
    t.insert(17);
    t.insert(18);
    cout<<"Total Number Of Nodes:" <<t.countAllNodes() <<endl;
    cout << "Leaf Nodes:" << t.countLeafNodes(t.getroot()) << endl;
    _getch();
}

Output:

Ouput

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM