我试图用Java编写一个平衡二叉树的方法,描述如下:
..。将树的无序遍历写入数组,然后使用递归方法(类似于二进制搜索)插入数组的中间元素作为根,然后构建平衡的左右子树。
然而,我很困惑如何一起做这一切。到目前为止,我已经尝试过各种方法,但都没有效果。
我也已经有了一个inOrder迭代器,它返回树中所有元素的ArrayList,因此将讨论这个问题。
这是我目前正在建立的:
public void rebalance()
{
Iterator<T> it = iteratorInOrder();
List<T> list = new ArrayL
所以我知道溪流的基本原理。基本上,我可以像这样在java中实现它
inputStream = new FileReader("infinite.txt"); // or socket, whatever
int c;
while ((c = inputStream.read()) != -1) {
//something here
}
但这更多是一个理论问题,而不是一个编码问题。当面试官问这个问题时,他在找什么?我的意思是,我可以使用一个ArrayList,只要每次有一个字符串出现时就使用.append,然后运行一个函数对其进行排序.每次我追加后,因为你不能说永远结束
排序算法可以描述如下:
1.从数组数据创建二进制搜索树.
(对于多次发生,当前节点的增量发生变量)
2.无序地遍历BST。。
(无序遍历将返回数组中元素的排序顺序)。
3.按顺序遍历每个节点,用当前节点值覆盖当前索引处的数组元素(索引从0开始)。
下面是一个相同的Java实现:
节点类的结构
class Node {
Node left;
int data;
int occurence;
Node right;
}
无序函数(返回类型为int,只用于在每次调用时获得正确的索引,它们没有其他用途)
public int inorder(Node root,int[
我正在构建一个递归Java方法来平衡二进制搜索树(使用ints,但设计为泛型),在每个节点中使用权重。就我的目的而言,节点的权重被定义为子节点的数量+ 1。
2
/ \
1 3
The weight of the root is 3, and the weight of both leaves is 1.
在平衡结束时,任何节点的值都应该是植根于该节点的子树中所有节点的值的中值。
这是我的代码:
public void weightBalance (BinarySearchTree<AnyType> t) {
// Base case
if (t.g
我正在研究TreeMap in JAVA的源代码。根据JAVA文档:
一个基于红黑树的NavigableMap实现。映射根据其键的自然顺序进行排序,或者由在地图创建时提供的比较器进行排序,这取决于所使用的构造函数。
该实现为containsKey、get、put和remove操作提供了保证的日志(N)时间开销。算法是那些在Cormen,Leiserson,和Rivest对算法的介绍的适应。
在源代码中,我发现内部类条目被用作节点。
static final class Entry<K,V> implements Map.Entry<K,V> {
正如标题所述。我正在尝试从我创建的通用树创建一个二进制搜索树。我的通用节点类的代码是:
Node<E> parent;
E data;
ArrayList<Node<E>> children = new ArrayList<Node<E>>();
public Node(E data){
this.data = data;
}
public ArrayList<Node<E>> getChildren(){
return this.children;
}
public void addCh
我正在为数据结构考试进行练习,并一直在研究以下问题:“编写一个算法,在二叉树T中找到kth最高节点值。该算法必须在O(d)中运行,其中d是树的深度。”
我已经想出了这个(几个小时后),并且不确定运行时,我已经遍历了树两次,这是2d,也就是d吗?我还希望得到一些关于如何减少我使用过的方法的建议(如果可能的话)。
下面是我的答案,它使用递归帮助器方法来计算树中的节点数和顺序DFS:
public int getKthHighestValue(Node root, int k) {
int nodeCount = countNodes(root);
if (k < 0 || k
更新时间:东部时间4月29日下午11:15 --
谢谢大家,这帮了我很大的忙,就快到了……
public BinarySearchTree<AnyType> rebalanceHelp(ArrayList<AnyType> nl){
BinarySearchTree<AnyType> p = new BinarySearchTree<AnyType>();
//base case, if theres two elements in the list,
if(nl.size() == 2){p.insert(nl.get(0));p
优先级队列:基本操作:插入删除(Delete minumum元素)
目标:为上述功能提供高效的运行时间或增长顺序。
通过:实现优先级队列:
Linked List: Insertion will take o(n) in case of insertion at end o(1) in case of
insertion at head.
Delet (Finding minumum and Delete this ) will take o(n)
BST:
Insertion/Deltion of minimum = In av
我需要实现一个map接口,并使用二进制搜索树存储数据。我已经实现了接口,但我不知道下一步需要做什么。所以我想我的问题是在Java中给我上一堂二进制搜索树的速成课程。我从离散的数学课程中理解了树的理论,但我在将其转化为编程时遇到了困难。
这就是界面
public interface MapInterface<K,V> {
public MapInterface<K,V> put(K key, V value);
public V get(K key);
public boolean containsKey(K key);
public int si
我尝试做经典的问题来实现一个算法来打印n对圆括号的所有有效组合。
我找到了这个程序(运行得很好):
public static void addParen(ArrayList<String> list, int leftRem, int rightRem, char[] str, int count) {
if (leftRem < 0 || rightRem < leftRem) return; // invalid state
if (leftRem == 0 && rightRem == 0) { /* all out of le