binary-search-tree

TreeMap removing all keys greater than a certain key

三世轮回 提交于 2019-12-12 11:38:43
问题 In a project I need to remove all objects having key value greater than a certain key (key type is Date , if it matters). As far as I know TreeMap implemented in Java is a red-black tree which is a binary search tree. So I should get O(n) when removing a subtree. But I can't find any method to do this other than making a tail view and remove one by one, which takes O(logn) . Any good ideas implementing this function? I believe treeMap is the correct dataStructure to use and should be able to

Parsing and building S-Expressions using Sets and binary search tree

余生颓废 提交于 2019-12-12 09:27:04
问题 This is pseudo homework (it's extra credit). I've got a BST which is an index of words that point to the lines (stored somewhere else) that contain the words. I need to implement a way to search using s-expressions so I can combine and (&) and or (|). At the command prompt a user could type something like: QUERY ((((fire)&(forest))|((ocean)&(boat)))&(water)) Essentially that should return all lines that contain the words fire, forest and water as well as all lines that contain ocean, boat and

First Common Ancestor of a Binary Tree

折月煮酒 提交于 2019-12-12 07:19:46
问题 If I have a binary search tree like this then what will be lowest common ancestor of nodes 6 and 1? 回答1: According to the Wikipedia definition of the Lowest common ancestor I correct myself: The lowest common ancestor (LCA) is a concept in graph theory and computer science. Let T be a rooted tree with n nodes. The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants ( where we allow a node to be a descendant of itself ). So

The parameter type `T` may not live long enough when writing a binary searching tree

我只是一个虾纸丫 提交于 2019-12-12 06:53:16
问题 I'm trying to write a binary searching tree in Rust, but I don't understand what is going on: enum BST<'a, T: Ord> { Leaf, BinTree { value: T, left: &'a mut BST<'a, T>, right: &'a mut BST<'a, T> } } impl<'a, T: Ord> BST<'a, T> { fn new() -> BST<'a, T> { BST::Leaf } fn add(self, val: T) { match self { BST::Leaf => self = BST::BinTree { value: val, left: &mut BST::<'a, T>::new(), right: &mut BST::<'a, T>::new() }, BST::BinTree{value: v, left: l, right: r} => if val < v { l.add(val); } else { r

C binary tree sort - extending it

雨燕双飞 提交于 2019-12-12 06:38:53
问题 I need some help in C Help me to extend the binary tree sort on C. I need to return a sorted array in sort function. here it is: #include <stdio.h> #include <stdlib.h> struct btreenode { struct btreenode *leftchild ; int data ; struct btreenode *rightchild ; } ; void insert ( struct btreenode **, int ) ; void inorder ( struct btreenode * ) ; int* sort(int *array, int arr_size) { struct btreenode *bt ; int i; bt = NULL; for ( i = 0 ; i <= 9 ; i++ ) insert ( &bt, array[i] ) ; inorder ( bt ) ; /

string search using symbol tables in Robert Sedwick book

。_饼干妹妹 提交于 2019-12-12 06:37:46
问题 I am reading about Algorithms in C++ by Robert Sedwick. In chapter 12 about symbol tables following text is taken. This program assumes that Item.cxx defines a char* data representation for string keys in items, an overloaded operator< that uses strcmp, an overloaded operator== that uses strncmp, and a conversion operator from Item to char* (see text). The main program reads a text string from a specified file and uses a symbol table to build an index from the strings defined by starting at

Find closest value in Binary Search Tree in C++

偶尔善良 提交于 2019-12-12 06:36:29
问题 This is my code. When I run my closestValue() method, I always get 0. Does anyone know how to get the closest value to an integer? This means I want the closest integer to the one I choose the closest value to but has to be present in the tree to be displayed template<typename T> T bst<T>::closestValue(T value) const { } template<typename T> T bst<T>::closestValue(T value, T & closest, bst_node<T>* node) const { bst<T>* pClosest = NULL; int minDistance = 5000; bst<T>* pNode = root; while

Complexity of a nested binary search tree

Deadly 提交于 2019-12-12 06:10:18
问题 Does anyone know how to calculate the complexity of a nested binary search tree? I have implemented a nested binary search tree to a depth of 3 BSTs. EDIT: I apologize for the confusion, I had meant that each node of the BST would point to the root node of another BST. The complexity I was asking for was time complexity of search, update, and delete (basic operations). I had assume that since the time complexity of a BST was O(log(n)), the time complexity of a nested BST in terms of search,

Trying to Adapt Binary Search Tree for Two Parameters, Getting Crash (C++)

人盡茶涼 提交于 2019-12-12 05:48:11
问题 For an assignment, I need to implement several functions of a binary search tree. I've gotten what seems like logical code, however, whenever I try and implement the insert node function, I get a program crash. Here is my code for the insert function: void insert (K k, V v) { TreeNode<K,V> * treeNode = NULL; TreeNode<K,V> *temp=NULL; TreeNode<K,V> *prev=NULL; temp = root; while(temp) //This is the loop that causes a crash, even if I remove the loop. { prev = temp; if (temp->key < treeNode-

Grid walking algorithm code correction

♀尐吖头ヾ 提交于 2019-12-12 04:34:33
问题 Grid Walking (Score 50 points): You are situated in an N dimensional grid at position (x_1,x2,...,x_N) . The dimensions of the grid are (D_1,D_2,...D_N) . In one step, you can walk one step ahead or behind in any one of the N dimensions. (So there are always 2N possible different moves). In how many ways can you take M steps such that you do not leave the grid at any point? You leave the grid if you for any x_i , either x_i <= 0 or x_i > D_i . Input: The first line contains the number of test