binary-search-tree

Dynamic prefix sum

[亡魂溺海] 提交于 2019-12-05 14:55:36
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 of first three elements would be 20 . For prefix sum and update, there is Fenwick tree . But I don't

Python: Create a Binary search Tree using a list

寵の児 提交于 2019-12-05 13:14:48
The objective of my code is to get each seperate word from a txt file and put it into a list and then making a binary search tree using that list to count the frequency of each word and printing each word in alphabetical order along with its frequency. Each word in the can only contain letters, numbers, -, or ' The part that I am unable to do with my beginner programming knowledge is to make the Binary Search Tree using the list I have (I am only able to insert the whole list in one Node instead of putting each word in a Node to make the tree). The code I have so far is this: def read_words

delete node in binary search tree python

雨燕双飞 提交于 2019-12-05 11:25:14
The code below is my implement for my binary search tree, and I want to implement delete method to remove the node. Below is my implementation, but when I perform bst = BSTRee() bst.insert(5) bst.insert(11) bst.insert(3) bst.insert(4) bst.insert(12) bst.insert(2) bst.delete(3) when I call delete method, it did nothing. Can someone help me to fix it. The link below is my code on github. Thank you so much for your help. https://github.com/hly189/sort/blob/master/tree/BST.py class BSTreeNode def ____init__(self, value): self.value = value self.left = None self.right = None def insert(self,key):

Recursive insertion of BST

末鹿安然 提交于 2019-12-05 10:32:07
I have made a function for insertion in BST using loops and it is working perfectly fine. Now, when iam writing to do it using recursion i don't know why it's not working properly, however the logic is correct according to me. It seems that no newnode is being added to the BST tree and head of the tree after coming out of the insertion function is again becoming NULL. #include <iostream> using namespace std; class node{ public: int data; node *right; node *left; node(){ data=0; right=NULL; left=NULL; } }; class tree{ node *head; int maxheight; void delete_tree(node *root); public: tree(){head

BST from Preorder by just inserting the nodes in same order

拜拜、爱过 提交于 2019-12-05 06:29:18
问题 To construct a BST from the preorder traversal given, if I try to insert in the BST in the same order as given in preorder, I get the BST. So, we don't to create the in-order by sorting of the elements or perform any other alogrithm? Is there an example which shows that tree can't be constructed by just inserting the elements ? 回答1: You're correct... you can just insert the nodes in the order given by a preorder traversal to rebuild the tree. The first node inserted must be placed at the

Balancing a binary search tree

限于喜欢 提交于 2019-12-05 04:50:33
Ok, I am trying to get a binary search tree to balance, and I know why it's not working, but I don't know how to fix it. This is what I have for my balancing methods. public void balance(){ if(isEmpty()){ System.out.println("Empty Tree"); return; } if(!isEmpty()){ values = new Object[count()]; index = 0; createAscendingArray(root); clear(); balanceRecursive(0, index); values = null; } } private void createAscendingArray(TreeNode<E> current){ if(current == null) return; if(current.getLeftNode() != null) createAscendingArray(current.getLeftNode()); else if(current.getRightNode() != null)

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

家住魔仙堡 提交于 2019-12-05 00:26:57
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 implemented as follows - when inserting element x in the balanced binary search tree, we can also

In Big-O notation for tree structures: Why do some sources refer to O(logN) and some to O(h)?

雨燕双飞 提交于 2019-12-05 00:26:33
In researching complexity for any algorithm that traverses a binary search tree, I see two different ways to express the same thing: Version #1: The traversal algorithm at worst case compares once per height of the tree; therefore complexity is O(h) . Version #2: The traversal algorithm at worst case compares once per height of the tree; therefore complexity is O(logN) . It seems to me that the same logic is at work, yet different authors use either logN or h . Can someone explain to me why this is the case? If your binary tree is balanced so that every node has exactly two child nodes, then

Remove recursively from a binary search tree

冷暖自知 提交于 2019-12-04 23:41:46
This is homework; please don't just give me code I have two methods: remove(T data) and removeRec(Node<T> node, T data) . In its current state, it seems my code only removes the root node of the BST. @Override public T remove(T data) { if (data == null) { throw new IllegalArgumentException("Data is null"); } if (root == null) { throw new java.util.NoSuchElementException("BST is empty"); } else { size--; BSTNode<T> dummy = new BSTNode<T>(null); return removeRec(root, data, dummy).getData(); //This is probably wrong too } } /** * Helper method to recursively search for, and remove the BSTNode

A sequence that forms the same AVL and splay trees?

自古美人都是妖i 提交于 2019-12-04 22:51:17
问题 Is there such a sequence of numbers (1-7, all numbers used, only once each), that would form equal AVL and splay tree? 回答1: Well, in the interests of science, I implemented both AVL and splay trees in Python based on their respective Wikipedia articles. Assuming I didn't make a mistake somewhere, my finding is that there are no permutations of {1, ..., 7} that produce the same AVL and splay tree . I conjecture the same is true for all sets of size k > 3. As to the fundamental reasons for this