Я пытаюсь найти элемент большинства в моем бинарном дереве поиска, но мне не повезло.Любые идеи, как реализовать алгоритм для получения элемента большинства?Я думаю, что мое двоичное дерево поиска в порядке, но я действительно не знаю, как искать элемент большинства с помощью двоичного дерева поиска. Это мой код
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)
{
}
}
}
Заранее спасибо!