函数来查找树是否为mono(所有元素都是唯一的)
Function to find whether a tree is mono (all the elements are unique) or not?
我试图将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));
}
算法解释
在树中的每个节点上,都需要执行以下步骤。
- 从节点右侧的子节点中查找节点最左侧的节点。如果它们的值相等,则树为NOTmono(停止搜索和
return false
) - 从节点左侧的子节点中查找节点最右侧的节点。如果它们的值相等,则树为NOTmono(停止搜索和
return false
) - 我们没有发现值相等的节点,所以树是mono!
return true
如果使用C++,只需使用std::set
或std::unordered_set
即可在树中存储对象列表。然后,使用树遍历(有序、预序、后序等),对遍历中的每个对象执行以下操作:
- 检查它是否已经在
set
中。如果是,则return false;
- 将元素添加到
set
中
然后,当您完成遍历时,您知道不存在任何重复,所以只需return true;
。
相关文章:
- 如何在 c++ 中迭代数组中的唯一元素
- tbb:current_unordered_map()-向量中每个唯一元素的ID
- 如何实现唯一 id 队列,其中元素可以在 C++ 中"bumped"到顶部?
- 如何在c++中找到数组的最小唯一元素
- 类实例的地址是否等于类中唯一元素的地址
- 识别结构C++中的唯一元素
- 在矢量中使用特定条件检查唯一元素
- 在迭代过程中删除向量的唯一元素?
- 在唯一元素上进行迭代器,而无需构造容器
- 实例化 std::vector 的唯一元素,而无需复制构造函数
- 从数组中删除唯一元素的最有效方法
- 在我试图创建一个载体中包含C 中的唯一元素的矢量时,发现不起作用
- 将整数分配给 std::vector 中排序的唯一元素
- 查找具有唯一标签的前 K 元素的算法
- 使用线程打印数组中的唯一元素
- 唯一元素堆栈的容器
- 元素在给定范围内的子矩阵中唯一元素的数目
- 创建包含唯一元素的优先级队列
- 自定义比较器,将唯一元素插入到c++中的集合中
- 这是在包含非唯一元素的未排序数组中选择第k个最大数的最快算法