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.

4
votes
2answers
34 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. ...
5
votes
0answers
38 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
31 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
67 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
116 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
109 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
60 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
47 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
92 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
80 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
16 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
55 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
31 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
111 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
34 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
122 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
78 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
84 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
53 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
96 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
69 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
133 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
145 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
115 views

Generic implementation of binary search tree in Java

The following is my binary search implementation in Java: ...
3
votes
1answer
79 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 ...
12
votes
3answers
537 views

Huffman tree compressing/decompressing in C

In a past course one of the assignments was to write a program that can compress files using Huffman Tree algorithm, and uncompress the files that the program generates. My design is to count the ...
3
votes
1answer
47 views

Find the number of elements larger than a given element in BST

I'm trying to solve the Hackerrank's Insertion Sort Advanced Analysis problem using BST (similar to this question on SO). As I put items in the tree, I need to find out the number of items greater ...
2
votes
2answers
156 views

Java implementation of well-separated pair decomposition

I was trying to implement the algorithm of the 3D well-separated pair decomposition WSPD using an octree. First, I begin by implementing the class OctreeNode as ...
2
votes
1answer
73 views

Java n-ary Tree class with custom made methods and nested Node class

I'm a beginner and I wrote this (working) code for n-ary tree. The specifics: Each node is to be built with an integer value. Each node has a variable number of children. The nodes should have an ...
6
votes
1answer
58 views

Unit testing for Splay Tree in Python

I am creating a test class for the following code. splay_test.py which is the unit test code for splay tree. I needed help with writing unit tests for python to get %100 coverage. I am very bad at ...
0
votes
1answer
133 views

Generic Binary Search Tree Implementation in Java

There is an implementation of Binary Search Tree. This is kind of based on Set Theory that duplicates are not allowed but an attempt to adding the same node twice will replace the older node. BSTNode ...
1
vote
2answers
61 views

Recursive range iteration

I decided to do Advent of Code this year in D, and the following is my solution to day 8. Algorithm aside, I was specifically wondering if there was a better way to use ranges in a recursive manner ...
0
votes
2answers
57 views

Unbalanced Binary Search Tree insertion and search with random and sorted values

I have created this unbalanced binary search tree in C. Can anyone say this is a true approach or not? I have two scenarios for insertion and search: in the first scenario I am inserting 10000 random ...
2
votes
2answers
156 views

Find kth smallest element in binary search tree

I was faced with below interview question today and I came up with below solution but somehow interviewer was not happy. I am not sure why.. Given a binary search tree, find the kth smallest ...
3
votes
1answer
86 views

C++ B+Tree search & insert implementation

I made a B+Tree example for C++. I tested it a lot. I didn't get any errors or mistakes but i can't be sure it is really works (without any bug) or I don't know it is suitable for use (Memory problems,...
3
votes
1answer
708 views

Binary search tree in C++, and display, search and delete functions

I feel ready to show you my work on creating BST in C++ using double linked list and 3 more functions for manipulating the tree. There is also one more function checking if the tree is real or not. ...
7
votes
1answer
104 views

Red-black tree in C

I would like to verify that the code fulfills the specification of a red-black tree or receive suggestions for improvements. ...
2
votes
1answer
68 views

Printing a K-ary tree stored in level-order in an array

I am given an array that represents a k-ary tree (I am also given the branching factor "k") that is stored in level order. My task was to print the tree in level order. For example a trinary tree ...
0
votes
0answers
95 views

Functional, recursive algorithm for flattening a tree

My goals for the algorithm were to use recursion, to avoid mutation, and to decouple the tree walking from the action that we take on each node. My sense is that the result uses mode code is ...
1
vote
1answer
406 views

Binary search tree insertion in Racket

I am learning Racket and implemented a BST insert function (insert tree n) where the format for a BST node is ...
2
votes
2answers
60 views

Collect members from custom tree nodes

In my program, I have a tree of Group nodes, each identified by their ImportId. Each group has zero or more members, that I ...
1
vote
2answers
152 views

Graph library including minimum spanning tree using Kruskal's Algorithm

I've only ever made small programs. So, I've tried to make something large but easily reusable/extendable. Posted below are the beginnings of a graph library I've written up. One particular point I'd ...
2
votes
1answer
111 views

Pramp: Sales path

the question is taken from Pramp(really cool site!) there is a more straight forward solution. using recursion. but I thought trying it using BFS. please review only the code of GetCheapestCost ...
2
votes
0answers
172 views

AVL Tree with delete method in Python 3

Just a general code review. I'm looking for my first developer job so anything constructive is awesome. Please, and thank you very much. I've written this implementation as an exercise and to add to ...
2
votes
1answer
72 views

Binary search tree with common operations

I trying to see if I implemented these operations correctly and if they could be optimized in any way. The implementations are just the typical implementations found online but I adjusted them to work ...
3
votes
1answer
140 views

C++ inorder traversal of binary tree

The challenge was to create a function which performs an in order traversal without using recursion. What I came up with was to use a stack and some clever popping and pushing. I was wondering if ...
5
votes
2answers
1k views

Generic tree data structure for flat data from a database

Could someone please review my code for performance(any other suggestions are welcome) which converts flat data list coming from database to a tree? Interface for db entity class ...
4
votes
1answer
622 views

Maximum depth or height of a Binary tree

The Following function is supposed to calculate the maximum depth or height of a Binary tree the number of nodes along the longest path from the root node down to the farthest leaf node. ...
1
vote
1answer
46 views

Traversing the Parent-child relationship between objects: Part 2

This code builds on the code in the previous iteration. All of the following tests should work: ...