【问题标题】:A function that will take a tree as argument and return the number of ints that were typed?一个将树作为参数并返回输入的整数数量的函数?
【发布时间】:2013-08-07 17:46:11
【问题描述】:

我的项目中需要一个函数,它将树作为参数并返回输入的整数数量和正确的调用。

不就是预购吗?如下所示。请。谢谢

#include "t.h"
void preorder(tnode * t){
if (t == NULL) return;
cout << t->info <<endl;
preorder(t->left);
preorder(t->right);
}

调用将是 preorder(t)。

这是我原来的功能

 #ifndef T_H

#define T_H

#include <iostream>
#include <iomanip>
using namespace std;

struct tnode {
    int info ;
    int count;
    tnode * right, *left;
};

tnode * insert(int target,tnode * t);
tnode * makenode(int x);
tnode * tsearch(int x,tnode * t);
void inorder(tnode * t);
int height(tnode * t);
int count(tnode * t) ;
int total(tnode * t) ;

#endif

int main() {
int n,c;
tnode * t = NULL, *x;
    while (cin >> n) {t=insert(n,t);cout << n <<' ';}
    cout << endl;
    inorder(t);
    cout << endl;
    c = count(t);
    cout << "count: "<< c  <<endl;
    cout << endl;
    c = height(t);
    cout << "height: "<< c  <<endl;
    cout << endl;
    c=200;
    while (c-->0) if (x = tsearch(c,t)) cout << c << " is on the tree."<<endl;
return 0;
}

#include "t.h"

int count(tnode * t) {
    if (t == NULL) return 0;
    return 1 +  count(t->left) + count (t->right);
}

#include "t.h"

int height(tnode * t) {
    if (t == NULL) return -1;
    return 1 + max(height(t->left) , height (t->right));
}

#include "t.h"

//write out t in order
void inorder(tnode * t) {
    if (t == NULL) return;
    inorder (t->left);//write out lst in order
    cout <<setw(5) << t->info <<setw(5) << t->count<< endl;
    inorder (t->right);//write out rst in order
}

#include "t.h"

tnode * insert(int x, tnode * t) {
tnode * tmp = tsearch(x,t);
if (tmp != NULL) {
    tmp->count++;
    return t;
}
if (t == NULL) return makenode(x);
if ( x < t->info ) {
    t->left = insert(x,t->left);
    return t;
}
t->right = insert(x,t->right);
return t;
}

#include "t.h"

tnode * makenode(int x) {
tnode * t = new tnode;
    t->info =x;
    t->count =1;
    t->right = t->left = NULL;
return t;
}

【问题讨论】:

  • 这个问题有点模糊。这棵树代表一个表达式的解析树(在这种情况下,整数的数量是叶子的数量only)还是它只是一个整数的二叉树(在这种情况下它暗示节点数)?此外,您没有通过函数传递累加器,因此您需要一个全局计数器(或修复函数参数以包含一个输入输出 int* 参数作为您的计数器累加器)。

标签: c++ unix recursion binary-search-tree preorder


【解决方案1】:

首先,您的函数不能为空。它必须返回输入的 int 数量,因此它必须返回 int 或 int*。

其次,这棵树是一棵包含所有已键入整数的二叉树吗?如果是这样,任何树遍历算法都可以。你只需要一个变量,当你找到一个新节点时你会增加一个变量(假设它们都存储一个 int)。

int preorder(tnode * t){
  if (t == NULL) return 0;

  else{
     return 1 + preorder(t->left) + preorder(t->right);
  }
}

如果 t 不为 null,则它存储了 1 个 int。然后你只需要检查节点的子节点。

【讨论】:

    【解决方案2】:

    当用户键入一个数字时,insert 函数要么插入一个计数为 1 的新节点,要么添加一个现有节点的计数。 您需要总结树中元素的计数:

    int tcount(tnode * t){
        if (t == NULL) return 0;
        return t->count + tcount(t->left) + tcount(t->right);
    }
    

    一个典型的电话是

    tnode * root = NULL;
    
    /* insert stuff into the tree */
    
    int count = tcount(root);
    

    【讨论】:

    • 知道了。我还尝试编写一个函数 ismono,它返回树是否为单声道(所有元素都是唯一的,也就是没有元素出现超过一次)。我不知道
    • @user2661545 - 简单。使用与上述类似的逻辑。如果根是NULL,则返回true;如果根的count字段大于1,则返回false;否则返回对左右子树的递归调用的逻辑与。
    • @user2661545 - 我看到你接受了 ivoSilva 的回答。该解决方案计算插入到树中的 unique 整数的数量,而不是键入的整数的数量。那是你真正想要的吗? (例如,如果输入是序列 [1, 2, 3, 2],我的代码将返回 4,而 ivoSilva 将返回 3。)
    猜你喜欢
    • 1970-01-01
    • 2016-05-18
    • 2019-10-18
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多