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
1
vote
0answers
26 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
40 views

Linked tree built from (n) leaves

Firstly, here is the relevant code. It is written in kotlin. ...
8
votes
1answer
51 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
39 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
70 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. ...
2
votes
0answers
23 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 ...
1
vote
1answer
46 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
40 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
57 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
81 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
60 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
113 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
97 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
63 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
28 views

Synchronize list of trees with DB using DRF serializer

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

Binary Tree Preorder Traversal

What is the best way to move _add and _preorder methods in class Node to ...
4
votes
3answers
83 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
99 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
236 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
79 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
86 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
60 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
205 views

Binary Search Tree Using Templates in C++

I have made this BST using templates Node.h ...
2
votes
1answer
87 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
123 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
332 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
85 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
777 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
74 views

Iterator for traversing a tree

What I want to achieve is the following: ...
6
votes
1answer
52 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
211 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
61 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. ...
1
vote
1answer
322 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
229 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
100 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
223 views

Binary Search Tree implementation in Python 3

Please review my BST Code 1. Class node class Node: 2. Constructor ...
4
votes
2answers
379 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: ...
4
votes
2answers
207 views

LeetCode: House Robber III C#

https://leetcode.com/problems/house-robber-iii/ Please review for performance The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "...
5
votes
3answers
83 views

Symmetric Tree Check in Python

Inspired by This LeetCode Question. Instead of checking if every layer is symmetric, it instead checks if any aren't. This allows the program to leave early is an early layer fails the test. I'm ...
4
votes
1answer
80 views

LeetCode: Symmetric Tree C#

https://leetcode.com/explore/interview/card/top-interview-questions-easy/94/trees/627 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For ...
5
votes
2answers
133 views

LeetCode: Binary Tree Zigzag Level Order Traversal C#

https://leetcode.com/explore/interview/card/top-interview-questions-medium/108/trees-and-graphs/787/ Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from ...
3
votes
1answer
43 views

Javascript Tree Class 2

This question is the second version of the code here. I'm writing a general tree class. Specifically, each node should have oen parent, some number of children, and hold a value. I'm looking for ...
4
votes
2answers
108 views

Data structure that retrieves each item with a specified probability

I wanted a data structure that allowed me to set the chances of randomly returning each of it's elements. For example, suppose I have a Human class. Every Human has an attribute called eyeColor. I ...
3
votes
1answer
94 views

Convert a Binary Tree to Doubly Linked List

Challenge Link on geeksforgeeks: Binary Tree to DLL Given a Binary Tree (BT), convert it to a Doubly Linked List(DLL) In-Place. The left and right pointers in nodes are to be used as previous ...

1
2 3 4 5
17