15
votes
Accepted
Max-heap implementation in C
This is a max-heap datastructure,
and there is very consistent nomenclature here which reflects that.
I'm sure it is clear and commendable.
But I wouldn't have minded seeing it written in a comment, ...
11
votes
Max-heap implementation in C
General Observations
Since this might be a set of library routines, it might be a good idea to move all the heap related functions to their own .c file and have a ...
8
votes
Accepted
Check whether a binary tree is symmetric
I note that this algorithm determines tests whether the tree's structure is symmetric, as well as the content. I haven't seen the problem specification, so have to assume that that is a requirement - ...
7
votes
Max-heap implementation in C
Avoid FP math for an integer problem
floor(log2(max_heap->size)) is overkill and at some level risks incorrect results with less than ideal FP functions. A ...
7
votes
Max-heap implementation in C
The biggest issue I see here, and is not yet commented on, is the call to realloc for every insertion. When you remove an element from the heap, the ...
7
votes
Check whether a binary tree is symmetric
communicating with humans
Something that can take a long time for junior devs to realize
is that coding is primarily about communicating a technical
idea to colleagues.
programs must be written for ...
6
votes
Accepted
A toy implementation of binary tree
Nested functions are not in the C standard. It is a GCC extension, of a dubious value. Don't do it.
Casting malloc is a bad practice. First, it is not necessary, ...
6
votes
Check whether a binary tree is symmetric
In addition to the previous answer by @Toby Speight, here are some general coding style suggestions.
Documentation
The PEP 8 style guide recommends
adding docstrings for classes and functions.
You ...
5
votes
Accepted
Simple BST implementation using C++ Struct
Neither of your implementations is "advantageous" over other because both of them are C-style, unidiomatic C++ code — they are really C code with a handful of C++ additions. Moreover, they ...
5
votes
Accepted
Simple Binary Search Tree Class with Insert and Search Functions
Overall organization
Right now, you've defined a BstNode, which you use directly in main. I'd at least consider defining a <...
5
votes
Accepted
Create and view a family tree
This is by no means exhaustive, more a list of general highlights on what to focus on rather that deep analysis of the implementation.
Some general pieces of advice first:
Use some coding conventions ...
5
votes
Accepted
Binary Expression Tree that evaluates Postfix input
Validate inputs
It's not mentioned if we can assume the input is always valid.
Aside from parsing errors,
if the input is not in valid postfix form,
the parsed tree may be broken,
consider for example ...
5
votes
A toy implementation of binary tree
I would be a little more careful and consistent with my styling. You have
if (curDepth == maxDepth) {return NULL;}
but you later have
...
5
votes
Binary tree deepcopy using the Morris traversal in C
single responsibility
I need to perform a deep copy of a binary tree using the Morris traversal.
Ok, maybe you.
I confess it's not obvious to me why that is.
Consider making the underlying binary ...
4
votes
Simple Binary Search Tree Class with Insert and Search Functions
This program is using purely C++ I/O so the include for the old style C I/O is not necessary, you don't need the #include <stdio.h> and it would be better to ...
4
votes
Accepted
Sort array of numbers using tree sort - Leetcode 912
edge case
I feel the first result displayed here is reasonable:
>>> sorted([])
[]
>>> sorted([6, 4])
[4, 6]
Given zero elements, and asked to ...
4
votes
Binary tree deepcopy using the Morris traversal in C
tNode* node = (tNode *)malloc(sizeof(tNode));
In ancient times, malloc() used to return a ...
3
votes
Accepted
How can I optimize this Binary Search Tree?
I am not a Java devloper, so take review with a grain of salt.
There are a lot of unncessary comments:
3) Methods: size
...
3
votes
Accepted
Implementation of binary tree traversal
Class definition
There is no need to explicitly inherit from object; that is a Python 2.x anachronism.
Instead of ...
3
votes
LeetCode: Deepest Leaves Sum C#
Rename DFS to DeepestLeavesSum.
Extract depth + 1 to variable.
In here ...
3
votes
Accepted
LeetCode: Most Frequent Subtree Sum
The function get_sums_freqs returns an int, but when I call it, I do not assign the result to anything: ...
3
votes
3
votes
Binary Expression Tree that evaluates Postfix input
Some additional notes on your code.
Keep code noise low. Code noise is any code that is not required and gets in the way of maintainability and readability.
Avoid single use variables.
Keep names ...
3
votes
Max-heap implementation in C
It strikes me that there is an opportunity for some of your functions to be very straightforward preprocessor macros.
...
3
votes
A toy implementation of binary tree
Inefficient
isAVLBalanced() makes far too many excursions through the tree to report a boolean answer. It looks O(n*n).
Note: AVL balanced tree
Instead walk the ...
3
votes
Accepted
leetcode 530. In-order traversal of a binary search tree (C++17)
Use std::invoke() to call the visitor
Visitor can be anything: a regular function, a lambda, or even a pointer to member ...
3
votes
Accepted
Binary tree struct in C
General Observations
The use of the typedef for the node struct is a good practice.
The fact that all of the loops and ...
3
votes
Maximum Sum BST in a Binary Tree
Is there anything I can change, in the same given code, that would optimize it and/or get it accepted?
Your “Time Limit Exceeded” question is basically asking “when is this code slow?”
We tend to ...
3
votes
Maximum Sum BST in a Binary Tree
Beyond method naming and a comment stating what the signature boolean isBST(TreeNode root,…) suggest, the code gives no indication what it is there for, what the ...
2
votes
Accepted
Leetcode: Subtree of Another Tree
Your code doesn't look that ugly! Here are some suggestions to make it better. Note that LeetCode makes significantly more mistakes than you when it comes to code quality; I have listed the ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
binary-tree × 43tree × 12
c × 11
c++ × 10
algorithm × 9
java × 7
python × 6
programming-challenge × 5
binary-search-tree × 5
time-limit-exceeded × 4
rust × 4
binary-search × 4
object-oriented × 3
c++17 × 3
python-3.x × 2
recursion × 2
homework × 2
heap × 2
c# × 1
javascript × 1
performance × 1
beginner × 1
strings × 1
sorting × 1
haskell × 1