电脑怎么格式化所有盘
电脑怎么格式化所有盘
更新时间:2023-08-15 09:08
最满意答案
示例代码。
public interface Tree<K, V> { /** * Find the nth smallest element in the tree * * @param nth * @return nth smallest element in the tree */ public K findSmallest(int nth); } @Override public K findSmallest(int nth) { Node iterator = root; return traverseLeftParentRight(iterator, new AtomicInteger(nth)); } private K traverseLeftParentRight(Node iterator, AtomicInteger nth) { if (null == iterator || nth.get() == 0) { return null; } K value = traverseLeftParentRight(iterator.left, nth); // Found in the left subtree itself if (null != value) { return value; } if (nth.decrementAndGet() == 0) { return iterator.key; } // Check in the right subtree return traverseLeftParentRight(iterator.right, nth); } public static void main(String[] args) { // Create a BST Comparator comparator = integerComparator(); Tree tree = new BinarySearchTree(comparator); fillData(tree); System.out.println("4thlest element " + tree.findSmallest(4)); } private static void fillData(Treetree) { tree.put(5, "value-5"); for (int i = 0; i <= 10; i++) { tree.put(i, "value-" + i); } }
阅读此Nth Samllest Element文章了解完整详情。
Sample code.
public interface Tree<K, V> { /** * Find the nth smallest element in the tree * * @param nth * @return nth smallest element in the tree */ public K findSmallest(int nth); } @Override public K findSmallest(int nth) { Node iterator = root; return traverseLeftParentRight(iterator, new AtomicInteger(nth)); } private K traverseLeftParentRight(Node iterator, AtomicInteger nth) { if (null == iterator || nth.get() == 0) { return null; } K value = traverseLeftParentRight(iterator.left, nth); // Found in the left subtree itself if (null != value) { return value; } if (nth.decrementAndGet() == 0) { return iterator.key; } // Check in the right subtree return traverseLeftParentRight(iterator.right, nth); } public static void main(String[] args) { // Create a BST Comparator comparator = integerComparator(); Tree tree = new BinarySearchTree(comparator); fillData(tree); System.out.println("4thlest element " + tree.findSmallest(4)); } private static void fillData(Treetree) { tree.put(5, "value-5"); for (int i = 0; i <= 10; i++) { tree.put(i, "value-" + i); } }
Read this Nth Samllest Element article for complete details.
相关问答
更多-
这是一个非常有名的问题,答案如下: public boolean isValid(Node root) { return isValidBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE); } private boolean isValidBST(Node node, int l, int h) { if(node == null) return true; return node.value > ...
-
示例代码。 public interface Tree
{ /** * Find the nth smallest element in the tree * * @param nth * @return nth smallest element in the tree */ public K findSmallest(int nth); } @Override public K findSmallest(int ... -
这是一种递归遍历树并在找到parentVal时停止的简单方法: // returns true if the element has been added to this subtree public boolean addLeft(E elem, E parentVal) { if (this.equals(parentVal)) { //Create node with element elem (call it newNode) //Set the left c ...
-
问题在于你的基础情况。 “树的高度是树中从根到最深节点的路径的长度,只有一个节点(根)的(根)树的高度为零。 - 维基百科 如果没有节点,则要返回-1不为0,这是因为最后添加1。 所以如果没有一个节点,你返回-1取消+1。 int findHeight(TreeNode
aNode) { if (aNode == null) { return -1; } int lefth = findHeight(aNode.left); int righth = ... -
我猜你正在寻找最低分的高尔夫球手 方法1:O(lg(n))时间,因为它沿树的左侧向下运行 public Golfer min(BinarySearchTree
tree) { BSTNode node = tree.root; if (node == null) { return null; } while (node.getLeft() != null) { node = node.getLeft(); ... -
它看起来不错,我重新格式化了一下代码,希望我没有错过任何东西。 迭代 public T FindSmallest(BinarySearchTree
tree){ BinaryNode Node = new BinaryNode(tree.getDataRoot()); if (Node == null) return null; while(Node.hasLeftChild()) Node = Node.getLeftChild(); return Node ... -
由Patrick Murphy提供,由于小错误导致输出错误,我很容易通过以下方式纠正: 如果(parent.get(键)== NULL) 正确的代码是: import java.util.*; class BST { Node root; LinkedList
q=new LinkedList (); TreeMap level=new TreeMap (); TreeMap par ... -
在以下代码中: Node* max = maximum(root); if (max->left){ if (max->parent->data > max->left->data) cout << max->parent->data; else cout << max->left->data; } else cout << max->parent->data; 如果你的max是root ,那么max->parent将为NULL 。 因此,在对data进行测试之前,应检查NULL ...
-
测试此案例 - 3 6 2 3.树将如下所示: 6 / 2 \ 3 你的方式,答案将是6,而它是3。 Test for this case - 3 6 2 3. Tree will look like this : 6 / 2 \ 3 The way you are doing, answer will come out to be 6, whereas it is 3.
-
find_element(root, k) if(root.left.nchildren + 1 == k - 1) return root; if(root.left.nchildren + 1 >= k) return find_element(root.left, k) else return find_element(root.right, k - (root.left.children + ...