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
4
votes
1answer
60 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
4answers
863 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....
0
votes
0answers
9 views

Tree design in db with same child twice and value store

I have a non-cyclic tree like the below sample representation. It has nodes and edges connecting them. The tree has the possibility of the same node connected multiple times to one parent. Like in the ...
9
votes
1answer
67 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
1answer
65 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 ...
8
votes
1answer
359 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 ...
5
votes
1answer
95 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
vote
0answers
34 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
0answers
48 views

Linked tree built from (n) leaves

Firstly, here is the relevant code. It is written in kotlin. ...
5
votes
1answer
98 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. <...
3
votes
1answer
41 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 ...
1
vote
1answer
83 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
0answers
33 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
1answer
52 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? ...
2
votes
0answers
45 views

Max Heap Implementation in Python

I decided to challenge myself to code this data structure by just seeing how it looks visually and this is what I got. I can change this easily to minheap by changing some greater than (>) operators. ...
1
vote
2answers
65 views

Leetcode: Cousins in Binary Tree

Mu solution to Leetcode problem Cousins in Binary Tree works, but the code feels bulky. Is there a better way to solve this problem, particularly to use less additional variables? I will appreciate ...
1
vote
1answer
49 views

Data mining in Java: finding undrawn lottery rows - follow-up

(See the previous (initial) iteration.) This time, I have substantially reduced the usage of the final and this keywords. Also, ...
2
votes
2answers
83 views

Data mining in Java: finding undrawn lottery rows

(See the next iteration.) Introduction Suppose Evil Lottery Inc is interested in not paying millions of dollars back to players. They gather the drawn lottery rows first, after which they mine rows ...
1
vote
1answer
61 views

Depth First Search vs Breadth First Search

So after years of teaching myself, I think I'm ready to stand on the shoulders of giants and ask you a good question! I have a tree of nodes. What I want is a function that returns an array of the ...
5
votes
2answers
118 views

Leftist Tree in Dyalog APL — can it be made more compact/idiomatic?

Following great feedback given in response to my previous question, I was encouraged to consider a different type of heap structure, the Leftist Tree. It lends itself better to a functional ...
2
votes
0answers
28 views

Traversing a tree with two types of nodes, while keeping track of all ancestors

I wrote code that stores an AWS Organization tree in a tree data structure in Rust. From the tree, I use a slightly modified preorder traversal algorithm to print all of the accounts in the ...
4
votes
2answers
130 views

Suffix Tree in Python

I was wondering why the following implementation of a Suffix Tree is 2000 times slower than using a similar data structure in C++ (I tested it using python3, pypy3 and pypy2). I know the bottleneck is ...
3
votes
1answer
65 views

LeetCode: Most Frequent Subtree Sum

My solution to LeetCode problem Most Frequent Subtree Sum works, but I have several questions regarding the code. I am also looking for an advice on how to improve the code. Problem: Given the ...
1
vote
1answer
30 views

Synchronize list of trees with DB using DRF serializer

Given the models below: ...
5
votes
2answers
92 views

Binary Tree Preorder Traversal

What is the best way to move _add and _preorder methods in class Node to ...
4
votes
3answers
87 views

LeetCode: Validate Binary Search Tree C#

I was struggling with this question for many times, and I have solved it every time, because I understand the concept. but I can never get to a good concise solution which is easy to follow. In a ...
1
vote
2answers
124 views

Binary tree pruning with BFS

Below the text of the exercise: We are given the head node root of a binary tree, where additionally every node's value is either a 0 or a 1. Return the same tree where every subtree (of the ...
3
votes
1answer
411 views

Parsing Binary Tree Array in Python

I got this from a practice problem set, and looking for feedback on my approach. Suppose you're given a binary tree represented as an array. Write a function that determines whether the left or ...
4
votes
0answers
94 views

JavaScript AVL Tree

I have been working through Introduction to Algorithms 3rd Edition, and have implemented an AVL tree through prototypal inheritance. The code presented here does work as expected based on the tests ...
2
votes
2answers
97 views

Iterative “post order” tree traversal [closed]

I wanted to get feedback on the correctness and efficiency of this post order traversal routine that my wife and I find really easy to grasp. ...
4
votes
1answer
61 views

Manipulate a tree layer wise

I built a tree in Haskell where every node has a number that is unique in its path from root to leaf. Nodes have a dedicated list for children that are leaves. ...
4
votes
3answers
316 views

Binary Search Tree Using Templates in C++

I have made this BST using templates Node.h ...
2
votes
1answer
136 views

Python Fenwick Tree

I am trying to solve this question: https://open.kattis.com/problems/fenwick A Fenwick Tree (also known as a Binary Indexed Tree) is a data structure on an array which enables fast (𝑂(log𝑛)) ...
3
votes
1answer
153 views

F# Binary Tree and Tree Traversal

So for an side exercise on exercism I implemented a BinarySearchTree. I was confident in implementing the creation of the binary tree. But I'm very unsure about the traversal of the tree. So I came ...
5
votes
1answer
470 views

Calculating the average of all nodes in a Binary Search Tree using visitors

I'm required to calculate the average value of all nodes in a BST using a Visitor, I'm not sure if I did this correctly or if i could just calculate the average ...
3
votes
1answer
67 views

Simple tree addition implementation

I am trying to learn Rust and I feel like a dummy every step of the way. I went through the tutorial on making lists in Rust. I am trying to apply the knowledge ...
1
vote
1answer
50 views

Don't include root directory in HTML tree

I've written a function to create an HTML tree from JSON. ...
3
votes
2answers
90 views

Performance Improvement for Subtree for another tree

I was doing this algo on leetcode: https://leetcode.com/problems/subtree-of-another-tree/ Question: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and ...
12
votes
4answers
2k views

Extraction of value from a dict-tree, given a path of arbitrary length

Problem: We read a complex json-file which grants us a tree in terms of a python "dict of dicts". We're then requested to retrieve a specific path from the tree, represented by a list of elements. The ...
1
vote
0answers
38 views

BST range search

I'd like some input on my implementation of bst-range, which follows below. Before we can get to bst-range, we first need some setup code to build the BST. (You ...
10
votes
2answers
827 views

Iterator for traversing a tree [v2]

this is an update of the implementation after I followed the feedback from Iterator for traversing a tree. After that I did changed quite a bit on the design and decided to ask for another round of ...
3
votes
1answer
81 views

Iterator for traversing a tree

What I want to achieve is the following: ...
6
votes
1answer
53 views

BST with node count

I took a simple BST implementation which is only really a set (i.e. just element, not key value), and decided to add a node count. I was translating this, which is explained here. In particular the ...
3
votes
2answers
214 views

LeetCode: Diameter of Binary Tree

https://leetcode.com/problems/diameter-of-binary-tree/ Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest ...
1
vote
0answers
64 views

leetcode 230: kth smallest element in a Binary Search Tree

Here is my implementation: the goal of the algorithm is to find the kth smallest element in a BST. ...
2
votes
2answers
482 views

Ternary search tree implementation in python 3

I have implemented a Ternary search tree. It works well. But if you think something needs to be improved, say it. This code was tested in Python 3.7.4. ...
4
votes
2answers
244 views

Heap Sort and Binary Tree Sort Algorithms (Python)

Heap Sort A Binary Heap is a Complete Binary Tree where the items are stored in a special order such that the value in a parent node is greater or smaller than the two values in the children nodes. ...
1
vote
1answer
101 views

Flatten binary tree to linked list

I'm trying to flatten a binary tree into a linked list. I have a working, correct solution: ...
3
votes
2answers
245 views

Binary Search Tree implementation in Python 3

Please review my BST Code 1. Class node class Node: 2. Constructor ...
4
votes
2answers
595 views

Build a flat List from a Tree structure in Java

I have a Tree structure represented by a List, which I want to get it flattened. I wrote the function below: ...

1
2 3 4 5
18