二叉搜索树中节点/值的频率
Frequency of Node/value in a binary search tree
给定一个二叉搜索树,其中可能包含重复项,但BST的所有其他逻辑都是完整的,确定最频繁出现的元素。
class TreeNode
{
public:
TreeNode* right = NULL;
TreeNode* left = NULL;
int val;
TreeNode(int value)
{
val = value;
}
};
// To keep track of the frequency of the value/node
struct holder
{
public:
TreeNode* most = NULL;
int count = 0;
};
int frequencyOfNode(TreeNode* root, struct holder* ptr)
{
if (root == NULL)
{
return 0;
}
int left = frequencyOfNode(root->left, ptr);
int right = frequencyOfNode(root->right, ptr);
// need to check of left and right are nor null
if (left != 0 && root->val == root->left->val)
{
return 1 + left;
}
else if (right != 0 && root->val == root->right->val)
{
return 1 + right;
}
else
{
// left has a higher frequency
if (left >= right)
{
// left is bigger;
if (left > ptr->count)
{
ptr->most = root->left;
ptr->count = left;
}
}
else
{
// right has a higher frequency
if (right > ptr->count)
{
ptr->most = root->right;
ptr->count = right;
}
}
return 1;
}
}
我正在对二叉搜索树进行后序遍历。当节点以连续顺序出现时,我的逻辑有效,但如果节点不是以连续顺序出现;重置节点频率。
我的时间是O(n),空间是O(1)。
问题是当节点没有连续链接时。
我的示例树:
int main()
{
TreeNode *root = new TreeNode(6);
root->right = new TreeNode(8);
root->right->left = new TreeNode(7);
root->right->right = new TreeNode(8);
root->right->right->right = new TreeNode(8);
root->right->right->right->right = new TreeNode(9);
root->right->right->right->right->left = new TreeNode(8);
root->left = new TreeNode(4);
root->left->right = new TreeNode(5);
root->left->right->right = new TreeNode(5);
root->left->right->right->right = new TreeNode(5);
root->left->left = new TreeNode(1);
root->left->left->right = new TreeNode(1);
root->left->left->right->right = new TreeNode(1);
root->left->left->right->right = new TreeNode(2);
root->left->left->left = new TreeNode(0);
struct holder freq;
int ran = frequencyOfNode(root, &freq);
std::cout << "random" << ran << std::endl;
std::cout << "The Node: " << freq.most->val << " frequency " << freq.count
<< std::endl;
return 0;
}
我真的很困惑如何考虑,当节点不是连续的(即8->8->8->9->8)。
我看你已经自己解决了一些问题。无论如何,我决定彻底解决这个问题,同时改变一些东西来简化一切。它占用O(N)个时间和O(1)个空间:
#include <iostream>
#include <limits>
class TreeNode
{
public:
TreeNode* right;
TreeNode* left;
int val;
TreeNode(int value)
{
val = value;
right = left = NULL;
}
};
// To keep track of the frequency of the value/node
struct Holder
{
public:
int value;
int count;
Holder(int v=std::numeric_limits<int>::min(), int c=-1): value(v), count(c) {}
};
void dfs(TreeNode* root, int &mostFrequent, int &mostFrequentCount, int ¤t, int ¤tCount)
{
if(root->left) dfs(root->left, mostFrequent, mostFrequentCount, current, currentCount); //first go to smaller
int val = root->val;
if(val == current) currentCount++;
else { current=val; currentCount=1; }
if(currentCount > mostFrequentCount)
{
mostFrequent=current;
mostFrequentCount=currentCount;
}
if(root->right) dfs(root->right, mostFrequent, mostFrequentCount, current, currentCount); //finally go to larger
}
Holder getMostFrequent(TreeNode *root)
{
int mostFrequent=-1,mostFrequentCount=-1, current=std::numeric_limits<int>::min(), currentCount=-1;
if(root) dfs(root, mostFrequent, mostFrequentCount, current, currentCount);
return Holder(mostFrequent, mostFrequentCount);
}
int main()
{
TreeNode *root = new TreeNode(6);
root->right = new TreeNode(8);
root->right->left = new TreeNode(7);
root->right->right = new TreeNode(8);
root->right->right->right = new TreeNode(8);
root->right->right->right->right = new TreeNode(9);
root->right->right->right->right->left = new TreeNode(8);
root->left = new TreeNode(4);
root->left->right = new TreeNode(5);
root->left->right->right = new TreeNode(5);
root->left->right->right->right = new TreeNode(5);
root->left->left = new TreeNode(1);
root->left->left->right = new TreeNode(1);
root->left->left->right->right = new TreeNode(1);
root->left->left->right->right = new TreeNode(2);
root->left->left->left = new TreeNode(0);
Holder h = getMostFrequent(root);
std::cout << "most frequently encountered element: " << h.value << ", " << h.count << " timesn";
return 0;
}
它利用了这样一个事实,因为这是一个BST,按[left -> current -> right]的顺序遍历它会得到排序的元素,就这样。
相关文章:
- 反向给定链表中的K节点
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- 芬威克树(BIT).找到具有给定累积频率的最小索引,单位为 O(logN)
- 在指针的帮助下,文本文件中单词的频率
- Boost Graph Library,修复节点大小
- C++A*算法并不总是在路径中具有目标节点
- 如何找到2个单链表的公共节点
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 为什么我的删除节点函数实际上没有删除节点?
- 我们可以删除链表中静态内存中的节点吗
- 如何在pugixml中获取节点的内部XML
- 函数计算用户按下按钮的频率
- 为什么我们要为avl树实现返回一个指向节点的指针,而不是void函数
- C++RapidXml-使用first_node()遍历以修改XML文件中节点的值
- 在条件变量中触发错误信号的频率是多少
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 节点 *temp; 和节点 *tmp = 新节点之间的差异
- 引擎节点:未定义的符号:_ZTV6Config
- 二叉搜索树中节点/值的频率