binary-search-tree

Dynamic prefix sum

匆匆过客 提交于 2020-01-02 05:37:05
问题 Is there any data structure which is able to return the prefix sum [1] of array, update an element, and insert/remove elements to the array, all in O(log n)? [1] "prefix sum" is the sum of all elements from the first one up to given index For example, given the array of non-negative integers 8 1 10 7 the prefix sum for first three elements is 19 ( 8 + 1 + 10 ). Updating the first element to 7 , inserting 3 as the second element and removing the third one gives 7 3 10 7 . Again, the prefix sum

C++ : Running time of next() and prev() in a multiset iterator?

隐身守侯 提交于 2020-01-02 00:58:12
问题 What is the time complexity of applying the next() and prev() function on an multiset<int>::iterator type object where the corresponding multiset contains the N elements? I understand that in STL, a multiset is implemented as a balanced binary search tree and hence I expect the time complexity to be O(log N) per operation (in the worst case) in case we just traverse the tree until we find the appropriate value, but I have a hunch that this should be O(1) on average. But what if the tree is

How to search and sort BST by name(string)? Printing by queue and indented?

五迷三道 提交于 2020-01-01 19:58:08
问题 I have to write a program that reads a .txt file into the tree and then it allows to perform specific operations with it. I'm stuck on the part where I need to sort tree by names and search by name as well, any input would be awesome. So, my input file is in the format : 3800 Lee, Victor; 2.8 3000 Brown, Joanne; 4.0 So, my binary tree is in the format of: typedef struct { int id; char name[MAX_NAME_LEN]; float gpa; } STUDENT; typedef struct node { STUDENT* dataPtr; struct node* left; struct

how to determine a balanced or perfectly balanced Binary search tree ( just from the picture )

南笙酒味 提交于 2020-01-01 11:20:11
问题 I am not sure how to determine if a tree is balanced, perfectly balanced, or neither if I have it as a picture not a code For example if I have this tree How can I check if it's balanced, perfectly balanced, or unbalanced? and can someone give me an example of a perfectly balanced tree? [o] / \ [b] [p] \ / \ [d] [m] [r] Clearly I can tell that the tree is unbalanced if it was something like this: [b] \ [d] \ [r] \ [c] However, if it was something very similar to the one above I don't know how

Converting a list of nested lists in binary tree

别等时光非礼了梦想. 提交于 2020-01-01 07:21:16
问题 In python, I have a list of nested lists that represents a binary tree: L = [0, [[1, [2, 3]], [4, [5, 6]]]] So the tree can be seen as follows: 0 / \ 1 4 /\ /\ 2 3 5 6 I now want to implement a function that takes as input a level of the tree and returns all the nodes of that level: GetNodes(0) = 0 GetNodes(1) = [1,4] GetNodes(2) = [2,3,5,6] Is there an easy way to do this, avoiding a brutal search on all the nested lists of L ? Is there the possibility of a more standard management of binary

Converting a list of nested lists in binary tree

流过昼夜 提交于 2020-01-01 07:21:09
问题 In python, I have a list of nested lists that represents a binary tree: L = [0, [[1, [2, 3]], [4, [5, 6]]]] So the tree can be seen as follows: 0 / \ 1 4 /\ /\ 2 3 5 6 I now want to implement a function that takes as input a level of the tree and returns all the nodes of that level: GetNodes(0) = 0 GetNodes(1) = [1,4] GetNodes(2) = [2,3,5,6] Is there an easy way to do this, avoiding a brutal search on all the nested lists of L ? Is there the possibility of a more standard management of binary

Finding the common ancestor in a binary tree

一世执手 提交于 2020-01-01 03:12:06
问题 This question was asked to me in an interview: I have a binary tree and I have to find the common ancestor (parent) given two random nodes of that tree. I am also given a pointer to the root node. My answer is: Traverse the tree separately for both the nodes till you reach the node that is expected. Parallel while traversing store the element and the next address in a linked list. Then we have two linked lists with us. So try comparing the two linked lists and the last common node in both the

Finding the common ancestor in a binary tree

血红的双手。 提交于 2020-01-01 03:11:11
问题 This question was asked to me in an interview: I have a binary tree and I have to find the common ancestor (parent) given two random nodes of that tree. I am also given a pointer to the root node. My answer is: Traverse the tree separately for both the nodes till you reach the node that is expected. Parallel while traversing store the element and the next address in a linked list. Then we have two linked lists with us. So try comparing the two linked lists and the last common node in both the

Concrete examples of using binary search trees?

…衆ロ難τιáo~ 提交于 2019-12-31 08:13:09
问题 I understand how binary search trees are implemented, but I am not sure what are the advantages of using it over the hash tables that most programming languages have built into their standard libraries. Could someone please provide examples of real-world problems solvable with binary search trees? 回答1: There are a few theoretical advantages of binary search trees over hash tables: They store their elements in sorted order . This means that if you want to store the container in a way where you

ConcurrentModificationException Occuring When Retrieving List Size

坚强是说给别人听的谎言 提交于 2019-12-31 04:43:07
问题 For a project in my Data Structures class I have been tasked with creating a 3-Dimensional Range Tree where each dimension is a BST. I read this question, but it's an Android question, and the causes of our issues seem different, and the only answer was not accepted. Wall of code coming shortly. Sorry. Classes Involved: Point3D<T> : Generic class containing the coordinates for the point. T extends Comparable<T> , and Point3D extends it as well RangeTree<T> : Generic class containing the root