函数来查找树是否为mono(所有元素都是唯一的)

Function to find whether a tree is mono (all the elements are unique) or not?

本文关键字:元素 唯一 查找 是否 mono 函数      更新时间:2023-10-16

我试图将Total函数和isMono函数添加到此代码中。是否已合计

需要函数ismono的帮助,该函数返回树是否为mono(所有元素都是唯一的,也就是没有元素出现一次以上)。请

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

只需遍历树

//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. 从节点右侧的子节点中查找节点最左侧的节点。如果它们的值相等,则树为NOTmono(停止搜索和return false
  2. 从节点左侧的子节点中查找节点最右侧的节点。如果它们的值相等,则树为NOTmono(停止搜索和return false
  3. 我们没有发现值相等的节点,所以树是mono!return true

如果使用C++,只需使用std::setstd::unordered_set即可在树中存储对象列表。然后,使用树遍历(有序、预序、后序等),对遍历中的每个对象执行以下操作:

  1. 检查它是否已经在set中。如果是,则return false;
  2. 将元素添加到set

然后,当您完成遍历时,您知道不存在任何重复,所以只需return true;