6,395 questions
0
votes
1
answer
168
views
How to use index in simple select
Table has index on name column:
CREATE TABLE firma2.klient
(
kood character(12) primary key,
nimi character(100),
...
);
CREATE INDEX IF NOT EXISTS ...
5
votes
3
answers
225
views
Efficient ways to check if two binary trees are the same
I implemented a recursive solution in Python to check if two binary trees are identical, which traverses nodes recursively and compares values and structure. The time complexity is O(n), space O(h).
...
0
votes
0
answers
42
views
How do I display my BST using my preorder() and inorder() methods?
I'm doing a programming assignment for my class. One of my programs includes creating a BST with Python within a BST class, and I'm trying to display using preorder() and inorder() functions. My ...
6
votes
2
answers
257
views
Prefer container::lower_bound to std::lower_bound?
I understand that container::lower_bound harness helpful invariant of the very container hence gives a better performance than std::lower_bound.
Taking std::set::lower_bound as an example, during my ...
1
vote
1
answer
111
views
How to implement insertion into BST iteratively in Rust?
I'm trying to implement an algorithm for insertion into BST (binary search tree) in Rust. I did it recursively, but am having problems implementing the iterative solution. I'm struggling to borrow a ...
-1
votes
1
answer
73
views
What form of recursion is occurring in the "insert "and "list_all" methods of this code?
I am struggling to understand the recursion going on in the insert, _insert_recursive and _inroder_traversal methods. For context, I know recursion and the concept is not strange to me, I struggle to ...
0
votes
1
answer
83
views
understanding a part of recursion for a binary search tree
Hello I basically need to implement a recursive function for insertion of a binary tree. I already implemented the chunk of insertion function (wether it's largest or smaller than root) but there's an ...
3
votes
1
answer
766
views
Difficulty setting up threaded BST in C++
I'm having trouble implementing a threaded Binary Search Tree in C++. I have a non-threaded tree completely implemented (see below code). What I'm having difficulty with is setting the threads to ...
0
votes
0
answers
62
views
Error When Mutably Borrowing a Value From a Struct That Has Been Mutably Borrowed
Explanation
I'm attempting to implement a binary search tree in Rust. The problem is in the insert method, where I have a &mut Box<Node<T>> called node_to_insert_at. To insert the ...
0
votes
2
answers
116
views
Binary search tree memory leak adding residual child nodes to new trees
I'm having two issues with my code; I'd post them as two separate questions, but I believe one is causing the other. The first involves insertion; from my amateur eyes, the insert code seems to be ...
2
votes
2
answers
100
views
Self-balancing binary search tree and duplicated keys
I've implemented a red-black tree (which is a kind of self-balancing binary search trees) with duplicated keys allowed. In the Insert method I put nodes with equal keys as right child nodes. As you ...
0
votes
0
answers
33
views
Optimizing frequent deletions in a BST: AVL vs. Red-Black Tree?
I am working on optimizing the Delete-Max operation in an unbalanced Binary Search Tree (BST) where frequent max deletions occur. The current approach follows:
Traverse to the rightmost node.
Remove ...
1
vote
1
answer
66
views
Maximum difference between node and its ancestor Java soltion having issue
I am practicing Maximum difference between node and its ancestor Java problem using my own solution which worked fine for smaller test cases. However, this is giving differnt output than expected for ...
2
votes
1
answer
226
views
Data structure for easily finding the maximum length of a non-decreasing sequence on a certain interval
I'm trying to find a solution for this challenge:
Given is n, which is the size of an array filled with ones. There are two types of operations possible on this array:
Increase (x, y, m): add m to ...
0
votes
1
answer
109
views
solution to factorial problem using dynamic programming resulting in more time than simple recursion
I was just starting out with dynamic programming and tried solving factorial problem using the same, i used binary tree for underlying data structure, but when i thought of comparing it with normal ...
1
vote
0
answers
43
views
Removing Root Node from a BST?
I build my tree in main with a for loop adding nodes 1-15, removing say 5 or 15 work fine, i think my problem lies in removing the root. It wont follow pre-order when I call it after removing 1, i ...
0
votes
0
answers
40
views
What am I missing ? BST hidden case failure
Some hidden test cases fail to output correctly. I've tested for all edge cases I can think of; what am I missing?
Edge Cases Tested:
Nonexistent Key Deletion: Assumes input is valid per the problem ...
0
votes
1
answer
74
views
Segmentation fault for inserting into an BST
So for this class here, I have to write out functions, including a recursive find function, an insert function, and a operator[] function, which overall helps insert into a BST
#include <compare>...
1
vote
0
answers
61
views
Pointer inconsistency in modified Queue struct template using AVL
I'm making a modified Queue struct template using AVL tree to allow for moving elements forward. If I use normal linked list implementation, moving a given element forward by x positions will cos O(n),...
3
votes
4
answers
297
views
Can a recursion with nested calls in tail position be tail recursion?
Can a recursion with nested calls in tail position be tail recursion?
For example, I have the following function in Racket that is intended to convert a binary tree, defined as a nested struct below, ...
3
votes
1
answer
150
views
Convert a sorted array into a height-balanced binary search tree by picking middle element -- why does it work?
I was doing an exercise to convert a sorted array to a binary search tree where every node in the tree has its children whose heights at most differ by 1.
One simple solution is to pick out the middle ...
0
votes
1
answer
39
views
Red Black Tree | is this tree balanced?
I have recently begun studying the structure of the red-black tree and am struggling to determine whether it is balanced. And explain why it is still balanced, or vice versa.
...
2
votes
1
answer
172
views
Is the inorder predecessor of a node with no left subtree and being the left child of its parent always its grandparent?
I'm trying to understand the rules for finding the inorder predecessor of a node in a binary search tree (BST).
If a node 𝑥 has a left subtree, the inorder predecessor is the
largest value in that ...
1
vote
1
answer
188
views
Is the Inorder Predecessor of a Node with a Left Subtree Always a Leaf Node in a Binary Search Tree?
In a Binary Search Tree (BST), I am trying to understand the properties of the inorder predecessor, particularly for nodes that have a left subtree.
Definition: The inorder predecessor of a node is ...
-1
votes
1
answer
145
views
Binary Search Tree javascript Array - Data Structures
I am trying to build a binary tree with a array. I want to take the array, find the root and split right and left side, then perform the same split on each side if needed until two numbers are left ...
0
votes
1
answer
56
views
How can an AA-Tree always have 2 children per non-leaf node for arbitrary total-element counts?
I've been reading up on AA-Trees. One of their invariants is that every non-leaf node has two children. How can that work whenever the total element count happens to not be one less than a power of ...
0
votes
0
answers
105
views
How do you insert an interval into a disjoint segment tree in Haskell?
I don't think this is standard terminology, but we will define a "disjoint segment tree" as a segment tree (a flavor of binary search tree) whose values are pairwise-separated intervals (a,b)...
3
votes
1
answer
133
views
Correctness of Deletion algorithm of BST in CLRS
It's not quite obvious to me why the algorithm (see below) to delete a node from a binary search tree, that CLRS offers, works correctly (like how do we know that the inorder arrangement of the nodes ...
0
votes
1
answer
100
views
How is the one-bit AA Tree designed?
In the original "Balanced Search Trees Made Simple" paper by Arne Andersson, the first paragraph of the second chapter mentions that a single bit could be the flag before introducing the ...
1
vote
0
answers
59
views
Binary Search Tree remove node, sometimes it works, sometimes it doesnt
I'm currently working on the odin project on Binary Search Tree. I'm trying to make deleteNode work, and I'm working on removing a node if it is a leaf node.
Here's my code:
class Node {
...
2
votes
1
answer
115
views
Does this algorithm for converting a Binary Search Tree into a sorted linked list on Wikipedia have a bug in it?
I wanted to get a sanity check here. I believe the algorithm listed on the wikipedia page for the Day–Stout–Warren algorithm for balancing BSTs has a problem.
This pseudocode purports to turn a BST ...
2
votes
1
answer
114
views
Why is it impossible to convert a Min Heap to a Binary Search Tree (BST) in O(n) time?
I'm working on formally proving that it's impossible to convert a Min Heap into a BST in O(n) time complexity.
My reasoning is that any algorithm attempting this conversion would need to perform ...
-1
votes
1
answer
88
views
Binary Search Tree Insertion Method in Java [closed]
In the following insert method for a binary search tree (BST), the left side of the tree is being updated correctly, but there is an issue with inserting values on the right side. Despite using the ...
1
vote
1
answer
86
views
Fast Random Access, Update, Insert, Delete Data Structure [closed]
I want an ordered index-able data structure like a python list, which can access, update, insert and delete at arbitrary indices efficiently.
I have modified a skip list and an AVL tree to do the ...
0
votes
0
answers
27
views
Unexpected values in HashSet before method invocation in Java
package main;
import java.io.*;
import java.util.*;
public class IsBSTTree {
Set<Integer> set = new HashSet<>();
public static void main(String[] args) throws IOException {...
1
vote
1
answer
40
views
How can I get array output instead of memory location when printing TreeNode?
I am looking at solutions of LeetCode problem 108. Convert Sorted Array to Binary Search Tree:
Given an integer array nums where the elements are sorted in ascending order, convert it to a height-...
-2
votes
1
answer
123
views
Morris traversal leads to exception on LeetCode: address sanitizer , stack overflow [closed]
I have implemented a Morris traversal to solve LeetCode problem 98. Validate Binary Search Tree:
Given the root of a binary tree, determine if it is a valid binary search tree (BST).
My code
/**
* ...
0
votes
1
answer
115
views
(Scheme) Having trouble with binary search tree in-order traversal
I'm trying to write a Scheme program that performs an in-order traversal of a binary search tree:
(define (inorder lst)
(cond
((null? lst)
'())
((not (pair? lst))
(list lst))
...
1
vote
1
answer
86
views
What is the importance of implementing binary search trees (BST) for our database?
Binary search trees (BST) are helpful to perform CRUD operations efficiently. Do we need to implement a BST in a programming language when storing data in a database?
For example, consider Django + ...
0
votes
5
answers
182
views
why this code's return value doesn't cover all control path?
TreeNode* searchBST(TreeNode* root, int val) {
if(root==nullptr)return nullptr;
if(root->val==val)return root;
if(root->val<val)return searchBST(root->right,val);
...
1
vote
1
answer
123
views
Hashmap using Binary search tree incorrect implementation?
I'm doing some leetcode practice https://leetcode.com/problems/design-hashmap/description/ trying to implement a hashmap in C++ using a binary search tree as the underlying data structure. I'm passing ...
0
votes
1
answer
59
views
What is wrong with my approach of finding an indorder predecessor of a binary search tree?
void inorderPredecessor(Node* root, Node* &pre,int key){
if(root == NULL ) return ;
if(root -> data == key){
inorderPredecessor(root ->left , pre , key);
}else if(root -&...
1
vote
1
answer
196
views
Trying to understand how to search a BST with a given input
I am trying to figure out how to search a BST via a given input. The nodes in the tree have a randomly generated number as the key, and the value of the node is an object with various attributes.
...
2
votes
1
answer
87
views
Implicit Treap building time in O(n) time
I have two algorithms to build an implicity from an array, one is continuously merging
Like so:
public Node nlognConvertToTreap(int[] arr){
Node node = Node.EMPTY_NODE;
for (int x : arr) node =...
1
vote
0
answers
80
views
Cartesian tree index
input
The first line of the input file contains an integer number N -- the number of pairs you should build cartesian tree out of (1 ≤ N ≤ 50 000). The following N lines contain two numbers each -- ...
0
votes
1
answer
52
views
BST Delete node method removes entire subtree. Python
I'm struggling getting my Delete function to work for my BST class, I mostly understand the idea behind but can't quite get the implementation right.
Here in DataStructures.py I have a class for the ...
0
votes
0
answers
52
views
Convert Binary search tree to max heap does not work property while insert each node to root
I need to convert binary search tree to max heap in java. As I did like this,
public static void inOrderTraversal(Node node, Vector<Node> vec)
{
if(node == null)
{
return ;
}
...
1
vote
1
answer
73
views
Segmentation fault while implementing a binary tree in c
I am new to c, but for a project I am implementing a binary tree. here is my code for the functions:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct ...
0
votes
0
answers
91
views
How does this single printf() line affect my result?
I was in the middle of building an AVL Tree until this odd thing happened. When I comment this printf() on line 81, my resulting balance factor shows a random(?) number when its actual BF is 0. But, ...
1
vote
1
answer
306
views
Can someone determine time complexity of this LeetCode solution I made?
I wrote a solution to the problem linked below but I'm not sure about its time complexity. I was thinking it was quadratic but it passed within 0 ms when I submitted, so it might be linear idk. I ...