gpt4 book ai didi

java - 如何在Java中返回二叉树中最频繁的元素

转载 作者:行者123 更新时间:2023-12-03 18:48:47 25 4
gpt4 key购买 nike

我想返回 BinaryTree 中最频繁的值的数量。我有 BinaryClass,它包含很多方法,如 add、contain、isEmpty、counter、iterator 等。我试图实现这个方法 public int getMaxFrequency()但我在标记行遇到了 StackOverFlowException 问题。
当我运行我的代码时,我得到 StackOverFlow 异常 ,任何人都可以帮助我,
我是二叉树的新手。
请帮帮我。

enter code here
public class TreeSetCounter<T extends Comparable<T>> implements Iterable<T>{
public Node<T> root;
int size;
int count=0;

public TreeSetCounter() {
root = null;
size = 0;
}
public int counter(T t) {
return counterRecursive(root, t);
}

public int counterRecursive(Node<T> root, T t) {
int count = 0;
if(root == null) {
return 0;
}
if(root.value.equals(t)) {
count++;
}
count = count + counterRecursive(root.left, t)+ counterRecursive(root.right, t);
return count; }


public int getMaxFrequency(){

return inorder(root);
}

public int inorder( Node<T> prev) {

int count = 1, max = 0;
if (root == null) {
return 0;}
List<T> list = new ArrayList<>();
inorder(root.left); // I get the Exception att this row code.
while (prev != null) {
if (root.value == prev.value)
count++;
else
count = 1;
}

if (count > max) {
max = count;
list.clear();
list.add(root.value);
} else if (count == max) {
list.add(root.value);
}
prev = root;
inorder(root.right);
return max;
}

enter code here
Node.java
public class Node <T>{
T value;
int counter;
Node<T> left;
Node<T> right;

Node(T value, int count) {
this.value = value;
right = null;
left = null;
this.counter= count;
}

enter code here
public static void main(String[] args) {
TreeSetCounter <String> tsc= new TreeSetCounter<String>();
tsc.add("java");
tsc.add("java");
tsc.add("not");
tsc.add("cool");
tsc.add("java");
tsc.add("is");
tsc.add("java");
tsc.add("good");
System.out.println(tsc.getMaxFrequency());}

最佳答案

为您的计数器功能尝试这样的事情:

public int counter (T t) {
if (root == null) return 0;

int count = 0;
if (root.value.equals(t))
count++;

count += counterRecursive(root.left, t);
count += counterRecursive(root.right, t);

return count;
}

public int counterRecursive (Node<T> root, T t) {
if (root == null) return 0;

if (root.value.equals(t))
return 1 + counterRecursive(root.left, t) + counterRecursive(root.right, t);
else
return counterRecursive(root.left, t) + counterRecursive(root.right, t);
}
counter function 看起来像要使用的主要方法,并且 counterRecursive是你的辅助功能。更复杂的递归解决方案通常具有这种设计模式。
用斐波那契数来思考:
public static int fibonacci (int n) {
return (n <= 1) ? n : fibonacci(n - 1) + fibonacci(n - 2);
}
这应该可以帮助您调试 inOrdergetMaxFrequency .我会用 countergetMaxFrequency当我浏览其余部分时。一般来说,使用 HashMapHashTable更适合跟踪计数。
尝试这样的事情:
public int getMaxFrequency () {
if (root == null) return 0;

HashMap<T, Integer> counts = new HashMap<T, Integer>();

int count = counter(root.value);
counts.put(root.value, count);

// traverse the tree and grab counts
int left = getMaxFrequencyHelper(root.left, counts, count);
int right = getMaxFrequencyHelper(root.right, counts, count);

return left > right ? left : right;
}

private int getMaxFrequencyHelper (Node<T> node, HashMap<T, Integer> counts, max) {
if (node == null) return max;

if (!counts.containsKey(node.value))
counts.put(node.value, counter(node.value));

int _max = counts.get(node.value) > max ? counts.get(node.value) : max;

int left = getMaxFrequencyHelper(node.left, counts, _max);
int right = getMaxFrequencyHelper(node.right, counts, _max);

return left > right ? left : right;
}
这种技术被称为 memoization以前的计数是 cachedHashMap .

关于java - 如何在Java中返回二叉树中最频繁的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67288222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com