使用最频繁节点的二叉搜索树打印某些语句

问题描述 投票:0回答:1

我有一个二叉搜索树,我想使用它来为最常见的节点提供一定的输出,但是我很难做到这一点。我已经有代码可以打印出节点进入树的次数以及哪一个是最频繁的,但是我不知道如何编写if语句来比较这些值并能够输出某些内容(如果有的话)比另一个更大。我已经为此工作了几个小时,但无法获得正确的输出,请帮助。因此,如果我有更多的a,我希望它具有一定的输出,或者如果我有更多的b,其应该具有不同的输出,等等。

**node.h**

      struct node {
        char data{};
        int count{};
        node *left{};
        node *right{};
        int height{};
        node *next{};
    };
//gets the letter that was inserted the most
    char maxChar(struct node *node) { 
      struct node *p = node; 

      int max = -1; 
      char res; 

      while (p != NULL) { 

        // counting the frequency of current element 
        // p->data 
        struct node *q = p->next; 
        int count = 1; 
        while (q != NULL) { 
          if (p->data == q->data) 
            count++; 

          q = q->next; 
        } 

        // if current counting is greater than max 
        if (max < count) { 
          res = p->data; 
          max = count; 
        } 

        p = p->next; 
      } 

      return res; 
    } 




**Main.cpp**

    #include <iostream>
    #include <cstdlib>  
    #include <ctime>   
    #include <string>
    #include <algorithm> 
    #include <bits/stdc++.h> 

    using namespace std;

    //User Library
    #include "node.h"


    //Prototypes
    node * newNode(char);
    void prntIn(node*); //left-root-right
    char height(node*); //Find the height of the tree


    //Helper function that allocates a new node with the
    //given data and NULL left and right pointers.

    node* newNode(char data) {
        node *n = (struct node *)malloc(sizeof(struct node));
        n = new node;
        n->data = data;
        n->left = NULL;
        n->right = NULL;
        n->count = 1;
        return n;
    }

    struct node* instNod(struct node* node, char data) 
    { 
        /* If the tree is empty, return a new node */
        if (node == NULL) return newNode(data); 

        // If key already exists in BST,  
        // increment count and return 
        if (data == node->data) 
        { 
        (node->count)++; 
            return node; 
        } 

        /* Otherwise, recur down the tree */
        if (data < node->data) 
            node->left = instNod(node->left, data); 
        else
            node->right = instNod(node->right, data); 

        /* return the (unchanged) node pointer */
        return node; 
    } 


    /* Driver program to test above functions*/
    int main() {

        node *root = NULL;

        string input;
        string input2;
        string input3;


        cout<<"Welcome to Movie Recommendation!"<<endl;
        cout<<"I will ask you 10 questions and then recommend a movie for you to watch."<<endl;
        cout<<"Are you ready to start?"<<endl;

        cin>>input;
        if(input == "yes"){
        cout<<"Question 1: Do you prefer action movies, romantic, horror, or comedy's? "<<endl;
        cout<<"Please enter action, romantic, horror, or comedy"<<endl;
        cin>>input2;
        if(input2 == "action"){
            root = instNod(root, 'A');
        }
        if(input2 == "romantic"){
            root = instNod(root, 'B');
        }
        if(input2 == "comedy"){
            root = instNod(root, 'C');
        }
         if(input2 == "horror"){
            root = instNod(root, 'D');
        }
        cout<<"Question 2: Do you like the Hunger Games"<<endl;
        cout<<"Enter yes, alright, h for haven't seen it , or no"<<endl;
        cin>>input3;
        if(input3 == "yes"){
            root = instNod(root, 'A');
        }
        if(input3 == "h"){
            root = instNod(root, 'B');
        }
         if(input3 == "alright"){
            root = instNod(root, 'C');
        }
         if(input3 == "no"){
            root = instNod(root, 'D');
        }


         if(input == "no"){
             cout<<"Thanks for coming"<<endl;

         }
        //prints out the max char
    maxChar(root);

        return 0;
    }
c++ binary-search-tree frequency
1个回答
0
投票

对于初学者,将node定义更改为更类似于以下定义。此定义将所有结构的字段初始化为它们的零值。对我来说,此更改可防止在if (p->data == q->data)行发生访问冲突,可能是因为next指针未初始化为nullptr

struct node {
    char data{};
    int count{};
    node *left{};
    node *right{};
    int height{};
    node *next{};
};

类似地,我的编译器警告未初始化的变量res。因此也应像char res{};一样进行初始化。

© www.soinside.com 2019 - 2024. All rights reserved.