binary-search-tree

How to write findMinimum of a lazy deleted Binary Search Tree in Java

旧巷老猫 提交于 2020-05-16 04:06:29
问题 I am writing a class for a Binary Search Tree in Java that uses lazy deletion (instead of removing the node from the tree, it sets a "deleted" flag to true) My question is, how would I implement a findMin function for a tree like this? The normal method of just going to the leftmost leaf wouldn't work because that lead may be "deleted". For example, A tree like this where you delete 20, 5, and 17 25 *20 30 *17 89 *5 should return 25 when you call findMin(). My implementation looks like this:

Binary trees and quicksort?

左心房为你撑大大i 提交于 2020-04-16 05:56:28
问题 I have a homework assignment that reads as follows (don't flame/worry, I am not asking you to do my homework): Write a program that sorts a set of numbers by using the Quick Sort method using a binary search tree. The recommended implementation is to use a recursive algorithm. What does this mean? Here are my interpretations thus far, and as I explain below, I think both are flawed: A. Get an array of numbers (integers, or whatever) from the user. Quicksort them with the normal quicksort

What is a coarse and fine grid search?

社会主义新天地 提交于 2020-04-13 17:01:12
问题 I was reading this answer Efficient (and well explained) implementation of a Quadtree for 2D collision detection and encountered this paragraph All right, so actually quadtrees are not my favorite data structure for this purpose. I tend to prefer a grid hierarchy, like a coarse grid for the world, a finer grid for a region, and an even finer grid for a sub-region (3 fixed levels of dense grids, and no trees involved), with row-based optimizations so that a row that has no entities in it will

Why red-black tree based implementation for java TreeMap?

房东的猫 提交于 2020-01-31 03:40:06
问题 The third paragraph of wikipedia's article on AVL trees says: "Because AVL trees are more rigidly balanced, they are faster than red-black trees for lookup-intensive applications." So, shouldn't TreeMap be implemented using AVL trees instead of red-black trees(as there will be more look up intensive applictions for a hashing based data structure ) ? 回答1: Red-Black trees are more general purpose. They do relatively well on add, remove, and look-up but AVL trees have faster look-ups at the cost

non-void function works fine even without executing return

强颜欢笑 提交于 2020-01-30 06:58:27
问题 I'm a computer science college student. Yesterday, I have a class about Binary Search Tree using C++. We are taught by lab assistants in that class. They define the node in the tree as a struct like this : struct Data{ char name[15]; int age; Data *left,*right; }; and they give us a code to search within the BST like this: // temp is current node, name is the value of the node to be searched for. Data* search(Data *temp,char name[]) { if(strcmp(temp->name,name)>0) search(temp->left,name);

Trying to create new instance of class using template, unexpected error

冷暖自知 提交于 2020-01-30 06:38:08
问题 Trying to make a B inary S earch T ree (BST for short) using a template. When I try to create a new instance of my BST I get an unexpected error. I hope the solution does not involve pointers since I would like to keep them at a minimum. For now I have: template <typename Type> class BST { // The binary search tree containing nodes private: BSTNode<Type> *root; // Has reference to root node public: BST (); bool add (int, Type); }; And the Node type: EDIT: When I cut out code to un-encumber

Next largest element in a binary search tree [closed]

早过忘川 提交于 2020-01-30 04:05:05
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I'm looking for a simple algorithm to find the next largest element (key) in a binary search tree, can anyone help? 回答1: Assuming that by "next largest," you mean, the next largest node from wherever the current

Next largest element in a binary search tree [closed]

时光怂恿深爱的人放手 提交于 2020-01-30 04:04:15
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I'm looking for a simple algorithm to find the next largest element (key) in a binary search tree, can anyone help? 回答1: Assuming that by "next largest," you mean, the next largest node from wherever the current

Bad Operand Types for Binary Operator “>”?

岁酱吖の 提交于 2020-01-29 17:45:28
问题 I am writing a BST Program. I get the error: "Bad Operand Types for Binary Operator ">" first type: java.lang.Object second type: java.lang.Object" This is the method where it gives me the error: public void placeNodeInTree(TreeNode current, TreeNode t) { if(current == null) current = t; else{ if(current.getValue() > t.getValue()) current.setRight(t); if(current.getValue() < t.getValue()) current.setLeft(t); } } getValue() has a return type of Object, thus the java.lang.Object types. This is

Balanced Binary Search Tree for numbers

拈花ヽ惹草 提交于 2020-01-26 04:45:45
问题 I wanted to draw a balanced binary search tree for numbers from 1 to 20. _______10_______ / \ ___5___ 15 / \ / \ 3 8 13 18 / \ / \ / \ / \ 2 4 7 9 12 14 17 19 / / / / 1 6 11 16 Is the above tree correct and balanced? 回答1: In answer to your original question as to whether or not you need to first calculate the height, no, you don't need to. You just have to understand that a balanced tree is one where the height difference between the tallest and shortest node is zero or one, and the simplest