0

我试图在这段代码中添加 Total 函数和 isMono 函数。已经总计了

需要函数 ismono 的帮助,它返回树是否是单声道(所有元素都是唯一的,也就是没有元素出现超过一次)。请

#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;
}
4

2 回答 2

1

只需遍历树!

//Gets the value of the node at the leftmost node
int left_most_value(tnode * t) {
    if (t == NULL) return (0); //Something went wrong
    if (t->left == NULL) return(t->info);
    else return(left_most_value(t));
}

//Gets the value of the node at the rightmost node    
int right_most_value(tnode * t) {
    if (t == NULL) return (0); //Something went wrong
    if (t->right == NULL) return(t->info);
    else return(right_most_value(t));
}

//Returns true (1) if node does NOT have duplicate
int node_is_mono(tnode * t) {
    //Ignore leaf nodes
    if (t->left == NULL && t->right == NULL) return 1;

    //Check the left side
    if (t->left != NULL && right_most_value(t->left) == t->info) return(0);

    //Check the right side
    if (t->right != NULL && left_most_value(t->right) == t->info) return(0);

    //This node is mono
    return(1);
}

int tree_is_mono(tnode * t) {
    if (t == NULL) return(1);

    //If one node has a duplicate, then the entire tree is NOT mono 
    if (node_is_mono(t) == 0) return 0;
    else return(tree_is_mono(t->left) && tree_is_mono(t->right));
}

算法说明

在树中的每个节点上,您需要执行以下步骤。

  1. 从右侧节点的子节点中找到该节点最左侧的节点。如果它们的值相等,则树不是单声道(停止搜索和return false
  2. 从左侧节点的子节点中找到该节点的最右侧节点。如果它们的值相等,则树不是单声道的(停止搜索和return false
  3. 我们发现没有节点值相等,所以树是单声道的!return true
于 2013-08-07T19:13:29.397 回答
0

如果您使用的是 C++,您可以简单地使用 astd::setstd::unordered_set在树中存储对象列表。然后,您使用树遍历(中序、前序、后序等)并对遍历中的每个对象执行以下操作:

  1. 你检查它是否已经在set. 如果是,那么return false;
  2. 您将元素添加到set.

然后,当您完成遍历时,您知道没有任何重复项,所以简单地说return true;.

于 2013-08-07T19:13:10.860 回答