binary-search-tree

Sorting an array using a Binary Search Tree in C++

空扰寡人 提交于 2021-02-11 14:11:07
问题 I'm trying to write a program that sorts integer elements of an array, using a Binary Search Tree(BST) as support data structure. The idea is that once the array is given, then it is possible to use a BST to sort his element; for example: if my array is: {120, 30, 115, 40, 50, 100, 70} then I build a BST like this: Once I have this BST, I can do an inorder tree traversal to touch every node in order, from the lowest to the highest element and modify the array. The result would be a sorted

Sorting an array using a Binary Search Tree in C++

强颜欢笑 提交于 2021-02-11 14:08:18
问题 I'm trying to write a program that sorts integer elements of an array, using a Binary Search Tree(BST) as support data structure. The idea is that once the array is given, then it is possible to use a BST to sort his element; for example: if my array is: {120, 30, 115, 40, 50, 100, 70} then I build a BST like this: Once I have this BST, I can do an inorder tree traversal to touch every node in order, from the lowest to the highest element and modify the array. The result would be a sorted

Generating an optimal binary search tree (Cormen)

我怕爱的太早我们不能终老 提交于 2021-02-08 13:53:15
问题 I'm reading Cormen et al., Introduction to Algorithms (3rd ed.) (PDF), section 15.4 on optimal binary search trees, but am having some trouble implementing the pseudocode for the optimal_bst function in Python. Here is the example I'm trying to apply the optimal BST to: Let us define e[i,j] as the expected cost of searching an optimal binary search tree containing the keys labeled from i to j . Ultimately, we wish to compute e[1, n] , where n is the number of keys (5 in this example). The

What is the time complexity of constructing a binary search tree?

安稳与你 提交于 2021-02-08 04:54:29
问题 "Every comparison-based algorithm to sort n elements must take Ω(nlogn) comparisons in the worst case. With this fact, what would be the complexity of constructing a n-node binary search tree and why?" Based on this question, I am thinking that the construction complexity must be at least O(nlogn). That said, I can't seem to figure out how to find the total complexity of construction. 回答1: The title of the question and the text you quote are asking different things. I am going to address what

Binary Tree: summation of all nodes between a min and max value

旧巷老猫 提交于 2021-01-29 16:46:00
问题 I have an assignment where I am given the root of a randomly generated BST. I am given randomly generated test cases for this assignment. The assignment description is the following: You are given the root node of a binary search tree, T, and two integers: min, and max. Note that min and max are not necessarily stored in the tree. Determine the sum of all keys stored in T that are larger than or equal to min, and smaller than or equal to max. Implement your algorithm recursively I am not

What is the most efficient way to implement a BST in such a way the find(value) function is optimized for random values in the tree on x86? [closed]

梦想的初衷 提交于 2021-01-29 11:39:28
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 1 year ago . Improve this question Suppose we have a binary search tree of depth $n$, where every level is full, we know in advance how big the tree will be, and we will run the find(value) function a lot. The values(integers) we will want to find will be uniformly random across the acceptable

I don't understand how to move portions of a tree

蹲街弑〆低调 提交于 2021-01-28 05:25:49
问题 My goal is to create a BST that: when I insert a node I insert it at the root, if the element to insert already exists move the existing element to the root. If you remove a node, move the father of that node to the root. class BSTNode { BSTNode left = null; BSTNode rigth = null; int data = 0; public BSTNode() { super(); } public BSTNode(int data) { this.left = null; this.rigth = null; this.data = data; } @Override public String toString() { return "BSTNode [left=" + left + ", rigth=" + rigth

Binary heap - Find number of nodes at a height

本秂侑毒 提交于 2021-01-28 03:03:57
问题 I have been struggling with this for several hours now, and I can't seem to find the answers here either. (there are many posts about Binary Heap, but I did not this particular problem). The problem is: For a Binary Heap with 1492 nodes, the number of nodes of height two is _ 187 _. I understand that with 1492 nodes, the binary heap has the depth log(1492)/log(2) = 10 height two should have 2^(10-2) nodes which should be 256 Why is the answer 187? Thank you 回答1: In case someone needs to know.

Binary search tree filter values in a range

这一生的挚爱 提交于 2021-01-27 20:19:22
问题 I have a tree(RBT) consisting of N elements. Let's imagine I have this tree (N = 7): 4 2 6 1 3 5 7 How do I filter values in some range (e.g. print all values between 3 and 6) with better performance than O(N)? Is there any specific algorithm? I'm imagining it something like find position of value 3 [log(N)], somehow continue until you get to the 6 [O(M)]. 回答1: If you have Sedgevick's Algorithms, 4 ed., look at the end of chapter 3.2 on BST's. Also book companion has implementation in Java.

Scheme: Remove the maximum from a BST

筅森魡賤 提交于 2021-01-20 12:18:06
问题 Good afternoon everyone, I have very odd question and I'm sorry If I have posted in a wrong part of the forum. I'm trying to understand remove-max function (provided bellow) from a BST written in scheme, but I have a hard time grasping the ideas in it. I know the Scheme syntax, however there is a lot of going on in this function and thus I am a bit confused. (define removemax-BST (lambda (T) (cond ((null? (caddr t)) (cons (cadr t) (car t)) (else (let ((r (removemax-BST (caddr t)))) (cons