Tag: 数据结构

检查树是否是二叉搜索树

我编写了以下代码来检查树是否是二进制搜索树。 请帮我查一下代码: 好的! 代码现在已编辑。 以下post中有人建议使用这个简单的解决方案: IsValidBST(root,-infinity,infinity); bool IsValidBST(BinaryNode node, int MIN, int MAX) { if(node == null) return true; if(node.element > MIN && node.element < MAX && IsValidBST(node.left,MIN,node.element) && IsValidBST(node.right,node.element,MAX)) return true; else return false; }

如何反向打印数组?

这是我的数组代码,我需要反向打印。 public class Array1D { public static void main(String[] args) { int[] array = new int[3]; array[0] = 1; array[1] = 2; array[2] = 5; for (int i = 0; i < array.length; i++) { //loop System.out.print("array["+i+"]="); System.out.println(array[i] + " "); } System.out.println("the last element in the matrix = " + array[array.length – 1]); // […]

从linkedlist删除最小值

我需要从链表中删除最小的元素值。 喜欢 {8,4,7,2,9,4,5,3} 变为: {8,4,7,9,4,5,3} 我写了这个: public void RemoveMin() { T min = list.getInfo(); for (int i = 0; i < 7; i++) { if (list.getLink() < min) min = (T) list.getLink(); else continue; } }

给定每个列表中最多N个元素的K个排序列表,在所有项目上返回已排序的迭代器

Example: List 1: [1, 4, 5, 8, 9] List 2: [3, 4, 4, 6] List 3: [0, 2, 8] Would yield the following result: Iterator -> [0, 1, 2, 3, 4, 4, 4, 5, 6, 8, 8, 9] 我不愿意创建一个接受k列表的“合并”方法,并以空间复杂的精神将List的内容合并到另一个List。 这是一个可以使用“min Heap”实现的k-way合并问题。 任何指针都会非常有用。 public class CustomListIterator implements Iterator{ private boolean canAddIterators = true; private boolean balanceTreeIteratorFlag […]

无法在java方法中正确validation平衡括号解析

我有一个方法,应该使用javavalidation字符串中准确的开括号和右括号。 此方法将用于解析数学表达式,因此对括号进行平衡非常重要。 出于某种原因,它在以下两个运行中都返回false: System.out.println(parChecker(“(()”)); // returns false 🙂 System.out.println(parChecker(“((()))”)); // returns false 🙁 WHY?? 这是使用堆栈来解决问题的方法。 这里出了点问题,因为它对于一组平衡的括号也会返回错误。 有什么问题? 先谢谢你。 public static boolean parChecker(String str) { String[] tokens = str.split(“”); int size = tokens.length; Stack theStack = new Stack(size); int index = 0; boolean balanced = true; while ((index < size) && balanced) { String symbol = […]

HashMap是一个合适的数据结构

我在HashMap中存储了3种类型的对象。 HashMap<String, ArrayList> [‘Lorry’, [list of lorries]] [‘Sport’, [list of sport’s cars]] HashMap字符串键保持对象的类型(Car的子类),第二个元素在数组中存储具有如下属性的对象:ID,日期等。 我要做的四件主要事情是: 当没有提供有关其类型的信息时,检查HashMap中是否存在某些ID 根据类型打印某些ID的元素。 打印特定类型的所有元素 如果每个对象分配的某个属性具有布尔值,例如“true”,则打印集合(不同类型)中的所有元素; HashMap是否是正确的结构? 如果谈到第一点,我发现它有问题。 似乎我将不得不遍历整个集合,如果是这样,其他集合对于这些要求更好?

如何使用枚举中的每种环境类型的值列表来表示键?

我有两个环境PROD和STAGING 。 在prod环境中,我们有三个数据中心ABC , DEF和PQR并且暂存有一个数据中心CORP 。 每个数据中心都有很少的机器,我为它们定义了常量,如下所示: // NOTE: I can have more machines in each dc in future public static final ImmutableList ABC_SERVERS = ImmutableList.of(“tcp://machineA:8081”, “tcp://machineA:8082”); public static final ImmutableList DEF_SERVERS = ImmutableList.of(“tcp://machineB:8081”, “tcp://machineB:8082”); public static final ImmutableList PQR_SERVERS = ImmutableList.of(“tcp://machineC:8081”, “tcp://machineC:8082”); public static final ImmutableList STAGING_SERVERS = ImmutableList.of(“tcp://machineJ:8087″,”tcp://machineJ:8088”); 现在我在同一个类中定义了另一个常量,它按DC分组到每个环境类型的机器列表。 public static final ImmutableMap<Datacenter, […]

理解节点和链表概念的问题

我是Data Structures和Algorithim的新手,我在解决链表的整个主题及其与节点的连接方面遇到了一些麻烦。 令我困惑的问题是指针和他们指向的东西 这是教科书中的示例问题 (i) “What happens to the pointer head when the object obj is added to an empty linked list?” head1 = new listNode(obj, head); (ii) Write A Constructor to represent (i) 我已经看到堆栈溢出是获得一些帮助的地方,我需要一些直接的帮助,所有的想法都将深受赞赏。 提前致谢

通过PriorityQueue迭代不会产生有序结果

import java.util.*; class Priority{ public static void main(String args[]){ PriorityQueue queue=new PriorityQueue(); queue.add(“Amit”); queue.add(“Vijay”); queue.add(“Karan”); queue.add(“Jai”); queue.add(“Rahul”); System.out.println(“head:”+queue.element()); System.out.println(“head:”+queue.peek()); System.out.println(“iterating the queue elements:”); Iterator itr=queue.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } queue.remove(); queue.poll(); System.out.println(“after removing two elements:”); Iterator itr2=queue.iterator(); while(itr2.hasNext()){ System.out.println(itr2.next()); } } } Output:head:Amit head:Amit iterating the queue elements: Amit Jai Karan Vijay Rahul after removing two […]

组合两个二叉树的算法?

例如: 两棵树: 8 9 5 7 4 20 30 成为一棵树?