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
3answers
79 views
Print all nodes from root to leaves
I've made a function to print all paths from root to all leaves in a binary search tree. I already have an insert function, which I haven't included in the code here as I felt it was irrelevant to the ...
2
votes
0answers
18 views
BST remove implementation
As practise, I implemented a BST and also the remove() method, but I figured I always have to check if the node being deleted is coming from the left child or the ...
4
votes
2answers
395 views
3
votes
0answers
28 views
BFS and DFS tree traversal
I posted some code as part of an answer to another question and I thought it would be a good idea to get that code reviewed also.
Any comments are welcomed, but I am mostly annoyed by ...
3
votes
2answers
41 views
BFS tree traversal Scala
I would love some feedback on the following code for BFS traversal in a binary tree. Is there a cleaner way to write this?
...
3
votes
1answer
33 views
Test if two nodes in binary search tree are cousins
Cousins are nodes at the same level of the tree, with different parents.
They don't have to share the same grandparent.
My solution depends on having a binary search tree with unique elements.
This ...
1
vote
3answers
221 views
Find the Max Width Of Tree
1
/ \
2 3
/ \ \
4 5 8
/ \
6 7
Width of a tree is maximum of widths of all levels.
For the above tree,
width ...
8
votes
2answers
324 views
Run time complexity of printing a binary tree level by level
I have some doubts about running time of following question. The problem is printing a binary tree level by level. For example if tree is like
...
3
votes
3answers
126 views
Binary search tree with tree traversal
Although there are several implementations of binary search tree, I have made my own implementation for practice. The following code does the followoing operation
- Create a binary tree
- search ...
6
votes
4answers
585 views
Binary Search Tree C++ Implementation
I've implemented a simple binary search tree for practice purposes:
...
0
votes
1answer
29 views
Find swapped nodes in BST
A BST has two nodes swapped. Figure out which two nodes.
Looking for code-review, optimizations and best practices.
...
4
votes
0answers
47 views
Equivalent binary trees (structure & values)
I am following the Golang tour and I have been asked to build a couple of functions to manipulate binary trees.
My code runs as expected, but I would like to know if I could further improve my code. ...
3
votes
0answers
83 views
Heterogenous tree in the application domain: How do I represent them?
The Domain
I have three types of items in my domain: ItemA, ItemB, ItemC. (I can't use their real names.) ItemA has one attribute: thing_id. ItemB has 6 attributes: thing_id, name, description, ...
4
votes
1answer
55 views
Implementation of a AVL Tree
I attempted to write a AVL Tree in a functional style and I am hoping to get it reviewed. I am looking for the code to be clear and concise, any opportunity for improvement would be greatly ...
2
votes
2answers
64 views
Print all nodes that are at distance k from a leaf node
Given a binary tree and a positive integer \$k\$, print all nodes that
are distance \$k\$ from a leaf node.
Here, the meaning of distance is different from the previous post.
Here, \$k\$ ...
3
votes
3answers
133 views
Find floor and ceiling in the BST
Find ceiling and floor in the BinarySearchTree. Looking for code-review, optmizations and best practices.
...
1
vote
3answers
64 views
Find all possible interpretations of an array of digits
Consider a coding system for alphabets to integers where 'a' is
represented as 1, 'b' as 2, .. 'z' as 26. Given an array of digits (1
to 9) as input, write a function that prints all valid ...
4
votes
3answers
51 views
Retrieve Father and Child structure in a Database Table
I have a table with this schema:
CREATE TABLE [td].[MyTable] (
[ID] [int] NOT NULL
,[FatherID] [int] NULL
)
(Note: I have excluded all the columns not ...
0
votes
1answer
54 views
Maximum sum between two leaf nodes
Given a binary tree in which each node element contains a number. Find
the maximum possible sum from one leaf node to another.
The maximum sum path may or may not go through root. For ...
0
votes
1answer
94 views
Check if two nodes are cousins in a Binary Tree
Given the binary Tree and the two nodes say ‘a’ and ‘b’, determine whether the two nodes are cousins of each other or not.
Two nodes are cousins of each other if they are at same level and have ...
3
votes
3answers
95 views
Delete alternate nodes of a linked list
If the linked list is 1->2->3->4 then the output should be 1->3. If the linked list is 1->2->3->4->5, then the output should be 1->3->5.
The question is attributed to GeeksForGeeks. I'm looking ...
1
vote
1answer
42 views
Remove all nodes which don't lie in any path with sum >= k
Given a binary tree, a complete path is defined as a path from root to a leaf. The sum of all nodes on that path is defined as the sum of that path. Given a number K, you have to remove (prune the ...
1
vote
1answer
64 views
Extract Leaves of a Binary Tree in a Doubly Linked List
Given a Binary Tree, extract all leaves of it in a Doubly Linked List (DLL). Note that the DLL need to be created in-place. Assume that the node structure of DLL and Binary Tree is same, only the ...
2
votes
2answers
76 views
Building tree structure based on flat objects - second follow up
This is a follow up on this question which is a follow up on this question
This follow up is created, because the class in question contained something, which didn't do what it should do.
Based on ...
1
vote
1answer
58 views
Building tree structure based on flat objects - follow up
This is a follow up on this question
I have implemented some changes to the class below and also added the method AddChildRange() to the ...
2
votes
1answer
37 views
Try to implement comments tree for django
I tried to create implementation of tree for comments. I want use it in Django to store comments. Please tell me how to implement it much simpler, without recursion, to find child nodes.
...
4
votes
2answers
139 views
Building tree structure based on flat objects
Description
A List<UnrelatedObects> is returned by a 3rd party webservice, which will then be transformed to a ...
1
vote
3answers
68 views
Check if all leaves are at same level
Check if all leaves are at same level. This question is attributed to geek for geeks. Looking for code-review, optimization and best practices.
...
0
votes
1answer
92 views
Convert a doubly linked list into balanced binary search tree in-place
Given a doubly linked list which has data members sorted in ascending
order, construct a balanced binary search tree which has same data
members as the given doubly linked list. The tree must ...
4
votes
1answer
58 views
A Quadtree implementation with Colliders
I have created a QuadTree in Java and would like a general code review or suggestions.
...
7
votes
3answers
352 views
Hierarchical k-ary tree in C without relying on RAM
I created a k-ary tree in C to be used as an easy and efficient way to organize "UML-like" data in embedded devices. The left node is at the lower logical level (a child) while the right node is at ...
10
votes
1answer
123 views
Good performance when predicting future game states
This question is about my answer to this king-of-the-hill challenge on Code Golf. In short, the purpose of the challenge is to code a slime that, trough various possible moves gains control over an ...
2
votes
2answers
96 views
Deepest left leaf node in a binary tree
Given a Binary Tree, find the deepest leaf node that is left child of its parent. This question is attributed to GeeksForGeeks. Looking for code-review, optimizations and best practices.
...
3
votes
1answer
67 views
Reverse values of alternate nodes of the tree
Given a Perfect Binary Tree, reverse the alternate level nodes of the binary tree.
Given tree:
...
0
votes
2answers
56 views
Return the next right node
Given a binary tree, return the next right node. This question is attributed to GeeksForGeeks.
For example, consider the following Binary Tree. Output for 2 is 6, output for 4 is 5. Output for 10, 6 ...
1
vote
2answers
86 views
Get the levels of a binary tree
Given a binary tree, return a list of each level.
I'm looking for a general review and a mention on best-practices, optimization, and verification of my complexities.
Time complexity: \$O(n)\$
...
1
vote
0answers
30 views
Optimizing Recursive Quadtree
I have a written a quadtree program in Python 2.7 to cross-correlate large catalogs with each other i.e. find the common objects in the catalogs based on their position. The problem is that it's still ...
3
votes
3answers
200 views
Find all nodes in tree, without a sibling
Find all nodes of trees, without siblings.
This question is attributed to GeeksForGeeks.
I'm looking for code reviews, optimizations and best practices.
...
1
vote
1answer
67 views
Find K distant nodes
Find all nodes, which are at a distance of k from the input node.
Input:
target = pointer to node with data 8.
...
3
votes
1answer
51 views
Construct KD-Tree in Java
I want to construct KD-Tree from unsorted List of points in the plane (i.e. 2-KD-Tree). To do so I used the method from Wikipedia: http://en.wikipedia.org/wiki/Kd-tree#Construction This is my code:
...
6
votes
2answers
328 views
Binary Search Tree implementation using templates
I wrote this implementation using templates. It works fine and as expected. Just want to know if this can be improved. I have some specific questions too at the end. Please feel free to critique to ...
9
votes
4answers
336 views
Do I need Generics for these node and tree classes?
I want to implement KD tree. I have defined class Node as follows:
...
-3
votes
2answers
119 views
Given a BST, transform it into greater sum tree
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node. Diagram is here.
Looking for code review, optimizations and best practices.
...
6
votes
3answers
225 views
Given a Perfect Binary Tree, reverse the alternate level nodes of the binary tree
Given a Perfect Binary Tree, reverse the alternate level nodes of the binary tree.
Given tree:
...
2
votes
4answers
100 views
Detect if a tree is complete binary tree
Detect if tree is complete binary tree. This question is improvement over previously asked here. Looking for code-review, optimizations and best practices.
Verifying both space and time complexity to ...
2
votes
1answer
51 views
Folding with trees
I was trying to implement a foldTree function to generate a balanced binary tree from a list of values using foldr (Question 2 ...
1
vote
1answer
39 views
How am I supposed to write a binary tree (S-Expression) pretty printer in C?
OK, I feel like I am doing something really wrong here. I'm just trying to create a binary tree, and a pretty printer for it. My approach is looking so bad I can't even...
...
4
votes
1answer
102 views
Detect a complete binary tree
Follow-up question: Detect if a tree is complete binary tree
Detect if a tree is complete binary tree or not. Looking for code review, optimizations and best practices.
...
4
votes
1answer
101 views
Segment tree implementation
I'm learning segment trees and their implementation. For the purpose, I tried solving the problem on CodeChef.
Full code here
My tree implementation is as follows:
...
2
votes
1answer
49 views
Basic binary Tree in JavaScript
I wrote a basic binary tree in JS. Can anyone give me some feedback about my code?
I just want to know if this is the right approach, and how I can improve the tree. I am new to JavaScript and data ...