Skip to main content
Share Your Experience: Take the 2024 Developer Survey

Questions tagged [tree]

A tree is a graph in which there is exactly one path between any two nodes. It is often used as a hierarchical data structure.

Filter by
Sorted by
Tagged with
2 votes
0 answers
63 views

Rust implementation of a BTree

I'm studying rust, and I decided to implement a BTree as a way of learning the language. Can anyone give me suggestions on the code? As the language has no inheritance, and we must replace it with ...
Lucas Paixão's user avatar
5 votes
1 answer
106 views

Making sklearn's decision trees easier to traverse

scikit-learn's decision tree structure is difficult for me to navigate. I would prefer to have functionality like tree.left, ...
Steven Gubkin's user avatar
5 votes
1 answer
325 views

Leetcode: Largest Number

I was trying out leetcode's Largest Number. As per the challenge's description: Given a list of non-negative integers nums, arrange them such that they form the largest number and return it. Since ...
ccot's user avatar
  • 341
2 votes
2 answers
657 views

Binary tree deepcopy using the Morris traversal in C

I need to perform a deep copy of a binary tree using the Morris traversal. I think I managed to do it, that is the code below returns what I expect; however, I am not 100% sure I have covered every ...
Slav's user avatar
  • 121
1 vote
3 answers
120 views

Maximum Sum BST in a Binary Tree

I was trying to find the Maximum sum BST of a binary tree on LeetCode. While the Java code I have come up with, mostly seems right and is able to pass 55 out of the given 59 test cases too, the error ...
Siddharth Garg's user avatar
5 votes
2 answers
835 views

Simple tree dictionary structure in C

Good morning, everyone. I have developed a C library whose goal is to store strings in an easily searchable tree structure (which I have dubbed tree-dictionary). I come from a math background and I ...
francescoriccardocrescenzi's user avatar
2 votes
1 answer
78 views

Semi-dynamic range minimum query (RMQ) tree in Java

Introduction I have this semi-dynamic range minimum query (RMQ) tree in Java. It is called semi-dynamic due to the fact that it cannot be modified after it is constructed. However, the values ...
coderodde's user avatar
  • 28.5k
3 votes
0 answers
48 views

Assign unique identifiers to binary matrices using a binary tree

Context I want to build a transposition table where the game states are binary square matrices which size MATRIX_SIZE is known at compile time. Rather than storing ...
Tristan Nemoz's user avatar
4 votes
1 answer
76 views

Hashing a fixed size binary stream with a tree structure in Rust

For a research project, I want to assign a unique identifier to a stream of bits. I can assume that all the streams will always have the same size. Because of this nice property, my idea has been to ...
Tristan Nemoz's user avatar
2 votes
2 answers
123 views

Binary search tree with Iterators

One more BST implementation with iterators added. Did not manage to find another implementation with iterators and it is my first attempt at implementing them so thought to check on your opinion: <...
acd's user avatar
  • 107
6 votes
2 answers
1k views

Avoiding memory leaks and using pointers the right way in my binary search tree implementation - C++

I'm learning C++ and I wanted to test my knowledge by implementing a Binary Search Tree using struct (I know, I’m using C++ I should use classes but I want to keep ...
Neptune Luke's user avatar
5 votes
2 answers
458 views

Optimizing a node search method given a 2-D spatial point

I have a tree-like structure called grid. I have designed it as a structured numpy array. Each element of grid is a tree-node. ...
matts's user avatar
  • 53
3 votes
1 answer
126 views

Parallel semi-sequential bottom-up tree traversal algorithm

I'm trying to traverse a tree-like structure from the bottom-up, and in parallel. Due to some other constraints I also have to allow for thread counts that may be lower than the width of a given set ...
Goubermouche's user avatar
4 votes
1 answer
155 views

AutoComplete-Esque Tree

I've written a tree data structure that takes in input from the user, and stores the each letter based on the previous letter in the text. I'm not sure what this is called, but the closest I can find ...
Linny's user avatar
  • 10.1k
2 votes
1 answer
128 views

QuadTree implementation in c++

I'm new on C++ and OOP. I wrote the following QuadTree implementation (I didn't wrote the remove object part yet). please take a look on it and provide feedback, mainly about C++ OOP design, ...
Joseph Hod's user avatar
5 votes
1 answer
328 views

Port Node and TreeBuilder from Python to C++

I am trying to port a TreeBuilder Python class to C++, keeping the structure as close as possible to the original. Here is a simplified Python version: https://onlinegdb.com/I4dg0hCtg The purpose of ...
Paolo's user avatar
  • 63
3 votes
3 answers
405 views

Leet Code: 236. Lowest Common Ancestor of a Binary Tree. First version

Trying my hand at Leet 236 Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is ...
Martin York's user avatar
  • 95.2k
4 votes
2 answers
455 views

Leet Code: 236. Lowest Common Ancestor of a Binary Tree

Trying my hand at Leet 236. Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is ...
Martin York's user avatar
  • 95.2k
3 votes
1 answer
390 views

Converting String to Binary Hash Tree

I've written a pretty basic algorithm to convert a string to a binary hash tree of its characters, where each character represents a leaf node. The hash tree itself is being stored in a vector. If the ...
FamousAv8er's user avatar
3 votes
2 answers
391 views

Implementation of a market that matches bids to offers

This is my task: Each line in the file can be one of the following: Updates to the limit order book in the following format: ...
Данил Денк's user avatar
2 votes
1 answer
119 views

function to group data by parent branches

I wrote this function to take database output like from PDO's fetchAll(PDO::FETCH_ASSOC) and turn it into a tree. For small datasets it works well. However, when ...
aswine's user avatar
  • 123
4 votes
3 answers
256 views

C++ B-Tree serialization/deserialization

Yesterday I wrote a serialization/deserialization for my B-Tree library. Since the code for whole tree is too large to upload here, I'll upload only serialization/deserialization and node class ...
frozenca's user avatar
  • 1,703
2 votes
1 answer
732 views

Traverse a list of tree nodes

I am wondering if there is a better way to traverse a list of trees. Here is my current algorithm: Collect the parent IDs in a Map Traverse all the groups and find ones where their ID is not in the ...
Mr. Polywhirl's user avatar
1 vote
1 answer
83 views

Java - Select the maximum integers from sliding windows over an array

So I have the following algorithm. Given an array A[0 ... n - 1] and the window length w, compute all the windows ...
coderodde's user avatar
  • 28.5k
1 vote
1 answer
353 views

Binary Search Tree implementation in C++ with templates

Im implementing a BST using templates and dynamic memory, here is my attempt: This is the BSTNode.hh ...
Norhther's user avatar
  • 269
2 votes
2 answers
54 views

Simple non balancing indexed binary search tree

The following code represents a simple indexed binary search tree of integers. The insert method inserts an int. The indexed lookup looks up the ith smallest element (0 indexed). I'm trying to write C+...
ha1234's user avatar
  • 21
1 vote
1 answer
104 views

Decision Tree for classification tasks in Python

I've decided to implement the ID3 Decision Tree algorithm in Python based on what I've learned from George F. Luger's textbook on AI (and other secondary readings). As far as I know, the code is ...
frix's user avatar
  • 357
1 vote
0 answers
2k views

Replacing multiple substrings in C#

There is a String.Replace method in C# and it is 1-1 mapping replacing operation, i.e. single Unicode character / string source replaced with another single Unicode ...
JimmyHu's user avatar
  • 5,060
3 votes
0 answers
336 views

Simple Merkle Tree

Discussion I just started learning about Merkle Trees and wanted to get some practice by attempting to implement a very simple Merkle Tree. The constructor for the tree takes some type of hash ...
Jae Bradley's user avatar
  • 1,845
1 vote
1 answer
66 views

Comparing two general LCA algorithms in Java

Now I have two algorithms solving a problem: given a general (multi-way) tree, and any array of node names, find the the node that is the deepest common ancestor. ...
coderodde's user avatar
  • 28.5k
0 votes
1 answer
504 views

Populate TreeView with arbitrary depth from DataTable

I have a DataTable with id, parent, label columns and want to populate a TreeView with it. I'm new to c# and as far as I understand, there is no ready solution for it, unlike some other languages. So ...
Ayb's user avatar
  • 11
3 votes
1 answer
500 views

Barnes-Hut \$n\$-body simulation (3D) in C++

I have implemented the Barnes-Hut algorithm for \$n\$-body-simulations (in this case "sort-of" 3D-gravity - see below). I'd appreciate any comments for improving my code (especially ...
Paul Aner's user avatar
  • 131
1 vote
1 answer
210 views

Comparing a prefix tree against a HashSet of strings in Java

Here, I have made an attempt to find out whether a prefix tree is any more efficient than a simple java.util.HashSet<String>. See what I have: ...
coderodde's user avatar
  • 28.5k
9 votes
0 answers
1k views

Tree for PowerShell

For anyone reading this old question, this module has evolved and improved a lot since then. It has been fully rewritten in C# and uploaded to the PowerShell Gallery! If you would like to try it out: <...
Santiago Squarzon's user avatar
2 votes
1 answer
206 views

A Lowest Common Ancestor (LCA) Tree Algorithm

My implementation of the lowest common ancestor (LCA) algorithm for my tree data structure. I selected the non-preprocessed (naïve) implementation for this first implementation. Support for any number ...
François Carouge's user avatar
0 votes
1 answer
54 views

is my Prim's MST implementation terse enough?

I did research because I want to learn Kruskal's and Prim's MSTs. I did research and after a solid understanding, I went to my textbook and got their libraries: https://algs4.cs.princeton.edu/code/...
William Golovlev's user avatar
0 votes
1 answer
205 views

Leetcode 662. Maximum Width of Binary Tree Javascript FlatMap attempt - Time Limit Exceeded

Problem Statement: Given the root of a binary tree, return the maximum width of the given tree. The maximum width of a tree is the maximum width among all levels. The width of one level is defined as ...
Prashin Jeevaganth's user avatar
2 votes
1 answer
122 views

Find ALL duplicate subtrees. using recursion

This is just a practice exercise, I'm trying to understand dynamic programming as deeply as I can. I solved this using recursion, though I am not sure if it will always work. If anyone could tell me a ...
beatmaister's user avatar
1 vote
1 answer
1k views

How does one write DFS in python with a Deque without reversed for Trees?

I have implemented a correct version of DFS (with comments explaining the implementation): ...
Charlie Parker's user avatar
3 votes
1 answer
104 views

Building a tree containing all permutations of arithmetic expressions given some numbers

Given any series of numbers, for example 1,2,3,4,5 and given the arithmetic operands + - * / including parenthesis, what would be the most optimal way to build a tree that contains all permutations ...
Mihael Keehl's user avatar
3 votes
1 answer
105 views

AVL Tree in C Criticism

I have made an AVL tree in C and coded the basic functionality of insertion, deletion, and search. I would love some criticism on my implementation especially on the insertion and deletion section of ...
CLox's user avatar
  • 419
2 votes
1 answer
163 views

Monte Carlo Tree Search Optimization and Loss Prevention

I'm working on an implementation of Monte Carlo Tree Search in Swift. It's not bad, but it could be better! I'm principally interested in making my algorithm: faster (more iterations/second) ...
emehex's user avatar
  • 131
2 votes
1 answer
128 views

Haskell Dynamic Programming on a Tree

Here is my shot at Homework 8 of CIS-194 (from Spring '13). The problem is about a hierarchy of employees at a company (a tree), each node being of type Employee, ...
P.K.'s user avatar
  • 125
2 votes
1 answer
103 views

Pretty print a tree from minimal data input

I'm currently working on an uncrafting tool for minecraft and got a neat output working that shows all the materials needed in a tree. I wonder if the code used to print that tree could be improved ...
mindoverflow's user avatar
2 votes
1 answer
192 views

Implementation of binary tree traversal

Based on what i understood of Binary Tree, queue and recursion I implemented this Breadth First Search algorithm as follows. Do you have any suggestions to improve (in term of readability, good ...
curious's user avatar
  • 313
2 votes
2 answers
99 views

Code structure for tree representation with multiple types (classes) of nodes and circular dependencies

I am developing a piece of code around a tree structure. The tree can do a few things - one of them being its ability to serialize & de-serialize its data. There are multiple different types of ...
s-m-e's user avatar
  • 273
1 vote
0 answers
37 views

Set operations on a tree / Hierarchical set class

Initially, I posted this question on StackOverflow. I wanted to know if there's an existing library that implements the concept I have in mind, which is the following. I want to define a hierarchy/...
Hlib Babii's user avatar
3 votes
1 answer
41 views

Counting unival subtrees - Follow Up

This code is a revised version of the implementation which asked for an advice on improvement. Original post here: Counting unival subtrees Credits to: [Deduplicator], [Reinderien], [Toby Speight], [...
Erdenebat Ulziisaikhan's user avatar
5 votes
1 answer
262 views

Implementing an autocomplete system

Question: Implement an autocomplete system. That is, given a query string s and a set of all possible query strings, return all strings in the set that have s as a prefix. For example, given the query ...
Erdenebat Ulziisaikhan's user avatar
2 votes
2 answers
199 views

Lowest Common Ancestor in Binary Tree (Iterative)

In the below code I've implemented a method to find the lowest common ancestor of a binary tree. This is an iterative approach using this pseudocode. Please suggest any improvements that can be made. <...
Saurabh's user avatar
  • 435

1
2 3 4 5
18