Questions tagged [binary-tree]
A tree data structure in which each node has at most two child nodes.
6,777
questions
-2
votes
0
answers
33
views
Binary Tree pre order transversal
treePreOrder :: BT a -> [a]
treePreOrder Empty = []
treePreOrder (Fork x l r) =
[x] ++ treePreOrder l ++ treePreOrder r
I am trying to generate all the possible trees from the list of pre ...
1
vote
3
answers
61
views
Iterative inOrder Binary Tree
I have the following class for the creation of a Binary Tree:
class BinaryTree:
def __init__(self):
self._root = None
self._left = None
self._right = None
def root(...
0
votes
2
answers
69
views
Simple database in C using a binary search tree
I was tasked with writing a simple database in C. The program would receive instructions in stdin and print their result. The instructions would have the following format: <operation>: <year&...
0
votes
1
answer
27
views
Binary Tree Delete Implementation
I'm attempting to implement a simple binary tree in Dart. My insert and search seem to work just fine, but my delete/remove does not.
void delete(int targetData) {
BinarySearchTreeNode? target = find(...
-1
votes
0
answers
34
views
why am i getting java.lang.NullPointerException at line 30? [duplicate]
We have a binary tree, find all duplicate subtrees. For each duplicate subtree, we only need to return the root node of any one of them. Two trees are duplicates if they have the same structure with ...
0
votes
1
answer
75
views
Understanding the Functionality of a Binary Tree Implementation in C
I am having trouble with the delete function to remove a node from a binary tree. This is my first tree program and I find the books and internet pages very confusing for the delete function. I wrote ...
0
votes
1
answer
42
views
How can I correctly traverse a tree?
The following code inserts some nodes in a tree and displays the inorder and preorder traversals of the same tree. The inorder traversal seems to be right because the nodes are arranged in an ...
1
vote
1
answer
33
views
Python pre order recursive binary tree traversal
I have this code:
def recursionTravel(node, returnArr = []):
if node:
returnArr.append(node.data)
if node.left: returnArr.append(recursionTravel(node.left, returnArr))
if ...
-4
votes
0
answers
64
views
Error in finding the left most node in binary tree
I have a binary tree and I am trying to find the left most node. So each node has a value and has its left and right value and the leaves' left and right values are None.
z = self
z = z.left
I wanted ...
0
votes
0
answers
87
views
how to use a binary tree to get partial sumation of a stack in log(n) time complexity
I am almost done with a custom data structure but I'm stuck on this last feature since I have little to no experience working with trees. I need to add a function that can get the sum of the first k ...
1
vote
2
answers
75
views
problem in finding a common ancestor for two nodes
I have the following code for finding the common ancestor of two nodes in a binary search tree. If the code can find the nth common ancestor that common ancestor will be returned and if there is no ...
0
votes
1
answer
75
views
Binary Tree Iterator in C++ works for T=int but Fails for T=std::string
I have implemented a binary tree with an iterator in C++:
#include <iostream>
#include <memory>
#include <iterator>
#include <iostream>
namespace binary_search_tree {
...
0
votes
1
answer
50
views
Path Sum - Where am I going wrong?
Leetcode problem #112 - Path Sum
Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals ...
0
votes
2
answers
92
views
Return the number of nodes in Fibonacci Recursion
I want to write a function that returns the number of nodes in the Fibonacci recursion tree, knowing that the number of nodes equals the number of additions required to compute the n-th Fibonacci ...
0
votes
0
answers
8
views
Non Symmetric XGBoost – Tennis Match Predictions
I'm working on developing an algorithm to predict the outcomes of tennis matches, focusing on player matchups. The core of my model is an XGBoost classifier. I've encountered a puzzling issue in the ...
1
vote
1
answer
35
views
Why does my binary tree deletion remove entire left portion of tree?
I have an assignment in which i need to make an implementation of a binary search tree in C. In my attempt of implementing a delete function for the tree i have failed to make an implementation that ...
1
vote
3
answers
71
views
How to spread an infected node to its adjacent nodes and eventually to the whole binary tree?
I want to iteratively return the state of the binary tree until the infection is unable to spread to a new node. So how it goes is that the virus will spread to any directly adjacent healthy node and ...
-2
votes
0
answers
61
views
get dot-bracket representation of the binary tree
I'm having an issue getting the dot-bracket representation of a binary tree.
• the dot-bracket representation of a tree with no nodes is the string "."
• the dot-bracket representation of a ...
0
votes
0
answers
22
views
method compareTo in class Node<E#2> cannot be applied to given types
i was building a binary tree by myself but this error occured:
LinkedBinaryTree.java:111: error: method compareTo in class Node<E#2> cannot be applied to given types;
if(leftNode.compareTo(...
0
votes
0
answers
18
views
binary tree width is given how to find its height?
how to find the maximum and minimum height of a binary tree which has maximum width and N nodes?
my best guess is since when a tree has maximum width it means on each level it has maximum nodes. thus ...
0
votes
2
answers
60
views
How to write a function to navigate through a non-binary tree?
I have a company organisation chart I built in React/Nextjs using the 'react-organizational-chart' npm package.
I would like the user to be able to navigate up/down and side to side on the non-binary ...
-1
votes
1
answer
40
views
Worst-case running time to print binary search tree
What is the worst-case running time to print out all the values sorted in a binary search tree containing N positive integers in ascending order?
I guessed it’s O(n) since n is number of elements that ...
0
votes
0
answers
59
views
Use return to stop recurisve function in binary tree, but it doesn't stop
Here is my Python code. I want the inorder_Node() function to stop at Node J so I use return before self.right.inorder_Node().
class TreeNode:
"""
Representing a tree node ...
0
votes
0
answers
24
views
Given 2 points (x,y -> latitude and longitude), find the Trip whose origin and destiny is closer - 2D Tree
Essentially I have an AVL of Trips (each node has, among other things, a Map of TripPhases - essentially data regarding the trip on each of its timestamps. 2 of the attributes are Latitude and ...
1
vote
2
answers
78
views
Define a class member that can be one of either two classes
I am working with a tree that contains two different types of nodes, inner and outer ones. I would like to have members 'leftChild' and 'rightChild' in the inner node class that can point to either an ...
-1
votes
0
answers
35
views
I'm working on a program compiled in the command line that takes a text file and places it into a binary tree, then either indexes or searches
This is essentially what I have so far regarding the Binary Tree.
From what I know, I'm pretty sure I'm supposed to create a Binary Tree in the MyBinarySearchTree. The length of the tree itself should ...
0
votes
1
answer
63
views
Need to write a C++ function that searches for a specific element in a binary tree
Need to write a C++ function that searches for elements with the value of the zodiac_sign field specified by the user in the binary tree and if there is no such element, print the corresponding ...
-1
votes
1
answer
41
views
Why does this code for a recursive preorder traversal on a binary tree work? Shouldn't it get stuck at the first recursive call?
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class ...
-1
votes
0
answers
40
views
Trying to execute a binary tree program
When I try to run the following code, it gives me the error :
"Assertion failed: (current->left->data==values[i]), function add, file coding.cpp, line 27."
#include <iostream>
#...
0
votes
0
answers
31
views
how to describe a recursion in an non static method. Now the recursion is not working
I want to go through a binary tree recursive in the method public void inorder(). Afterwards I want to give out all the values which are in the binary tree. But in my case the recursive process does ...
0
votes
1
answer
110
views
How to Print the Postorder Traversal of a Binary Tree Given the Preorder and Inorder Traversals?
I was given a task of trying to print the postorder traversal of a binary tree when you are given the preorder and the inorder traversals.
I went online to search up the problem, but the only results ...
0
votes
0
answers
31
views
How do I create a method to get the size of a sub-tree or even the tree of a Binary search tree in java
`
private int size(Node node)
{
Node n = node;
if(n == null)
{
return 0;
}
else
{ int leftside = size(n.left);
int ...
0
votes
2
answers
127
views
Delete node in binary tree
I got a segmentation fault when trying to print tree after node's deletion.
#include <stdio.h>
#include <stdlib.h>
typedef struct tree {
int val;
struct tree *left, *right;
} tree;...
1
vote
1
answer
54
views
why this function not work in create BinTree
I want create this BinTree .
in this function it can't work.
typedef struct BinNode {
Elemtype data;
struct BinNode* left;
struct BinNode* right;
}BinNode,*BinTree;
void createBinTree(...
0
votes
0
answers
35
views
Space complexity of checking if binary tree is balanced
I'm trying to understand the time and space complexity of this code for determining whether a Binary tree is balanced:
def height(t):
if t is None:
return 0
return max(height(t.left), ...
0
votes
1
answer
58
views
What is the simplest AVL tree structure to demonstrate a complete right rotation?
I'm learning about AVL trees and their rotations in data structures. I wish my lectures had showcased the simplest complete right rotation because I found the topic became way easier for me when I ...
0
votes
0
answers
13
views
Related to insertion of an element in a binary search tree
This is the method im using to insert in BST.
class BinarySearchTree:Tree{
public:
void add(int data){
if(root==nullptr){
createRoot(data);
}
else{
if(data>=root->...
0
votes
1
answer
78
views
Converting Prefix to Postfix expression in python
I am a beginner and been learning python for a couple of months now.
Trying to work out the subject problem using Tree method but not successful. However, I could not determine which is not working.
...
0
votes
1
answer
31
views
Build speed of Binary search tree depending on order of insertions, but with same height
I have a problem understanding insertion time complexity.
I was solving exercise problems about binary search tree, and am not sure about an answer to this problem.
Consider building two binary search ...
0
votes
1
answer
85
views
Leetcode question: Flatten binary tree into linkedlist
I am trying to solve this problem on leetcode: https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ (Flatten binary tree into linkedlist)
This is my code:
class Solution{
...
0
votes
1
answer
61
views
Why doesn't my word counting binary tree program work correctly?
My program takes read input through getchar(), saves the number of appearances of each word using a binary tree structure and then prints them at the end. Any set of non-blank characters that only ...
4
votes
2
answers
270
views
How can I learn to convert recursive solution to iterative solution while preserving the space complexity?
I am trying to solve a easy binary tree question to convert the existing binary tree to a sum tree.
The recursive solution for this is :
class Solution{
public void toSumTree(Node root){
...
0
votes
0
answers
43
views
How do I set the value of a dependency poperty of a UIControl that has been cast to a Framework Element?
I want to build a UI control for a binary tree - like the ones used in Stochastic Calculus for Finance I: The Binomial Asset Pricing Model
My current implementation has the nodes drawing as I'd like. ...
0
votes
1
answer
34
views
Minimum number of nodes in AVL tree given that the shallowest leaf is at level k
My teacher asked this quesion in class, to find the minumum number of nodes in an AVL tree where the depth of the nearest leaf is K. But there is a catch, like we have to do it in O(1) time and not a ...
0
votes
2
answers
185
views
How to find kth smallest element in a BST when the tree may be modified frequently?
I'm working the problem LeetCode 230: Kth Smallest Element in a BST. My Python code uses recursive inorder traversal, and although it is not directly relevant to this question, is given below for ...
0
votes
1
answer
78
views
Printing binary tree [duplicate]
I have been trying for a while to identify why the tree is printing with indentation for the first time around. I'm trying to make it so that it doesn't print with indentation on the first time.
...
0
votes
1
answer
37
views
Tress DSA (Problem while running the code)
I am trying to find out the top view of a binary tree. The code is running correct on the custom test case but it shows different output on the same input whenever I tried to submit it. The platform ...
0
votes
1
answer
72
views
Leetcode: Binary Tree Paths
I'm solving the following leetcode problem:
Given the root of a binary tree, return all root-to-leaf paths in any
order.
A leaf is a node with no children.
Input: root = [1,2,3,null,5]
Output: [&...
0
votes
1
answer
51
views
Assigning to 'node *' from incompatible type 'void' [closed]
I am implementing a pre-Order Traversal function. But I am stuck at pop() function. It keeps giving this error " Assigning to 'node *' from incompatible type 'void' " . I have tried this ...
0
votes
0
answers
39
views
Leave One Out Cross-Validation for Conditional Inference Trees with a continuous outcome
I'm currently working on a project involving conditional inference trees in R using the partykit package, and I'm trying to perform Leave-One-Out Cross-Validation (LOOCV) because I have a small data ...