Hot answers tagged

26 votes
Accepted

Google Foobar Challenge: Lucky Triples

1. Testing Let's take a look at your second problem: I've tested it on rather trivial cases, but have no way of knowing [...] if it returns the correct answer in more intricate examples A good ...
Gareth Rees's user avatar
  • 49.6k
21 votes
Accepted

Extraction of value from a dict-tree, given a path of arbitrary length

Recursive Approach Review In this method you are performing too many look-ups. First, you are looking up the 0th value of p two or three times. List indexing ...
AJNeufeld's user avatar
  • 33.3k
17 votes
Accepted

Printing binary trees

First of all, in my opinion, both the algorithm is nice and interesting, and the code is really well-written, broken down into easily understandable functions! Well done! The only addition that I can ...
Attilio's user avatar
  • 1,645
16 votes
Accepted

Binary Tree Implementation in Rust

Examples have first-class support in Cargo. Place them in a directory examples next to src. They can then be run with ...
Shepmaster's user avatar
  • 8,608
15 votes

Building a Tree from a flat List<Nodes>

Well your provided example data can't be real, because I never ever saw an int? which could take 1.1 as value. I will apply ...
Heslacher's user avatar
  • 50.3k
12 votes

Extraction of value from a dict-tree, given a path of arbitrary length

Use a library. In particular, glom does an admirable job of this, and is actively developed. ...
Cireo's user avatar
  • 239
11 votes
Accepted

Using smart pointers to construct Binary Search Tree

Answers to your questions Am I using std::unique_ptr correctly here? Using std::move and get? Yes, you are using those correctly. Is there any way for me to make ...
G. Sliepen's user avatar
  • 50.4k
10 votes

C# Treap implementation

It would be nice to be able to pass an IComparer<TKey> instead of having to make sure TKey implements ...
ChrisWue's user avatar
  • 20.3k
10 votes
Accepted

Trie Tree match string containing ? and *

1. Review The implementation with defaultdict is very elegant. I like it. The code is not portable to Python 3 because of the use of the ...
Gareth Rees's user avatar
  • 49.6k
10 votes
Accepted

BTreeMap as a multimap in Rust

Idiomatic Rust style is to place braces on same line. When the function definition gets longer than one line then the brace lives on the next line all on its own. Learn and love the ...
Shepmaster's user avatar
  • 8,608
10 votes
Accepted

Use generator to do inorder traversal

Your structure seems adequate, but there are a couple of mistakes, and some new language syntax in 3.3 that will make things even clearer: That name! ...
aghast's user avatar
  • 12.3k
10 votes
Accepted

Binary Search Tree Data Structure Implementation in C++11 using Smart Pointers

I would put the node inside the tree as a private member. ...
Martin York's user avatar
  • 94.3k
10 votes
Accepted

Generic Binary Tree

Use std::unique_ptr! I see two potential memory leaks in the code, and both of them would have been prevented by using ...
hoffmale's user avatar
  • 6,458
10 votes
Accepted

Recursive search on Node Tree with Linq and Queue

Reading Material .. any reading material about searching algorithms on trees These are the most common tree walkers: Breadth-First Search Depth-First Search Review There is a bug with IsRoot. ...
dfhwze's user avatar
  • 13.9k
10 votes
Accepted

Iterator for traversing a tree [v2]

My problem with this iterator is that it is very expensive to copy; as you have to copy a stack when you copy the iterator. The main point of iterator is that it should be "cheap" to make copies. All ...
Martin York's user avatar
  • 94.3k
9 votes

Building a Tree from a flat List<Nodes>

(Bug) Edit: It seems that your incoming data are sorted. If so, this 'bug' is not valid of course Your traversing algorithm works fine as long as the TreeNodes ...
JanDotNet's user avatar
  • 8,538
9 votes

Binary Search Tree BFS iteratively

Much of the code looks good. Some comments could be clearer, and there are some correctness issues. In is_leaf(), please strike this redundant remark: ...
J_H's user avatar
  • 16.8k
9 votes
Accepted

Implementing a binary tree in C++ using "std::unique_ptr"

On raw and smart pointers First, I'll address your specific question, about using a raw pointer to refer to nodes in the tree. I believe you're doing exactly the right thing here, because smart ...
Toby Speight's user avatar
9 votes

Iterator for traversing a tree [v2]

Martin York already has a very good point about the iterator, so I'll not comment on that. Avoid writing types twice In main.cpp you are writing statements like: ...
G. Sliepen's user avatar
  • 50.4k
8 votes
Accepted

Implementing a fast DBScan in C#

_metricFunc is unused, which means it can either be removed, or there's a bug in the program. The first line in ...
mjolka's user avatar
  • 16.2k
8 votes
Accepted

Maximum element in a binary tree

static, and possibly public I'd recommend having this method outside your binary tree class itself. It can operate on a binary ...
Simon Forsberg's user avatar
8 votes
Accepted

Inorder, Preorder, and Postorder traversal of a binary tree in C++

I can see a few changes that at least might be worth considering. Minimize Requirements All else being equal, you typically want to minimize the requirements you place on the type contained in a ...
Jerry Coffin's user avatar
  • 33.5k
8 votes
Accepted

Code smell when recursively returning a List

I wouldn't say there is a code smell with what you have now. However, it could be made more OOP-friendly. An actual problem is that you're using raw types for Node,...
Tunaki's user avatar
  • 9,243
8 votes

C++ Tree Class implementation

Some quick comments: Const correctness You really should make your code const correct. For example at least these functions could be const declared: ...
Emily L.'s user avatar
  • 16.5k
8 votes
Accepted

Iterator for BFS binary tree traversal

I'll ignore the 'PluralSight' code. Note that you have implemented a breadth-first traversal. As far as I'm concerned, a spec which doesn't specify how the tree is traversed is insufficient, but you ...
VisualMelon's user avatar
  • 7,421
8 votes

An array with \$O(\log\ n)\$ time complexity for all operations

Import import java.util.*; The class does not need to import the whole java.util package. Instead it only needs: ...
Roman's user avatar
  • 2,773
7 votes

Path sum in binary tree

If recursion has reached a leaf node then either you have accumulated the correct sum or not, but in no case it is necessary to check the left or right child node. Therefore I would change the first ...
Martin R's user avatar
  • 23.1k
7 votes
Accepted

Binary tree data structure

I see problems in nearly every method. First of all, PEP 8 specifies four spaces for indentation. Since whitespace is significant in Python, this is a pretty strong convention. ...
200_success's user avatar
7 votes
Accepted

Binary trees in Rust: iterators

Again, this code looks pretty great! A few minor style points to start: Avoid using explicit typing when you don't need it. Don't say ...
Shepmaster's user avatar
  • 8,608

Only top scored, non community-wiki answers of a minimum length are eligible