543
questions
1
vote
2
answers
111
views
How does heapify maintain max-heap property when both children are larger than the root and their children are also greater than their parents?
I am currently learning about heap sort and I am having trouble understanding the heapify process, particularly when both children of the root are larger than the root itself, and the sub-children (...
0
votes
2
answers
45
views
How to Optimize a Deeply Nested Object Search for Performance?
I have a deeply nested JavaScript object structure representing a hierarchical data model. The object can have multiple levels of nested children, and I need to search for a specific value within this ...
1
vote
1
answer
107
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 ...
2
votes
0
answers
74
views
Graph algorithm representation: Breadth-first search data structure [closed]
I'm completing the datastructures and algorithms preparatory course for a masters in data science.
Here, is a breadth-first search algorithm for a graph I wrote based on the pseudocode provided in the ...
1
vote
1
answer
71
views
R Tidyverse recursive function path creation issue
In the context of the bill of materials, I have a tree structure and I want to perform a recursive function to find the different paths of this tree.
I did, however, a mistake when trying to define a ...
0
votes
2
answers
63
views
How can I prevent the stack overflow error?
I am working on a Java project to involving quadtrees.
In this representation of the quadtree, the node with intensity -1 has 4 children and the node with any other intensity does not have children. I ...
1
vote
1
answer
39
views
Wrong output when checking whether binary tree is balanced
I am trying to solve LeetCode problem 110. Balanced Binary Tree:
Given a binary tree, determine if it is height-balanced.
Here is my attempt:
class Solution {
boolean c = true;
public ...
2
votes
1
answer
79
views
Detecting shared structure in tree made of cons cells
I'm writing a programming language (details irrelevant) which uses Lisp-like cons cells to store its data (this makes implementing the garbage collector easy). I'll spare you the details of everything ...
0
votes
1
answer
70
views
How recursion works in case of permutations after the base case reached
When the base case is reached then for next combination call we re-swap how it is taking in to account the not visited or considered cases.
For example
function dfs(i, nums, slate) {
if (i === ...
0
votes
0
answers
89
views
F# recursive bind with "and" gives "null", doesn't work
This is a brief example from the book, by Tomas Petricek, "Functional programming in the real world", chapter 8.
type QueryInfo =
{ Title: string
Check: Client -> bool
...
-2
votes
1
answer
86
views
Is there a way to fix this code or should I ask to change the data structure?
I'm loosing my mind with this (because I'm not a very logic person).
RUST Playground
I cannot change the data part.
The string returned from the below code is A = 1 AND (B = 2 OR C = 3 OR D = 4 OR ) ...
0
votes
0
answers
82
views
Recursively defining a recursive model in Prisma (Postgres)
The API I use sometimes returns recursive data:
{
"chain": {
"details": "0",
"subChains": [
{
"details": "1",
...
0
votes
1
answer
53
views
instance of parent Class not accessible from subclass? "does not exist in current context"
So I'm fairly new to all this. I've got the beginnings of a simple Octree implementation for a Unity project. This consists of an Octree parent class, an instance of it, and a OctreeNode subclass
I'm ...
0
votes
1
answer
135
views
Some confusion regarding the Binary tree Property about the Number of external nodes = Number of Internal Node + 1
Everybody would agree that this one, shown below, is a valid Binary tree.
* (R)
\
\
* (C)
The above binary tree node marked R, is the root and the node marked C is the child.
This is not a Full ...
0
votes
1
answer
29
views
Implementing a recursive function to execute layered calculations
Apologies for the butchered title.
I have a db of metrics where I have fields like Metric Title, Metric ID, Metric Abbreviation, and Metric Formula. The Metric Formula indicates if the metric requires ...
1
vote
1
answer
99
views
Finding time complexity of T(n) = 2T(n/2) + Logn
T(n) = 2T(n/2) + Logn
This is the given time complexity and I am using recursive tree method to find it's time complexity.
For the first call it is doing : Log(n) work
For second it is doing: Log(n/2) ...
0
votes
3
answers
104
views
Appending two lists mutually to each other gives unexpected results containing "[...]"
I have 2 lists:
a = [1, 2, 3, 4, 5]
b = [9, 8, 7]
I performed the following two operations and tried printing each list
a.append(b)
b.append(a)
print(a) # Expected Result: [1, 2, 3, 4, 5, [9, 8, 7]]
...
0
votes
0
answers
30
views
recursive function iterate over python object
I have developed a system with different classes. Some classes are attributes of other classes, such as:
@dataclass
class MyFirst:
name: str
numb: float
time: pd.Series
my_second: MySecond ...
0
votes
1
answer
79
views
finding cycles in directed graph using algorithm design manual
so, i am following algorithm design but the below algorithm implemented from the book, fails in the following e.g., is there an issue with this algo -
bool directed = true;
bool dfs(...
0
votes
1
answer
65
views
boost spirit x3 variant Recursive assignment problem
In special scenarios
I have this data structure
namespace x3 = boost::spirit::x3;
struct one;
struct two : x3::variant<x3::forward_ast<one>,int>{
using base_type::base_type;
using ...
1
vote
1
answer
32
views
Python recursive function to generate a specific type of sequence from a specific type of dictionary
I need to write an algorithm where I have a python dictionary like {1: [2, 6], 2: [3], 6: [5], 3: [4], 5: [8, 9], 4: [5, 7]} and generate the sequence like [[1, 2, 3, 4, 5,8],[1, 2, 3, 4, 5,9], [1,2,...
0
votes
1
answer
239
views
Leetcode question: Flatten binary tree into linkedlist
I am trying to solve this problem on leetcode: https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ (Flatten binary tree into linkedlist)
This is my code:
class Solution{
...
0
votes
1
answer
174
views
I can't pinpoint why memory gets corrupted while working with Recursive Tagged Union in Zig
I am working on a small feature-incomplete Lisp interpreter to learn a bit of Zig. My inspiration is this https://norvig.com/lispy.html, this might not be the most idiomatic way of implementing that ...
0
votes
0
answers
28
views
DSA Binary Search Tree - Insert function not working fully
I'm working through binary search trees in The Odin Project (TOP)(https://www.theodinproject.com/lessons/javascript-binary-search-trees) and I'm having trouble with the insert function.
I believe the ...
2
votes
1
answer
314
views
Interface the BTreeMap and HashMap in Rust
I am implementing a tree data structure. My idea to do this is to have a struct Tree that have children:SomeMapType<Tree> field to keep track of its children nodes, where SomeMapType may be ...
-2
votes
1
answer
59
views
How return works in recursion call in python?
I've written a python function to reverse a list, here are the two functions and their respective outputs.
Function 1
a=[1,5]
def rev(k,i):
if i==len(k)-1:
print("in base case {}&...
0
votes
1
answer
68
views
How to save previous "output" or a state of the previous output in a recursive function call in python?
I am using a recursive function to generate text using a RegEx match, where it finds a pattern of words according to a combination of synonyms inside square brackets (pattern = '\[.*?\]') separated by ...
2
votes
1
answer
314
views
having problems inserting words on a patricia/radix tree
i am trying to make a code for a college aplication of a patricia/radix tree that inserts words read from each line in a txt file in the tree, so if i read a file that says
roman
romance
romantic
its ...
1
vote
1
answer
121
views
Mapping recursively from a dataframe to python dictionary
I am struggling to find a recursive mapping to get the end result. Here is the input df
## mapping recursive
import pandas as pd
data = {
"group1": ["A", "A", "B&...
0
votes
1
answer
339
views
Question about Time Complexity analysis of Merge Sort
I'm a student starting to learn about Computer Science, specifically Data Structures and Algorithm.
Been struggling in understanding the Time Complexity of Merge Sort Algorithm, specifically deriving ...
-1
votes
1
answer
98
views
Merge Two Sorted List Leetcode Question 21
So I keep getting a maximum recursion error on line 22, for this test case
list1 is 1,2,4
list2 is 1,3,4
Output is 1,1,2,3,4,4
can someone explain what is causing this infinite loop to occur, python ...
1
vote
1
answer
286
views
How to implement fmt::Display on a recursive data structure in Rust
I tried to provide an implementation of the Display trait on the recursive data structure Tree as defined below.
#[derive(Debug)]
struct Tree <'a> {
node: (&'a str, Vec<Tree<'a>&...
1
vote
1
answer
88
views
Spring Boot : Infinite recursion
I have the following entities in my Spring boot Hibernate project.
Entities
Task - represents the task / job details
Agent - represents the agents available to work on the tasks
TaskAgent - stores ...
0
votes
0
answers
29
views
R Recursive Function and map() for Bill Data Structure
Using R tibbles, I have a df as shown below. It's a BOM data structure.
df <- data.frame(
product_id= c("P1", "P1", "P1", "P1", "P1", "...
1
vote
2
answers
59
views
How do I make this function automatic, it clearly has a pattern
def populate_kids(self, arr, used_Indices):
for a in self.children:
a.get_all_jumps(arr, used_Indices)
for a in self.children:
for b in a.children:
b.get_all_jumps(...
1
vote
1
answer
84
views
Recursive function to restructure array that inside an object
I'm currently working on displaying tables.
But im stuck because the data structure is super nested. This is the original structure:
{
"key": "parent_table",
"...
1
vote
2
answers
89
views
Recursive function to restructure an array of object
I'm currently working on displaying item.
But im stuck because the data structure is super nested.
This is the original structure:
[
{
"key": "name",
"value": &...
0
votes
1
answer
191
views
Inserting a node in a complete binary tree with python
How to insert a node in a complete binary tree without using queue DS? I tried the following code:
class TreeNode:
def __init__(self, value=None) -> None:
self.left = None
self....
1
vote
1
answer
61
views
Is there a way to sort the values of a DataStructure (Key/Value pairs) by first extracting the values (int) to an array, and performing a MergeSort?
I want to simulate an interview scheduler, where the names with the highest scores are called first. It is my priority to make sure I apply the mergeSort algorithm somehow to this implementation.
What ...
0
votes
0
answers
52
views
solution for jump game problem is not running properly
You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.
Return true if you can ...
0
votes
1
answer
216
views
How to create self-referential AST in Rust?
Let's imagine we have a very simple AST for programming language with only functions and calls
use std::sync::Arc;
struct Function {
pub body: Vec<Call>
}
struct Call {
pub function: ...
1
vote
0
answers
156
views
How do I create an iterator over a recursive data structure in Crystal?
I have a tree structure and currently I am trying to return an iterator that iterates over the elements of the datastructure so that my function can accept a block.
I have currently reduced my code to ...
-1
votes
1
answer
81
views
Inconsistent output in Recursion?
I was solving a question today on Leetcode named Lexicographically Smallest Equivalent String (link) and I came up with a solution of DFS with some customization to solve the problem and here's is my ...
3
votes
2
answers
62
views
Recursive CTE Query to make groups without duplicates
I need to build a sql query to find the least expensive squad (salary-wise) for teams in a European volleyball league.
For each team, the squad consists of 6 players. The entire team has 10 players (...
0
votes
2
answers
210
views
Spy Number using Recursion
def spynum (n,r,sm=0,pd=1):
#s = 0
#p = 1
#b = False
if n == 0:
print (sm==pd)
return sm == pd
else:
rem = n % 10
sm += rem
pd *= rem
print (sm,pd,rem)
spynum(n//10,rem,...
1
vote
1
answer
80
views
Find all possible modifications to a graph
I am now using lists to represent the graph, which would be similar to previous question. I found out that the dict approach would be very long and complex, so decided to go with the list approach. ...
-1
votes
2
answers
62
views
Segmentation fault is on terminal. Its the code to convert string to binary tree
Node * create(Node * root, int I, int J, string str)
{
if (I == J) { root -> data =str[I]; root -> left=NULL; root -> right=NULL; }
int i = 0, j = 0, k = 0, l = 0;
//// to store ...
4
votes
1
answer
110
views
Constructing an infinite, lazy Monad value recursively
As an exercise, I tried to construct an infinite, lazy list, recursively, inside a monad.
Basically, the equivalent of nat, but monadic, monat:
nat :: Int -> [Int]
nat n = n : nat (n+1)
monat :: ...
0
votes
1
answer
128
views
Implement `Eq`/`Ord` for infinitely recursive data type
I want to build NFAs that match strings. I have:
data State = State Char State | Split State State | Final
Note that State can be infinitely recursive on purpose, because I want to be build NFAs like ...
1
vote
1
answer
132
views
get values of unknown hierarchy of lists and dicts
So lets say I have a bunch of data that is not really known how is structured except that it is a combination of lists, dictionaries and string values. And I would like to extract only the string ...