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.
3
votes
0answers
37 views
How can this python quadtree be improved?
Here's an attempt I've made of implementing the Barnes-Hut nbody algorithm, or its initial stage - The quadtree. That there're lots of lengthy doc strings might excuse the lack of detailed explanation ...
1
vote
1answer
23 views
Transform a MongoDb parent reference tree into a deep nested tree
Given a mongodb collection that stores trees as nodes with parent references this methods returns a deep nested tree where every child nodes are stored in a property childs[Seq]
def getTree(rootId: ...
5
votes
2answers
68 views
Binary Tree/Knowledge Base design C++
Currently I have a binary tree template setup where my main is using it with strings to make a question/answer game. I'm using a knowledge base that works as an interface to the binary tree that main ...
2
votes
1answer
40 views
Printing the values on each level of a Binary Tree
The below code is for printing level-by-level in a binary tree:
//level order printing
public static void levelOrderPrint(Node root){
Queue<Node> que = new LinkedList<Node>();
...
5
votes
3answers
279 views
Binary Search Tree implementation
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct Node NODE;
struct Node {
int value;
int is_leaf;
struct Node *right;
struct Node *left;
};
NODE ...
1
vote
0answers
57 views
level traverse a binary tree
Level traverse binary tree question
The problem is pretty common level order traverse a binary tree but break each level into single array.
I implemented mine: I've designed a few test cases, but ...
2
votes
2answers
36 views
JavaScript bottom-up tree transformer aimed for performance
function transform(tree,fn){
var root = tree,
node = tree,
child,
parent,
is_node,
dir;
root.dir = 0;
while(true) {
is_node = ...
2
votes
2answers
51 views
Algorithms for traversing unordered tree
I have been doing some programming 'exercises'. I'm trying to come up with the most efficient algorithm for tree traversal.
Consider the following function signature:
CNode * CNode::find(int ...
1
vote
3answers
102 views
Lowest common ancestor in recursive tree traversal
LCA = Lowest Common Ancestor
The following code finds the lowest common ancestor in tree of nodes, where a node can have two parents, left and right.
The tree looks like this:
J-K
/ \
...
3
votes
1answer
68 views
Interval search tree
An interval is a data structure that represents a range (start & end, from & to, or min & max, etc.). An Interval Tree stores these intervals in a sorted tree structure that makes ...
3
votes
1answer
49 views
Join/ connect all levels of Binary tree without any aux storage
This program connects all nodes of the binary tree at the same level. A node of a tree contains a left, right and a sibling pointer which would connect it to the next node at the same level. This ...
2
votes
1answer
44 views
Iterator for binary tree - pre, in, and post order iterators
Implemented iterator for a binary tree and "pre" "in" and "post" order flavors. I'm looking for code review, best practices, optimizations etc.
public class IterateBinaryTree<E> {
private ...
5
votes
1answer
56 views
Hide the tree menu at a certain depth
I have created this piece of js purely for learning purposes and I was hoping you could code review to see any mistakes / improvements I can make.
Basically I have a tree structure and I will use the ...
1
vote
1answer
90 views
Search an element/item in an n-ary tree
Search an element in a n-ary tree. Looking for good code practices, optimizations etc.
If question is ambiguous, let me know and I will reply ASAP.
Note - SearchInANAryTree name of class is for ...
4
votes
2answers
66 views
Testing to see if tree is BST
I found a function online that tests if a tree is a binary search tree:
private boolean isBST(Node node) {
if (node==null) return(true);
// do the subtrees contain values that do not
// ...
3
votes
1answer
72 views
Inorder traversal of a tree without recursion or stack
Please review the code for code cleanup, smart optimizations and best practices. Also verify my complexity: O(n2), where n is the number of nodes
/**
* This class traverses the tree and returns ...
5
votes
1answer
51 views
Unbalanced binary search tree
I wrote this unbalanced binary tree and would like to know how to improve the code and performance. If you can point out any situations that are not being handled appropriately, that would be great ...
6
votes
1answer
276 views
MutationObserver (shim): Finding differences between 2 DOM trees
I've been working on a MutationObserver es5 shim and would appreciate some feedback on my technique for identifying changes between a node and its clone from earlier state. The reason I'm asking for ...
0
votes
1answer
332 views
Serializing/deserializing binary tree in most space-efficient way
I'm looking for corrections, optimizations, general code review, etc.
final class TreeData<T> {
private T item;
private boolean left;
private boolean right;
public TreeData(T ...
1
vote
0answers
85 views
Review my F# Red Black Tree Implementation [closed]
I have written this implementation for red black tree. I also have some helper methods to verify whether the generated tree is really balanced or not.
Looks good to me.... but I find that most of the ...
3
votes
2answers
64 views
Constructing a binary tree in java
I am constructing a binary tree. Let me know if this is a right way to do it. If not please tell me how to?? I could not find a proper link where constructing a general binary tree has been coded. ...
1
vote
1answer
93 views
Max depth of tree when all parent pointers are provided
Request for optimization, good practices, recommendations.
class NodeMaxDepth {
private final NodeMaxDepth parent;
public NodeMaxDepth (NodeMaxDepth parent) {
this.parent = parent;
...
3
votes
1answer
184 views
Printing a Binary Tree top-down (column wise)
We have a binary tree, suppose like this:
8
/ \
6 10
/ \ / \
4 7 9 12
/ \
3 5
We have to print this binary tree in top-down manner - column wise. Note that, 8, ...
5
votes
1answer
803 views
Prims algorithm implementation
Review this code regarding optimization, cleanup, and best practices.
final class EdgePrims<T> {
private final T source, target;
private final int distance;
public EdgePrims(T ...
5
votes
1answer
83 views
Simple prefix tree
I've written a simple function for building a prefix tree (and searching):
import qualified Data.List as L
import qualified Data.Map as M
data Tree a = Empty | Node a [Tree a] deriving (Show, Eq)
...
4
votes
2answers
251 views
Printing out a binary tree level by level
Below is my implementation for an interview question of printing a binary tree level by level. I've also included some methods for creating a binary tree from a vector in my solution. Please review ...
2
votes
2answers
93 views
In-order Predecessor BST review
I was referred here from StackOverflow, so here is what I posted over there:
So, for an assignment, I have to construct a binary search tree, plus an assortment of related functions. I have them all ...
0
votes
1answer
986 views
Implementation of Prim's minimum spanning tree
Feel free to tell about every possible issue (style, errors, inffective solution etc.) you found. Here is the main part of what I want to be reviewed, through there is some related code on GitHub.
...
4
votes
0answers
665 views
Performance of List representation of MonadPlus versus Tree representation
I am working on a code where I represent a MonadPlus using a Tree data structure and then explore it, summing over all of the leaves (whose type must be an instance of Monoid); I need to use a Tree ...
0
votes
2answers
54 views
How to enumerate the internal nodes and leaves of a tree more elegantly?
Might there be a simpler way to write this code?
This is the code I have, and I'd like to learn to write code more efficiently.
Thank you.
def leaves_and_internals(self):
"""(BTNode) -> ...
3
votes
2answers
319 views
Returning a list of the values from the binary tree
I want to return a list of the values from the binary tree. Is there a shorter and more efficient way to write the method for numbers?
class BTNode(object):
"""A node in a binary tree."""
...
2
votes
2answers
336 views
Building a Red Black tree using a structure as a node
I have the following structure:
struct Keys
{
//value of word in the vector<string> words.
string word;
//index of word in the vector<string> words.
int index;
//I want to ...
0
votes
0answers
42 views
Improvements to my binary tree written in CoffeeScript
This is my attempt at a binary tree. I'm using it only for ordering a LDAP user list. My need is for storing multiple objects, so this tree is limited to leaves that are objects (see data = {[..]}.
...
0
votes
1answer
770 views
Breadth-first tree traversal using STL list
I am writing code for breadth-first search of a tree with C++'s STL. Please help as I am new to the STL.
#include<iostream>
#include<malloc.h> //on llvm we don't need this
...
2
votes
2answers
217 views
Generic binary search tree implementation
I use this interface for my BST node class:
public interface BinNode<E> {
public E getValue();
public void setValue(E value);
public BinNode<E> getLeftChild();
public ...
2
votes
1answer
2k views
Arithmetic expression parsing, and converting infix to postfix notation
I'm doing a infix to postfix conversion. The code works, but here are some points I want to improve
1) In the line of
while ((!operators.empty())&&(order(operators.top()) >= ...
1
vote
1answer
239 views
Trie - code review request for improvement
Ok, code reviewers, I want you to pick my code apart and give me some feedback on how I could make it better or more simple.
public class Trie {
private static final int ASCII = 256;
...
-1
votes
1answer
144 views
Join levels of binary tree
Please ignore feedback to include generics and function renaming while giving me feedback. Names of functions have been deliberately kept the way they are for time-being. Also please let me know if ...
1
vote
1answer
1k views
Create a binary tree, code review request
Ok, code reviewers, I want you to pick my code apart and give me some feedback on how I could make it better or more simple. ( Generics would be added a bit later).
public class CreateABinaryTree {
...
3
votes
2answers
177 views
Creating a binary search tree
I want you to pick my code apart and give me some feedback on how I could make it better or simpler. Although, agree that generics should be used, instead of integers, let's punt on generics as a ...
1
vote
1answer
115 views
Given preorder array construct a BST
Ok, code reviewers, I want you to pick my code apart and give me some feedback on how I could make it better or more simple. Also its really hard to make this code work if preorder array contains ...
1
vote
1answer
671 views
Find height of a tree without using recursion, code review request
Ok, code reviewers, I want you to pick my code apart and give me some feedback on how I could make it better or more simple.
public int heightHelper(TreeNode node) {
int height = -1;
...
5
votes
1answer
77 views
Duplicate detection and reporting in a BST
For the purpose of this problem, the BST is defined to allow duplicates, and the duplicate values will always follow to the right-side of the parent nodes with the same value. For example, with the ...
1
vote
1answer
121 views
Print all path from root to leaves - code review request
Ok, code reviewers, I want you to pick my code apart and give me some feedback on how I could make it better or more simple.
public class PrintAllPath {
private TreeNode root;
private class ...
3
votes
1answer
351 views
non-recursive tree traversal, code review request
Ok, code reviewers, I want you to pick my code apart and give me some feedback on how I could make it better or more simple.
public class NonRecursiveTraversal {
private TreeNode root;
...
1
vote
1answer
494 views
Print path(leaf to root) with max sum in a binary tree - code review request
Ok, code reviewers, I want you to pick my code apart and give me some feedback on how I could make it better or more simple.
public class MaxSumPath {
private TreeNode root;
private static ...
4
votes
1answer
2k views
Create a tree from a list of nodes with parent pointers only
I want you to pick my code apart and give me some feedback on how I could make it better or more simple.
class TreeNode {
private TreeNode left;
private TreeNode right;
private TreeNode ...
2
votes
1answer
296 views
Finding inorder successor
I want you to pick my code apart and give me some feedback on how I could make it better or more simple. This code finds inorder successor in a binary tree.
public class Successor {
private ...
3
votes
1answer
144 views
Convert tree to circular doubly Linkedlist - critique request
Ok, code reviewers, I want you to pick my code apart and give me some feedback on how I could make it better or more simple.This code converts a binary tree to a doubly linkedlist (indorder).
public ...
1
vote
2answers
36 views
Print periphery of a binary tree, critique request
Ok, code reviewers, I want you to pick my code apart and give me some feedback on how I could make it better or more simple. This code will traverse peripheri ( clock and anticlockwise ) in O(n) just ...