6,826
questions
0
votes
0
answers
40
views
Why is this code for finding top view of a binary tree not correct?
The problem is to find the top view of a binary tree.
I tried to solve this without using a map or a queue but my code fails for most test cases. I do not understand why.
I first found the left view , ...
-8
votes
0
answers
113
views
Why `heap-use-after-free" (LeetCode 669. Trim a Binary Search Tree) [closed]
669. Trim a Binary Search Tree
Given the root of a binary search tree and the lowest and highest boundaries as low and high, trim the tree so that all its elements lies in [low, high]. Trimming the ...
1
vote
1
answer
95
views
Is there any concrete reason to prefer `Node a (Tree a) (Tree a)` over `Node (Tree a) a (Tree a)`? [closed]
Consider the following two binary tree definitions:
data Tree a = Empty | Node a (Tree a) (Tree a)
data Tree a = Empty | Node (Tree a) a (Tree a)
I know this is a bit of a trivial question, but I am ...
0
votes
1
answer
34
views
" heap-use-after-free" error even when I am not accessing the deleted node of a binary tree
I was trying to generate the inorder traversal of a binary tree using O(1) space ie I don't wanna use recursion or any other data structures such as queues or vectors to store the node.
Edit: This ...
0
votes
0
answers
25
views
What should be the comparison key when I have a coordinate pair binary tree
I want to create a binary tree out of 2d points. What should be the key to be compared for the binary tree?
2
votes
2
answers
76
views
Diameter of a binary tree - failing case when the longest path doesn't pass through the root
So I'm working on this problem:
Given the root of a binary tree, return the length of the diameter of the tree.
The diameter of a binary tree is the length of the longest path between any two nodes in ...
-2
votes
1
answer
66
views
How Can I Implement a Binary Tree Traversal to Print Nodes in a Specific Order? [duplicate]
I'm working on a project where I need to implement different types of tree traversals (in-order, pre-order, post-order) for a binary tree. I understand the basic structure of a binary tree, but I'm ...
0
votes
1
answer
58
views
There is an error in the non-recursive implementation of the preorder traversal of a binary tree, but there is no problem in debugging
When implementing a preorder traversal of a binary tree using a non-recursive approach, I wrote my own stack. An error occurred during runtime.
When I try to debug and find the problem, it runs ...
0
votes
2
answers
33
views
Recursive insert method on Binary Tree reaches the correct node but it doesn't save data
I'm trying to make a recursive insert method on a binary tree and while the recursive part of the function seems to do the job correctly (I tracked the code step by step with debug option) when time ...
-1
votes
1
answer
67
views
Binary Search Tree Insertion Method in Java [closed]
In the following insert method for a binary search tree (BST), the left side of the tree is being updated correctly, but there is an issue with inserting values on the right side. Despite using the ...
0
votes
1
answer
86
views
Is it possible to find distance between 2 nodes of a generic tree without lowest common ancestor (or path from root)
In case of Binary Trees, It is possible to find the distance between the two nodes without finding the path from the root node or lowest common ancestor.
I was wondering if it is possible in the case ...
2
votes
1
answer
80
views
Use of ampersand (&) along with arrow operator (->) for inputting data into a struct in C, is it necessary?
My professor is currently teaching Binary Trees at college and she gave us a program to create a binary tree in C using Linked Lists.
However, there was a debate on whether ampersand (&) has to be ...
0
votes
1
answer
59
views
Why am I getting "time limit exceeded" error in binary tree level order traversal problem in leetcode?
I'm trying to solve the problem: Binary tree level order traversal problem on LeetCode and I've also tried looking for the answers, but I'm still getting a time limit exceeded (TLE) error. Please help ...
0
votes
4
answers
106
views
Why using else if instead of if here when building a binary tree?
When building the insertion function in binary tree, I use two if statement to track the node( if the value is smaller than the current node,go left;if bigger,go right) .
I know the difference between ...
1
vote
2
answers
74
views
Construct binary tree from preorder and inorder traversal: wrong tree
I am looking at LeetCode problem 105. Construct Binary Tree from Preorder and Inorder Traversal:
Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary ...
0
votes
1
answer
38
views
Red Black Tree Black Height Increase after Insertion
I understand that the black height of a RB tree increases after insertion when a red-red violation is propagated to the root, which then gets colored to red then recolored to black, and it causes for ...
1
vote
1
answer
69
views
How to represent an algebraic expressions tree in a non-ambiguous way?
I have some physical model that needs to be represented as an algebraic expressions full binary tree. How can I represent such a tree in a non-ambiguous way?
Consider, as an example, the three full ...
3
votes
1
answer
103
views
Optimally counting number of nodes in a complete binary tree
I am looking at LeetCode problem 222. Count Complete Tree Nodes:
Given the root of a complete binary tree, return the number of the nodes in the tree.
According to Wikipedia, every level, except ...
0
votes
1
answer
46
views
Exception thrown: read access violation.Unable to read memory
My English is not very good, I'm sorry.I'm a beginner and want to do a problem with Ternary search tree. The problem occurs during the insertion of the node,VS throws an exception in insert_BiTreeNode:...
5
votes
5
answers
602
views
Efficient solution for the same-fringe problem for binary trees
The fringe of a binary tree is the sequence composed by its leaves, from
left to right. The same-fringe problem [Hewitt & Patterson, 1970]
consists of determining whether two binary trees have the ...
2
votes
1
answer
155
views
Trying to understand how to search a BST with a given input
I am trying to figure out how to search a BST via a given input. The nodes in the tree have a randomly generated number as the key, and the value of the node is an object with various attributes.
...
0
votes
0
answers
68
views
Wrong drawing style in repeatable function in C++
I create a programm in C++ to convert an input expression to postfix expression then visualize it out under a binary tree (you can use arrow key to move the graph and press SPACE to go back to menu ...
0
votes
1
answer
51
views
Creating a balanced BST in Python
I'm trying a implement a complete BST tree, currently I have only created a balanced tree. The idea is to move all the leaves from the balance tree to the left of the left subtree to create a complete ...
1
vote
1
answer
74
views
Debugging an erroneous AVL tree 'insert' operation
I am attempting to create an AVL tree data structure which can handle duplicate element keys. I have based my insertion algorithm on the following: https://www.sanfoundry.com/c-program-implement-avl-...
-1
votes
1
answer
55
views
Error with local variables in recursion in python
I am trying to write a program to find leaf value sequences in python (https://leetcode.com/problems/leaf-similar-trees/description/?envType=study-plan-v2&envId=leetcode-75)
Here is my code
def ...
0
votes
0
answers
32
views
Memory overflow when running graphic code in C++
#include <bits/stdc++.h>
#include <graphics.h>
#define E exp(1)
#define Pi M_PI
using namespace std;
struct tNode{
string data;
tNode *pLeft, *pRight;
};
tNode *root;
tNode *...
0
votes
1
answer
92
views
Duplicate Subtree in a Binary tree Time and space complexity
I saw the following question on GFG to find if there's a duplicate subtree of size 2 or more in a binary tree.
Now, it is written in the practice question requirements and also articles everywhere say ...
0
votes
0
answers
42
views
Issues with passing a pointer via reference in Cpp
void insertNodeInternal(int v, Node* &selectedNode){
if (selectedNode == nullptr) {
selectedNode = new Node(v);
}
else if (v <= selectedNode->GetValue())...
-1
votes
2
answers
101
views
How to pass on values when converting a recursive function to an iterative one with stack and while loop
EDIT: My initial recursive code was incomplete, as I did not multiply by f(l, r) the sum of the two recursive branches.
Given a function f(l, r) that computes something from the nodes (l, r) of a ...
-1
votes
1
answer
25
views
Converting the following recursive binary tree traversal algorithm to an iterative one [closed]
I would like to learn whether the following recursive code can be made more efficient by an iterative implementation, and if so, how such implementation can be done. It has been some years since I ...
0
votes
0
answers
48
views
finding the minimum and maximum paths in a binary tree C#
Help me please that will search for the maximum distance (between the farthest leaves) and the minimum distance between the closest leaves in the tree
I understand that the maximum path will have the ...
0
votes
1
answer
22
views
My binary tree creation code not printing any values
I wrote this binary tree code to construct a tree and then print it:
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
node* r;
node* l;...
-1
votes
1
answer
98
views
Leetcode 235 Lowest Common Ancestor of a Binary Search Tree [closed]
I am trying to solve LeetCode problem 235. Lowest Common Ancestor of a Binary Search Tree:
Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.
...
0
votes
0
answers
41
views
Cannot get Binary Tree to Build Correctly for Project in CMSC 315
Edit: I have corrected my code and the errors have been corrected. I have updated the code in this question with the correct code.
I am stuck on a homework assignment. I have to be able to read in a ...
2
votes
1
answer
86
views
Find the missing value in a complete BST populated by each number from 1 to 2^K, where K is the number of levels
Let K be the number of levels in a binary search tree. Hence, the maximum number of nodes I can have is (2^K)-1. I have a complete binary tree (i.e., to say that each level is completely filled), ...
0
votes
1
answer
44
views
C++: Program for Deleting a node and return its right child:
I was attempting to solve the question for deleting a node in BST, and saw a strange output. In the case where node has only right child, I was trying to delete that node and return its right child. ...
1
vote
1
answer
51
views
What is the problem in my "sumAtBis" code?
i am here to ask about the problem about this function that calculates the sum at a given depth in a tree, it is working in all cases but the last level, the compiler is giving me this :
[Done] exited ...
-2
votes
1
answer
112
views
Binary Tree preorder traversal understanding
This homework assignment ask me to write the preorder traversal of the tree but I'm confuse on how this would work because it visit node, left subtree, right subtree but it doesn't go to the middle. ...
0
votes
1
answer
96
views
check if object is a binary tree in prolog
There is a task for prolog that sounds like this:
write an istree predicate that returns true only if its argument is a binary tree.
Example:
?- istree(t(a,t(b,nil,nil),nil)).
Returns yes
?- istree(t(...
0
votes
1
answer
32
views
Represent a full, but not complete, binary tree with an array structure
I have been reading about binary heaps and I was wondering if there was a similar representation that could be used to represent a non-complete binary tree, if that binary tree happened to also be ...
0
votes
1
answer
57
views
Binary Trees Changing Node vs Changing Value of Nodes
Confused about the pros/cons of moving the entire node vs just changing values in the node when modifying a binary tree. In the specific question, we have to delete the root and than replace it with ...
1
vote
1
answer
99
views
How to create Tree Zipper using Rust?
I am trying to create a Tree Zipper using Rust. It is inspired by this answer but I am using struct fields instead of Vec for the childs.
I want to have a tree of nodes, e.g.:
7
/ \
4 11
/
...
1
vote
2
answers
85
views
in Lua, how to write an iterator for a binary tree?
In Lua, how to write an iterator for a binary tree. e.g. so I could do something like:
for node in tree:visit() do ... end
I've been trying but best I can do is to walks the tree, carrying around ...
-1
votes
1
answer
64
views
A straightforward recursive level-order traversal method?
I have seen answers indicating that there is something intrinsically non-recursive about level-order traversal. I suggest it can be done recursively in a very natural way (Node is defined as expected) ...
1
vote
3
answers
98
views
Creating an instance of a Binary Tree (Programming Standard ML by Robert Harper)
In the online book by Robert Harper (Programming Standard ML, on page 88, we have the following definition of a binary tree:
datatype 'a tree =
Leaf
| Node of 'a branch * 'a branch
and 'a branch =
...
0
votes
1
answer
80
views
Debugging AVL Tree Deletion: Unbalanced Node Not on Deletion Path
I'm working on implementing an AVL tree in C++, and I've run into a situation where, after deleting a node and rebalancing the tree, a node not directly on the path from the deleted node to the root ...
-1
votes
1
answer
95
views
What is the maximum degree of imbalance in a red black tree ? Is it height/2?
The goal of the red-black tree structure is to have a tree that is only approximately balanced. What is the maximum degree of imbalance possible?
I think it would be 1 since the maximum diference ...
1
vote
1
answer
39
views
Wrong output when checking whether binary tree is balanced
I am trying to solve LeetCode problem 110. Balanced Binary Tree:
Given a binary tree, determine if it is height-balanced.
Here is my attempt:
class Solution {
boolean c = true;
public ...
0
votes
1
answer
24
views
Why the height of segment tree is O(logn)
I don't get why the height of a segment tree is O(logn).
https://dzone.com/articles/binary-trees-part-1
Based on the article above, the maximum height of a full binary tree with n nodes is (n-1)/2 (-&...
1
vote
1
answer
201
views
Recurrence Relation for Full Binary Tree
For general n, derive a recurrence relation for Bn. Here Bn denote the number of full binary trees with n vertices. I have found out that it is be B(n)=2B(n-1) +1. But after solving this equation, the ...