我试图在我的二叉搜索树中找到多数元素,但我没有运气。任何想法如何实现获取多数元素的算法?我认为我的二叉搜索树没问题,但我真的不知道如何使用二叉搜索树搜索多数元素 这是我的代码
class BinarySearchTree
{
private Node root = null;
public Node Root { get => root; set => root = value; }
private int size = 0;
public BinarySearchTree()
{
//empty
}
public Node getNodeByValue(int v, Node start)
{
//if start node is empty value not found.
if (start == null)
{
return null;
}
if (v == start.Value)
{
return start; //Value found at start
}
else if (v < start.Value)
{
return getNodeByValue(v, start.LeftChild);
}
else if (v > start.Value)
{
return getNodeByValue(v, start.RightChild);
}
else
{
return null; //value not found
}
}
public void AddNode(Node start, int v)
{
//Insert the new node in the tree
InsertNewNode(v, start);
}
public Node InsertNewNode(int v, Node start)
{
Node newNode = new Node(v);
if (root == null)
{
root = newNode;
}
if (start == null)
{
start = newNode;
size++;
return start;
}
if (v < start.Value)
{
if (start.LeftChild == null)
{
start.LeftChild = newNode;
}
else
{
InsertNewNode(v, start.LeftChild);
}
}
else if (v > start.Value)
{
if (start.RightChild == null)
{
start.RightChild = newNode;
}
else
{
InsertNewNode(v, start.RightChild);
}
}
else
{
InsertNewNode(v, start);
size++;
}
return start;
}
public void IncrementNodeFrequency(Node n)
{
//If the node is not null
if (n != null)
{
n.Frequency = n.Frequency + 1;
}
}
public int getMajorityElement(Node start)
{
if(start!=null)
{
}
}
}
提前谢谢!
答案 0 :(得分:0)
在许多访谈中都会提出这个问题,它有很多方法:
1)遍历整个树并包含在字典中并获得最多计数
2)或者在职业杯中的这个问题:https://www.careercup.com/question?id=17192662
#include <iostream>
using namespace std;
struct TreeNode{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int val_)
:val(val_), left(NULL), right(NULL){}
};
int maxFreqVal;
int maxFreqCount;
void inorder(TreeNode *node, int &curFreqVal, int &curFreqCount){
if(node == NULL)
return;
inorder(node->left, curFreqVal, curFreqCount);
if(curFreqVal != node->val){
curFreqVal = node->val;
curFreqCount = 1;
}else{
curFreqCount++;
if(curFreqCount > maxFreqCount){
maxFreqVal = curFreqVal;
maxFreqCount = curFreqCount;
}
}
inorder(node->right, curFreqVal, curFreqCount);
}
int getFreq(TreeNode *root){
if(root == NULL)
return -1;
maxFreqVal = root->val;
maxFreqCount = 1;
int curFreqVal = root->val;
int curFreqCount = 1;
inorder(root, curFreqVal, curFreqCount);
return maxFreqVal;
}
int main(){
TreeNode *root = new TreeNode(10);
root->right = new TreeNode(12);
root->right->left = new TreeNode(12);
root->right->left->left = new TreeNode(12);
root->right->left->right = new TreeNode(12);
root->right->right = new TreeNode(12);
root->right->right->left = new TreeNode(12);
root->right->right->right = new TreeNode(12);
root->left = new TreeNode(6);
root->left->left = new TreeNode(6);
root->left->left->left = new TreeNode(4);
root->left->left->right = new TreeNode(6);
root->left->right = new TreeNode(6);
root->left->right->left = new TreeNode(6);
root->left->right->right = new TreeNode(6);
root->left->right->right->right = new TreeNode(7);
int result = getFreq(root);
cout << "result : " << result << ", frequency : " << maxFreqCount << endl;
return 0;
}