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.

learn more… | top users | synonyms

2
votes
0answers
49 views

Python AVL Tree

While learning python, I decided to implement some data structures, this time AVL tree. I think the logic is correct, is there a way to make it more clearer and any ideas to add more tests? ...
2
votes
1answer
36 views

Beginning C BST implementation

I'd really appreciate if I could get some feedback on the following code with regard to security, efficiency and possible uncaught errors. Personally I feel the ...
4
votes
1answer
85 views

Binary search tree implementation in C#

I have implemented a BST for an assignment in C#. I am still in the process of implementing IEnumerable and ToList functions but ...
4
votes
0answers
114 views

Assembling and traversing a tree, given a list of items and parent pointers

I've written a function which takes input such as this: ...
2
votes
2answers
45 views

Binary Tree Inversion

I solved this problem. Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 To this ...
1
vote
0answers
42 views

Insert Tree in F#

Can this be improved, or made more concise? ...
-1
votes
0answers
36 views

Find parents in list of nodes

I have a python dict "tree" with entries like these: {'baritone': ['baritones', 'reobtains', 'obtainers']} A function is now supposed to look into the values and ...
3
votes
1answer
62 views

Serialize and deserialize a tree

Suppose each node of a tree has maximum n children. I want to serialize and de-serialize for a tree. My current solution is doing pre-order traverse, and using <...
2
votes
1answer
25 views

BTreeMap as a multimap in Rust

I'm learning Rust so as an exercise I'm trying to write a datatype wrapping the builtin BTreeMap to allow it to store duplicates. Like the STL multimap type. The ...
1
vote
0answers
33 views

Huffman code generator in Typescript

Write a program that takes any input text and produces both a frequency table and the corresponding Huffman code. Take approximately 360 words from any English document as your input text. ...
1
vote
0answers
45 views

Multi-way Tree organized alphabetically and search in preorder trasversal order

In attempt to create a k-ary or multiway binary tree sorted alphabetically at every deep, I created this class in Ruby to handle the order and search in a recursive way by the ...
3
votes
1answer
66 views

C++ Tree Object Class

I'm writing a class that acts as the base for almost all objects in my project. I know this class technically doesn't represent a tree structure, because it can have more than 2 children. What should ...
2
votes
2answers
35 views

Inserting into a binary search tree in C

I am in the process of refactoring this simple binary search tree code responsible for adding new values: ...
-1
votes
1answer
71 views

Balanced Binary Tree

There are some things that smell about this code, for instance multiple checking, length both returning value and changing some other variable. But I can't for the ...
3
votes
1answer
109 views

Calculating binary tree height as asked in the interview

Input: The first line contains the number of vertices \$n\$. The second line contains \$n\$ integer numbers from the range \$\left[−1, n − 1\right]\$ representing the \$0\$-based index of the ...
9
votes
3answers
292 views

Google Foobar Challenge: Lucky Triples

Note: Since my time has passed for this challenge, I do not remember exactly the stipulations but will attempt to recapitulate them to the best of my knowledge. Essentially, the challenge was this: ...
3
votes
1answer
58 views

Find the shortest whole repetitive substring (part 2)

This is a continued improvement from discussion here (Find the shortest whole repetitive substring), since code has changed a lot dues to improvement, I post it here. Major smart ideas are from ...
0
votes
1answer
14 views

Displaying a List in a Managed Metadata TreeView

I have some Managed Metadata in SharePoint which is in the following format: Managed Metadata Service People Job Title And I have a list of people, ...
3
votes
1answer
46 views

Binary trees exercise

I'm not sure about this code but it feels ugly to me. It is a solution to this. ...
4
votes
2answers
94 views

QuadTree C++ Implementation

I recently created a QuadTree implementation in C++. It varies slightly from most implementations. Instead of storing elements it only organizes elements. This allows programmers to insert elements ...
3
votes
1answer
56 views

Printing different aspects of a binary tree

I had an assignment, for which I got full marks, but am really upset about the code I have written. I feel it is too manual and repetitive. Please help me make it more optimized. I am making many ...
6
votes
4answers
137 views

Count number of nodes in a binary tree the OO way

I am trying to write a simplify the algorithm of finding the number of nodes in a binary tree by using good object oriented design. I have been into good OOP style recently and found it really ...
0
votes
1answer
47 views

Root to leaf path with given sum in binary tree

For a given binary tree and a sum, I have written the following function to check whether there is a root to leaf path in that tree with the given sum. ...
3
votes
0answers
41 views

Optimising insert operation for btree

I'm working on implementation insert operation for a btree, and would like to know if there is any way to optimise the inserting operation to make it go faster. The code seems really long so I'm ...
3
votes
1answer
57 views

Trie Tree match string containing ? and *

Using Python 2.7 and here is my code which implement trie match for a whole word, and also support if the whole word contains ? (match any one character) or ...
8
votes
3answers
351 views

Building a minimal-height BST from a monotonically increasingly ordered array structure

Problem statement: Given a sorted (increasing order) array with unique integer elements, write an algorithm to create a binary search tree with minimal height. Assumptions: The array (and ...
2
votes
0answers
48 views

GSS4 - Segment Tree implementation 2

The problem is also known as GSS4. The exact detail can be seen here but I reached TLE. You are given a sequence A of N(N <= 100,000) positive integers. There sum will be less than \$10^{18}\$....
4
votes
3answers
184 views

Kattis Challenge - Animal Classification

I'm new to competitive programming and C++ in general. I have solved the Kattis Animal Classification challenge. We are given two classification trees, each represented using nested parentheses. ...
6
votes
4answers
90 views

Find the 1st and 2nd largest num in an array, O(log N) + O(N) version

I have implemented the O(logN) + O(N) version for the problem: How to find 1st and 2nd largest element in a non-negtive unique array? The algorithm principle is from this stackoverflow answer.The ...
3
votes
1answer
51 views

Printing the hierarchical representation, and finding all ascendants for a node, in a self-referential table in Python with recursion

I have a self referential database table: CREATE TABLE category ( id INT PRIMARY KEY, name VARCHAR, parent_id INT REFERENCES category (id) ); And ...
-1
votes
1answer
64 views

Diameter of a binary tree in Java

I wrote this to calculate the diameter of a binary tree. Are there any cases for which this code might fail? ...
1
vote
0answers
43 views

Finding Duplicate Subtree in O(n) time

I wrote the following algorithm to try to find the largest duplicate subtree in a tree as described here: http://ayushcshah.github.io/algorithm/binarytree/2016/04/01/detect-duplicate-subtrees.html (I ...
1
vote
0answers
39 views

Node map builder for trees

I've been building a tree from a flat collection recently in this question (Re)Creating a tree from a flat collection (or “unflatten” a tree). It turned out very soon that this wasn't enough because ...
10
votes
1answer
246 views

Yet another minimax tree for TicTacToe

I have written a working minimax tree for Tic-Tac-Toe. What I have done so far is create a tree with end node having value 10, 0 or -10 base on win, lose or draw. After I create a tree I then ...
1
vote
3answers
107 views

Sort a list of objects to put parents first

I have a class which contains a reference to a parent of the same type( or null if it has no parent). ...
2
votes
0answers
33 views

Simple Unbalanced KDTree with 1NN Implementation

This is our first attempt at building an unbalanced k-dimensional tree with nearest neighbor (1NN) lookup to process multi-dimensional data. In particular, the "suspect region" in the ...
0
votes
0answers
45 views
0
votes
1answer
62 views

Kth largest element BST

Would this take \$O(n)\$ in worst case? Can it be improved? ...
5
votes
2answers
107 views

Creating a minimax tree recursively

I am currently working on creating a tree (this is my learning process of creating a minimax tree and am starting towards minimax tree from here) with multiple nodes recursively. Can anyone please ...
3
votes
3answers
194 views

(Re)Creating a tree from a flat collection (or “unflatten” a tree)

I need to create a tree from a flat collection. The order of elements is defined in a specification so it's rather impossible that it will ever change... but I want the whole algorithm to be ...
1
vote
1answer
78 views

Print tree level by level including Empty node

Here is my code to print tree in a level by level way. It needs to print an empty node as well to keep tree structure like full binary tree for elegancy reasons. I'm wondering if anything you think ...
2
votes
2answers
62 views

Game combo press model (substring search)

I'm trying to make a model for checking combo presses in a game. Given a list of Button (enum) presses, it returns the list of possible combos completed. I tried ...
2
votes
0answers
63 views

HashMap with binary search tree for chaining

My friend was asked to implement a hashtable with a binary search tree. I tried practicing it myself, and it turned out to be very verbose. I need suggestions to clean this up! ...
0
votes
0answers
35 views

k-Ary Tree Implementation

Simple implementation of k-ary trees. Is this the "correct" / standard way of implementing k-ary trees? ...
3
votes
3answers
157 views
1
vote
2answers
61 views
2
votes
2answers
54 views

AVL tree insertion and deletion of nodes in C. 2.0

I asked a question yesterday, based on the answers to that question and some personal insights I was able to update the original code. which I am posting here to get reviewed. I also thought about ...
2
votes
3answers
128 views

C++ Tree Class implementation

I am trying to implement a class for the Node of a Tree in C++ in order to represent an HTML structure. It is not complete by a shot, but i'd still like an opinion. TreeNode.h : ...
3
votes
2answers
188 views

AVL tree insertion and deletion of nodes in C

This is my implementation of AVL tree, it works fine. is there any thing that can be improved about addition and deletion procedures specifically when deleting the root, ...
4
votes
2answers
119 views

Print tree in a zigzag manner

I want to print a tree in a zigzag manner level-by-level. I'm wondering if there are any logic issues in my code and any further performance improvement ideas by using a smarter algorithm ...