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
7
votes
1answer
446 views

Counting nodes in a binary tree with odd values, using an iterative algorithm

I've to simulate exactly a recursive algorithm with an iterative one. Assuming that I have a binary search tree that contains only a key and two references at his right and left child I want to do ...
0
votes
2answers
974 views

Preorder traversal of a tree

I am practising my coding skills on interviewbit. I got one question about preorder (for a tree). This is what I did first: ...
4
votes
1answer
379 views

Convert Ternary Expression to a Binary Tree

This is programming question I came across (geeksforgeeks) : Given a string that contains ternary expressions. The expressions may be nested, task is convert the given ternary expression to a binary ...
1
vote
2answers
536 views

Merge Two Binary Tree- LeetCode

I solved this problem on LeetCode Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. ...
2
votes
1answer
214 views

segment tree for adding a array with single index updates

I have written a program to sum up a value between a range using segment tree and this tree also supports single index updates. I am wondering if I have followed all the python rules for names and ...
6
votes
1answer
1k views

Function call tree

I'm trying to make a tree that can be used to build nested functions out of smaller functions. The goal is to have a tree that is filled with functions. Functions should fill the oldest unfulfilled ...
14
votes
3answers
20k views

Building a Tree from a flat List<Nodes>

This is my incoming data: ...
1
vote
2answers
331 views

Given a binary search tree and a level, calculate the number of nodes at that level

The problem statement is in the title. If the given tree is empty, -1 is returned. If the tree has k levels, and the given level is m, and m > k, -2 is returned. If neither of these conditions is met, ...
6
votes
2answers
2k views

Red-Black Tree Implementation

This is my implementation of a red-black tree that I'm planning to use in a little personal project. It works fine, however I'm not sure that the code is very good as I'm only a beginner, and ...
1
vote
1answer
4k views

Creating a Tree from an input string

I have a tree in a file which looks like this: (1 (2 (3 (10 11) 15) 4 (5 (() 13) 6))) Here, a Node and its children are written like this: A ...
3
votes
2answers
4k views

Making a binary tree using an array in C

If I want to make a binary tree from an array in the following order: Example: for the following array: { -1, 17, -1, 3, -1, -1, -1, 55, -1, 4, -1, 15, 11, 2, 3 } ...
5
votes
1answer
272 views

Hackerrank - Maximum Disjoint Subtree Product

Problem statement We define an unrooted tree, T, with the following properties: T is a connected graph with nodes ...
3
votes
2answers
7k views

Java: Given a list of edges, build a tree and return the root

In an interview I was asked to write Java code to build a tree and return the root, given a list of edges. It was a fairly open ended question where the interviewer left all the decisions up to me. I ...
3
votes
1answer
2k views

Height of a general tree in C++

I need to calculate the height of an optional (not AVL, binary) tree. For input I receive an n integer positive number: parent(0)...
2
votes
0answers
31 views

Public function that traverses through a tree

So I have this function that lets one traverse through a tree without having to get the root node of the tree. The user basically sends a parameter that is either ...
3
votes
1answer
794 views

A JS Tree Viewer in VueJS

To learn Vue.js I have made a tree viewer with the code below. Can anyone give me some advice on how to improve the code? ...
1
vote
1answer
437 views

Sum of nodes at a given depth of a binary tree (String representation)

Given a binary tree of integers and a depth, the goal is to find the sum of all node values at the depth in the tree. The root is considered depth 0, and the tree is given as a string in the format: <...
6
votes
3answers
182 views

Count number of ways to construct binary search tree with n elements

I had this question asked in an interview: In how many ways, we can construct binary search tree from \$n\$ elements? I have written this code, but got a feedback that it could be improved. How ...
1
vote
0answers
68 views

Extending a simple Tree parser and lexer

Yesterday, I decided to learn about scala's Parser-Combinator library. After a while, I was able to write a simple Tree parser and lexer which understands trees written like this: ...
1
vote
3answers
401 views

Exercise - count binary-tree nodes with 1 leaf and 1 internal node as children

I'm currently working through "Algorithms in java" by Robert Sedgewick (3rd edition, german version) on my own and am trying to solve one of the exercises there. The exercise Write a program that ...
3
votes
2answers
371 views

binary tree to binary search tree

Today I had an interview where I was asked to solve "Given an binary tree, convert it to binary search tree in minimum time, space complexity". I wrote this code, but got the feedback that complexity ...
3
votes
1answer
520 views

A Trie data structure in Java

this is the first time I' implementing a Trie. My goal was to make it as simple as possible. So here is the result: Can I simplify anything else? Are there some things I can change to increase ...
2
votes
0answers
237 views

Follow-up 1: Compare 2 unordered, rooted trees for shape-isomorphism

The previous post can be found here. In the previous post I tried to solve an exercise but found through the answer of Peilonrayz a bug in the code and 2 more while fixing it. These have now been ...
2
votes
2answers
531 views

Compare 2 unordered, rooted trees for shape-isomorphism

I'm currently working through "Algorithms in java" by Robert Sedgewick (3rd edition, german version) on my own and am trying to solve one of the exercises there. Edit: This question now also has a ...
0
votes
1answer
46 views

Binary Heap Order Fixing in Java

I am implementing a binary heap in Java. I've come to the point where I have to fix the heap ordering whenever a new node is being inserted to the heap. The code below fixes the heap ordering. I have ...
4
votes
1answer
262 views

Python beginner's tree with unit tests

I have implemented a tree in Python. However, due to my lack of experience with Python, I am not able to judge the overall quality of my code. ...
4
votes
3answers
318 views

C++ binary search tree

I am learning C++ and today I've implemented a version of the binary search tree. I would like your thoughts about it. node.h ...
1
vote
2answers
182 views

Check if a tree is a proper BST [closed]

I have a function that takes the root node as input and needs to return if the tree is a proper BST as per the definition below: The data value of every node in a node's left subtree is less than the ...
2
votes
1answer
3k views

Finding minimal spanning tree of a graph using Kruskal's algorithm

I am doing some practice interview questions for class, one of the questions is: Given an undirected graph G, find the minimum spanning tree. Function should take in and output an adjacency list. ...
5
votes
1answer
1k views

C++11 : simple Tree implementation

I have created a C++11 implementation of a simple Tree for an online code challenge, I'd like to know how to improve it and how to make my code use less memory : ...
3
votes
1answer
300 views

Check if a Binary Search Tree is valid

I've written the following code in Ruby: ...
5
votes
2answers
172 views

Test if a binary tree satisfies the BST property

I implemented code logic provided in the EPI book to determine if a binary tree is a BST. There were two approaches, therefore I used polymorphism for the common parts. I'm also using templates. I ...
-4
votes
1answer
624 views

Winforms tree view checking/unchecking

How can I improve the tree searching algorithm, or can I? ...
4
votes
1answer
148 views

Counting increasing subsequences with a “hacker's” Binary Index Tree

This is an \$O(n \sqrt n)\$ solution to the the following problem: Given a sequence, compute the number of non-empty increasing subsequences The algorithm is to compute ...
5
votes
1answer
69 views

Given a binary tree, \$T\$ whose nodes have positive values, find the value of the maximal path

Problem Problem is Maximum sum path from https://firecode.io Given a binary tree, \$T\$, whose nodes have positive values find the value of a maximal path. A path is a simple path from a node to ...
4
votes
4answers
2k views

Check if two binary trees are equal

I've recently solved the LeetCode's "Same Tree" problem: Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are ...
2
votes
1answer
3k views

Binary tree inorder traversal (iterative solution)

Problem statement Given a binary tree, traversal the binary tree using inorder traversal, so the left child is visited first, and then node and then right child. Write an iterative solution as a ...
3
votes
0answers
347 views

Building a k-d tree in Python

To learn Python, I have implemented a function that creates a k-d tree from a list of 2-d Points. I would like to refer to this question where i asked about the k-d tree in C. However, the Python code ...
1
vote
1answer
2k views

Generic binary search tree in Java

I've posted the code before, but this time I believe I fixed the bug with remove. The class implements a Binary Search Tree without rebalancing, since unbalanced tree is not an issue in my case. I ...
3
votes
0answers
108 views

Splay tree using shared and weak pointers

I'm having fun with shared pointers in a splay tree. Please let me know what I can do about code readability, and if you have any tips on speeding up my code, let know about that too (however, I am ...
8
votes
1answer
125 views

Implementation of k-d-Tree

I am a complete beginner in C and I have started learning it by implementing binary trees. So have implemented a basic k-d-Tree. I provide two structs and two methods for comparing values on different ...
8
votes
1answer
473 views

Convert Iterable tree to string

I code this function to convert list of dict to a specific string to export last in a row table string field. ...
1
vote
1answer
1k views

Recursive Preorder Traversal

For today, I tackled this coding challenge question: Given a binary tree, write a method to recursively traverse the tree in the preorder manner. Mark a node as visited by adding its data to the list -...
1
vote
0answers
162 views

A Random Insult Generator (i.e. a Tree class for generating random sequences of elements)

I made a generic tree class, which is meant to be useful for generating random sequences of elements, for example, sentences (sequences of strings). Also, I have some concernings regarding the tree's ...
7
votes
1answer
865 views

Hackerrank: Kindergarten Adventures

Problem statement Description: Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round ...
3
votes
1answer
465 views

B-tree based map in Java

I have this B-tree based map in Java: BTreeMap.java ...
1
vote
1answer
827 views

Method that remove duplicates from a list of words in Java [closed]

Today, I took a coding challenge with this question: Given a List of Strings, write a method removeDuplicates that removes duplicate words from the List and returns an ArrayList of all the unique ...
5
votes
2answers
259 views

Improved Van Emde Boas tree based map in Java

After working on Van Emde Boas tree -based map in Java, I came up with this code. I managed to fix the bugs and improve the performance by using internally primitive integers instead of wrapped ...
-3
votes
1answer
435 views

Huffman tree node

I've implemented a Huffman tree for one of my academic coursework. For each of the nodes in the tree, there will be a left child, right child, and a parent. For each ...
2
votes
2answers
61 views

Alternate Tree implementation

I am trying to implement algorithms related to trees in Scala. I have dabbled with the following implementations of a tree. Are there any pros and cons of preferring the non-...