Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

7
votes
2answers
423 views

Word-Ladder solver in Python 3

Follow-up to Python program to find a word ladder transforming "four" to "five" I'm trying to write a program to solve a word ladder puzzle to brush up on my Python. For those who ...
2
votes
0answers
25 views

Haskell API for manipulating Tree/Forest

I'm experimenting how a Relation (a cartesian product's subset) can be implemented in Haskell using different data classes. Forest and Tree allow to keeps together in a single data structure ...
2
votes
1answer
48 views

Unique binary search trees

I have written some code that prints out all unique pre-ordered binary search trees containing numbers 1 to n. It is not an ...
4
votes
2answers
81 views

Find the in-order successor of a given node in a binary search tree

I have an implementation of an algorithm to return the in-order successor of a given node in a binary search tree. We are allowed to assume that each node in the BST has a reference to its parent node....
4
votes
1answer
47 views

Building a tree in python

Please can someone confirm if this code is the best way to build the following tree ...
3
votes
1answer
31 views

A BFS and DFS implementation

I believe this code prints both the DFS and BFS of a directed graph. As with some of my previous posts, this is mainly to share my code with other people working on a similar issue but I would also ...
3
votes
2answers
54 views

LeetCode: Leaf-similar trees C#

Please comment about performance https://leetcode.com/problems/leaf-similar-trees/ Consider all the leaves of a binary tree. From left to right order, the values of those leaves form a leaf ...
1
vote
1answer
42 views

AVL tree implementation using shared_ptr/unique_ptr

I have implemented an AVL tree using shared_ptr. I know that there is an overhead regarding using shared_ptr and instead a unique_ptr could be used. But the thing is that my node contains also a ...
1
vote
1answer
55 views

Sequence for Pre-order traversal of Binary Trees

I wanted to make a Sequence that can do a pre-order traversal of Binary Tree. Doing so provides automatically unlocks wonderfully useful methods, like ...
0
votes
0answers
22 views

Navigate among Microsoft UI Automation elements by using the TreeWalker

I wrote a method that finds a child item in a menu based on some condition. The code works but in the cases when I'm searching for more items which are way down in the tree structure it will take a ...
0
votes
1answer
41 views

Python program for Word Search II

This is a Leetcode problem - Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of a sequentially adjacent ...
3
votes
0answers
35 views

reverse recursive search on an object with javascript - like tree search

I implemented a tree search with JavaScript. It has below basic rules. It will search over an array of objects which has recursive(like fractal) If it found a according data in a leaf(the final ...
2
votes
1answer
55 views

Removal of subtrees in tree on condition optimally

I have a small program that removes subtrees of a tree based on following condition: If a subtree consists only of nodes with "rank" 1, remove it. I am using recursive DFS approach. Trees or ...
-4
votes
1answer
38 views

Finding the sublist with best utility function among all list's permutations [closed]

I try to find the the better solution in terms of time complexity. Among the list of elements I need to find the sublist with maximum value of given utility function. Each element has it's own type. ...
1
vote
1answer
89 views

Parse provided json, and in jObject find and convert all jToken.double types that have whole number value to long type [closed]

I wrote code sample below that is finding decimal values in jObject without '.' char, and convert them to long type. The issue is that it works for max jObject that contains two child jObjects. ...
2
votes
1answer
49 views

Fibonacci sequence binary tree console logger

I wrote code to show the structure of a Fibonacci sequence pattern in a graphical way. First I structure a tree given a variable the user inputs for how many iterations of the sequence they want to ...
2
votes
2answers
65 views

Leetcode: Flatten binary tree to linked list C#

https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Please comment on performance Given a binary tree, flatten it to a linked list in-place. For example, given the following ...
0
votes
1answer
49 views

Is it possible to DRY these 2 methods of a tailrec Node Tree?

I have a Tree structure composed by Node class. I have to @tailrec method that are visiting up to the root. Those 2 methods ...
3
votes
3answers
95 views

LeetCode: maximum-depth-of-n-ary-tree

https://leetcode.com/problems/maximum-depth-of-n-ary-tree/ Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the ...
3
votes
0answers
35 views

Adding an item to a hierarchical data structure

What I need to do is create a new object, modify it using a recursive function, then merge it with another object, and do this all in such a way that the values of the new object take precedence over ...
1
vote
2answers
125 views

Leetcode - find if BST is valid or not

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node'...
1
vote
1answer
85 views

Convert nested array of values to a tree structure

I have a nested array that represents a parent child relationship, that I need to convert to a tree structure. Example nested array ...
2
votes
2answers
50 views

Find all k points which are closest to origin

We have a list of points on the plane. Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the ...
3
votes
1answer
102 views

C# Binary search tree

I implemented binary search tree. What do you think about my code? How could i optimize it? Where are my major mistakes? I am not asking only about BST but about my coding style in general, using of ...
4
votes
2answers
77 views

Extract indices of visible nodes in a tree where only some nodes are expanded

A program that I work on constantly updates and modifies program state containing an arbitrarily deep and wide tree. The data looks something like this. ...
6
votes
1answer
69 views

Rendering tree of nodes as string

I'm creating a debug-view for my expression-trees. They record each operation automatically in a tree that I am transforming into string so that I can see the exact ...
1
vote
1answer
37 views

Depth-first traversal implementation in Java (by recursion)

I implemented a recursive depth-first traversal, with all three traversal modes in this function. Is this the most succinct way to write the code? ...
1
vote
1answer
74 views

Slow insertion into a Red Black tree

I am trying to implement a red black tree. The problem is that the code runs slowly and I wonder if I did something wrong or just did it too many times. I already checked and the problem is in the ...
3
votes
2answers
120 views

Implement BinarySearchTree class in python

I am currently study the basic data structure and trying to implement everything as I go. can anyone give me some feedback about class BinarySearchTree how to make the code more elegant. any review ...
5
votes
3answers
112 views

Repeatedly keep removing 2 smallest elements and add its sum back to the list and return newly inserted elements' total

I have a list of unsorted numbers. Each time, I need to remove 2 smallest numbers and insert back its sum into the list. Repeatedly do this till the 1 final element is remaining in the list. Return ...
5
votes
2answers
64 views

A recursive solution to symmetirc tree

I tried to solve a symmetric tree problem Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree ...
4
votes
0answers
54 views

Ternary Search Tree / N-Gram Model in Python

I implemented a word n-gram model using a character ternary search tree. It is intended to be passed a generator that yields a long sequence of words (from a corpus) and its requirements are that it ...
4
votes
1answer
97 views

Walking over a N branching tree

The aim of this code, written in Python3.6, was to write a function that will non-recursively walk over a N-branching tree structure. It works by using a list of indices that keep track of visited ...
5
votes
1answer
86 views

Phonebook using a trie

I implemented a PhoneBook utilizing a Trie data structure. Could I ask you to evaluate it? Did I apply Trie correctly for such ...
0
votes
0answers
18 views

HTree graph in graphviz

This is the HTree I'm trying to recreate: In my class of Data Structures I'm learning about trees. They asked us to research trees of our own interest and upon ...
4
votes
1answer
71 views

Building balanced BST from sorted array: recursive and iterative approaches

I am implementing an algorithm to solve the following problem: Given a sorted (increasing order) array with unique integer elements, write an algorithm to create a binary search tree with minimal ...
1
vote
1answer
38 views

Evaluating an arithmetic expression from a binary tree

Let's say I have a Binary Tree that looks like + 2 * 5 8 I've written a function to traverse inorder to solve it as 2+5*8 Is ...
2
votes
2answers
223 views

Implementing a binary tree in C++ using “std::unique_ptr”

I have implemented a simple binary search tree class in C++ using std::unique_ptr objects to hold the pointers to each node. In doing this I have come across a ...
3
votes
1answer
36 views

Optimizing splay tree

I wrote an splay search tree using the algorithm description and debug and now I want to find out how I can optimize it, maybe I have some obvious errors and I will be glad if someone shows them to me....
1
vote
1answer
125 views

Embedding an HTML (String) Generated from a Tree Structure JSON

How to make insertJson faster (I should keep the script compatible with old versions of IE and other browsers)? Sorry about my codes that I had to write with short ...
3
votes
1answer
140 views

Counting subtrees where all nodes have the same value

A unival tree (which stands for "universal value") is a tree where all nodes under it have the same value. Given the root to a binary tree, count the number of unival subtrees. For example,...
3
votes
1answer
98 views

Leetcode 102: Binary tree level-order traversal in Swift

102. Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary ...
2
votes
1answer
54 views

Revision: BinaryTree<T> written in C#

Since my last question (BinaryTree<T> written in C#), I have rewritten my code based on the responses. My project can be found on my GitHub repo here; ...
2
votes
1answer
121 views

AVL height-balanced binary search Tree and Dictionary

Mostly as a learning exercise, and partly as I thought it might be useful, I have written an implementation of AVL trees ( https://en.wikipedia.org/wiki/AVL_tree ). An AVL tree is a height-balanced ...
0
votes
2answers
71 views

Find whether two (or 'n') particular values are present in a binary tree

I have a solution to find if two values are present in a binary tree (not a BST). I would like to know if there is a more efficient way to do this? a more elegant way to this? specific improvement ...
0
votes
1answer
220 views

Get all node descendants in a tree

I have a CSV holding a "flat" table of a tree's edges (NOT binary, but a node cannot have two parents), ~1M edges: ...
2
votes
1answer
172 views

Execute promise tree in order of declaration

I have created a little framework for my work to manage loading and unloading plugins by using a modular tree structure that is based on Promises. I would like to be able to create the tree structure,...
1
vote
3answers
165 views

Generic implementation of binary search tree in Java

The following is my binary search implementation in Java: ...
3
votes
1answer
84 views

Very slow frame rate in C++ N-body simulation using Barnes-Hutt and RK4

I am writing the code for my EPQ project and am aiming to produce a graphic showing the collapse of a particle cloud with around 10^5/6 particles. My current code will spawn a specified number of ...