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
3 votes
3 answers
264 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 ...
  • 92.9k
4 votes
2 answers
402 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 ...
  • 92.9k
0 votes
0 answers
42 views

Traversing a 2D array with a tree

In order to help me solve the December Jane Street puzzle "Die Agony" I wrote a quick and dirty script to manually brute-force through the thing and do the calculations for me. I then tried ...
3 votes
1 answer
332 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 ...
3 votes
2 answers
130 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: ...
0 votes
0 answers
33 views

Java pathfinding in general trees - Part 2/4: algorithms

Java pathfinding in general trees - Part 1/4: model classes Java pathfinding in general trees - Part 2/4: algorithms (this post) Java pathfinding in general trees - Part 3/4: demonstration Java ...
  • 26.5k
0 votes
0 answers
54 views

Java pathfinding in general trees - Part 1/4: model classes

Java pathfinding in general trees - Part 1/4: model classes (this post) Java pathfinding in general trees - Part 2/4: algorithms Java pathfinding in general trees - Part 3/4: demonstration Java ...
  • 26.5k
2 votes
1 answer
117 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 ...
  • 123
4 votes
3 answers
145 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 ...
  • 1,653
1 vote
1 answer
100 views

Is there a better way to traverse a list of tree nodes efficiently

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 ...
1 vote
1 answer
73 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 ...
  • 26.5k
1 vote
1 answer
79 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 ...
  • 269
2 votes
2 answers
45 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+...
  • 21
1 vote
1 answer
81 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 ...
1 vote
0 answers
583 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 ...
  • 3,758
2 votes
0 answers
155 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 ...
  • 1,835
1 vote
1 answer
59 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. ...
  • 26.5k
0 votes
1 answer
194 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 ...
  • 11
3 votes
1 answer
249 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 ...
  • 131
1 vote
1 answer
166 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: ...
  • 26.5k
5 votes
0 answers
561 views

Tree for PowerShell

This Module started as a fun coding exercise designed to emulate the tree command and also calculate the size of each folder based of the files size of that folder (...
2 votes
1 answer
104 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 ...
-1 votes
1 answer
48 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/...
0 votes
1 answer
158 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 ...
2 votes
1 answer
102 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 ...
1 vote
1 answer
718 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): ...
3 votes
1 answer
85 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 ...
3 votes
1 answer
88 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 ...
  • 413
2 votes
1 answer
139 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) ...
  • 121
2 votes
1 answer
102 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, ...
  • 125
2 votes
1 answer
75 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 ...
2 votes
1 answer
166 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 ...
  • 293
2 votes
2 answers
64 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 ...
  • 273
1 vote
0 answers
29 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/...
3 votes
1 answer
39 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], [...
5 votes
1 answer
197 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 ...
2 votes
2 answers
132 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. <...
  • 435
4 votes
1 answer
75 views

A Concurrent Tree implementation with consistent snapshot capability

I have a tree in my application from which I need to periodically take a snapshot. I've been using locks so far, but after running into a few deadlock issues I decided to create a lock-free ...
4 votes
4 answers
1k views

Check if a binary tree is symmetric in Python

I have solved the following Leetcode problem. Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmetric....
  • 169
9 votes
1 answer
393 views

Construction and search of Kd-Tree in c++

I am not very fluent in C++ and want to construct a Kd-Tree in C++ based on this paper Efficient Locally Weighted Polynomial Regression Predictions, to use it to conduct some fast implementations of ...
2 votes
1 answer
108 views

HackerRank - Binary tree to compute the Nth power of natural numbers that sum up to X

I'm trying to solve this challenge on HackerRank. In short, given x and n, I have to determine how many ways I can pick numbers ...
  • 527
9 votes
1 answer
954 views

Using smart pointers to construct Binary Search Tree

I have three main questions: Am I using std::unique_ptr correctly here? Using std::move and ...
  • 375
6 votes
1 answer
590 views

C++ : Red-Black Tree with std::unique_ptr

This is my C++ implementation of a Red-Black Tree, referring the CLRS book. Half for fun, half for studying. As implementing child node, I chose std::unique_ptr ...
  • 1,653
2 votes
0 answers
39 views

Find Invalid User Relationships in User to Manager Map (validate map as valid tree structure with no circular relationships)

We have a new service where an admin can upload a spreadsheet of new users they want to add to our application. During our processing we make a union map of all the users both existing and new ...
2 votes
0 answers
55 views

Linked tree built from (n) leaves

Firstly, here is the relevant code. It is written in kotlin. ...
8 votes
1 answer
2k views

Red-Black Tree Implementation in Python

I'm wondering if there are better practices, and optimizations I could make on my code, to do things easier/faster. I have added comments, in many places in the code, to make understanding easier. <...
user avatar
3 votes
1 answer
53 views

XML Tree Reformatter requested code. C#

So this is a post as per request in a different post on stack overflow: https://stackoverflow.com/questions/62412713/why-does-my-c-sharp-xml-code-only-work-when-i-enumerate-variable-enumerable. The ...
  • 31
2 votes
1 answer
293 views

Sum of all Paths in Binary Tree

Problem For the given binary tree return the list which has sum of every paths in a tree. i.e Every path from root to leaf. I've written following solution. ...
3 votes
0 answers
252 views

Noughts and Crosses Terminal Game with AI

I created a game which can play tic-tac-toe with: 0 players (AI vs AI), 1 player (human vs AI) or 2 player (human vs human). Here is the AI function which wraps around my innermost function. This ...
4 votes
1 answer
119 views

AVL Tree implementation Based on VisualGo

I have tried implementing an AVL Tree on my own, based on visualising it. But i'm unsure how many testcases it work with, and how efficient it is. Are there any ways to make it efficient, and compact? ...
  • 1,204

1
2 3 4 5
18