Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [tree]

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.

2
votes
2answers
47 views

Printing a K-ary tree stored in level-order in an array

I am given an array that represents a k-ary tree (I am also given the branching factor "k") that is stored in level order. My task was to print the tree in level order. For example a trinary tree ...
0
votes
0answers
23 views

Functional, recursive algorithm for flattening a tree

My goals for the algorithm were to use recursion, to avoid mutation, and to decouple the tree walking from the action that we take on each node. My sense is that the result uses mode code is ...
0
votes
0answers
23 views

Lowest Common Ancestor(LCA) code in python

I have searched for efficient code to find LCA recursively but did not find it, so I have tried my self. Please some one review this code for finding LCA in python ...
1
vote
1answer
114 views

Binary search tree insertion in Racket

I am learning Racket and implemented a BST insert function (insert tree n) where the format for a BST node is ...
2
votes
2answers
60 views

Collect members from custom tree nodes

In my program, I have a tree of Group nodes, each identified by their ImportId. Each group has zero or more members, that I ...
1
vote
2answers
84 views

Graph library including minimum spanning tree using Kruskal's Algorithm

I've only ever made small programs. So, I've tried to make something large but easily reusable/extendable. Posted below are the beginnings of a graph library I've written up. One particular point I'd ...
2
votes
1answer
44 views

Pramp: Sales path

the question is taken from Pramp(really cool site!) there is a more straight forward solution. using recursion. but I thought trying it using BFS. please review only the code of GetCheapestCost ...
1
vote
0answers
27 views

AVL Tree with delete method in Python 3

Just a general code review. I'm looking for my first developer job so anything constructive is awesome. Please, and thank you very much. I've written this implementation as an exercise and to add to ...
2
votes
1answer
59 views

Binary search tree with common operations

I trying to see if I implemented these operations correctly and if they could be optimized in any way. The implementations are just the typical implementations found online but I adjusted them to work ...
3
votes
1answer
87 views

C++ inorder traversal of binary tree

The challenge was to create a function which performs an in order traversal without using recursion. What I came up with was to use a stack and some clever popping and pushing. I was wondering if ...
5
votes
2answers
164 views

Generic tree data structure for flat data from a database

Could someone please review my code for performance(any other suggestions are welcome) which converts flat data list coming from database to a tree? Interface for db entity class ...
4
votes
1answer
87 views

Maximum depth or height of a Binary tree

The Following function is supposed to calculate the maximum depth or height of a Binary tree the number of nodes along the longest path from the root node down to the farthest leaf node. ...
1
vote
1answer
42 views

Traversing the Parent-child relationship between objects: Part 2

This code builds on the code in the previous iteration. All of the following tests should work: ...
3
votes
1answer
83 views

Monoidal tree interview in Haskell

I was working on the following interview question: Given an array of integers, return a new array such that each element at index i of the new array is the ...
3
votes
0answers
100 views

Pretty print a tree

When you're working with trees one of the most useful methods to implement is a clear way to print them, the following is my solution to print a tree in Java: ...
1
vote
2answers
49 views

Binary Search Tree delete function

I have implemented this delete function for BST. Tt takes care of all the three cases, deleting node with 0, 1, or 2 children. I have added comments throughout to explain my decisions. Is it sloppy ...
5
votes
1answer
67 views

Generating all unlabeled trees with up to n nodes

I want to visualize all unlabeled trees with \$n\$ or fewer nodes, not just count them. First idea/attempt: Take a list of all \$n-1\$ node trees, then append a new leaf to every tree in every way to ...
4
votes
1answer
89 views

Implementing a tree parsing function

I've solved an assignment a week ago, which is my first assignment using Lisp. Tasks: Task 1: a. Given a tree as recursive lists, print the tree in breadth first order. b. Given a tree like ...
3
votes
0answers
83 views

All the paths from the root to the leaves

Given a binary tree, return all root-to-leaf paths. Example: -- 1 / \ 2 3 \ 5 Output should be: ["1->2->5", "1->3"] My approach: I walk the branches ...
4
votes
1answer
55 views

ZigZag tree travesal

The question is taken for a coding interview I had, it is also in geeks for geeks, https://www.geeksforgeeks.org/zigzag-tree-traversal/ you are given a binary tree and you need to print it in a ...
0
votes
1answer
54 views

Binary search tree traversal and depth first search in Python

I am learning about the tree traversal, and I would like to ask for feedback. Using recursion, write a function that takes in a binary search tree and number of outputs a list of values ordered by ...
7
votes
2answers
212 views

Iterator for BFS binary tree traversal

I implemented an iterator pattern for C# using IEnumerator and IEnumerable. The goal of the implementation is to traverse a ...
3
votes
2answers
80 views

First Swing at an AVL Tree Implementation in C++

I've implemented a simple AVL binary search tree. For no real reason I decided to implement this without multiplicity, so if we try to insert(5) twice, it's only stored once in the tree. I've also ...
0
votes
1answer
49 views

Binary Tree Longest Consecutive Sequence

I written code to solve this problem which I found on leetcode. My solution worked for the vast majority of the test cases run against it but failed on 2 for some reason. The first test case it failed ...
4
votes
2answers
69 views

Macro to convert an indented hierarchy to a database format

Our business intelligence suite outputs data as an indented hierarchy, as below: ...
1
vote
1answer
65 views

BST implementation in Python

I was asked to perform the following tasks at one of the technical interviews. I did the BST implementation in python. I think the solution passed all of the solution. I broke down the tasks in the ...
3
votes
1answer
73 views

Multi-threaded tree search

This is my first nontrivial multi-threaded program. It recursively searches through a tree (haystack) to find the leaf where ...
5
votes
1answer
296 views

Traversing the parent-child relationship between objects

I wrote this code because I was often annoyed that I couldn't get the parent object. This class allows you to traverse the parent-child relationship between objects, in both directions. It also ...
0
votes
1answer
99 views

create sorted vector of content of two binary search trees in sorted order

You are given two binary search trees, the goal is produce a sorted array of elements containing elements from both the trees. I wanted to know if there is a simpler approach, and if the use of ...
2
votes
3answers
184 views

Insertion of a node in M-way search tree

I am currently studying data structures and i am looking to see if my implementation of the insertion method in an M-way search tree is good and i am looking forward to improving my coding approach. ...
4
votes
2answers
253 views

Generic Binary Tree

I am a mathematician attempting to become proficient with C++. At the moment I am learning about data structures. I am now writing a binary tree data structure using linked list from scratch. This ...
3
votes
0answers
55 views

Binary Search Tree in Python 3

I just implemented binary search tree as described in Cormen's algorithm book. I'm not interested in correctness or performance of the implementation. I'm looking for reviews, comments on code style ...
2
votes
1answer
107 views

B-Tree implementation in secondary-memory/disk-memory

I will start by saying this implementation was based on the approach taken with B-Trees in the book: Introduction to Algorithms, 3rd Edition - Cormen, 2011 The implementation stores all the b-tree ...
3
votes
2answers
88 views

Binary Tree max sum conditionally

Problem : A binary tree is given as an input, each node of binary tree contains one integer value. Find the maximum sum of collection of nodes such that following two conditions are met. if node's ...
5
votes
2answers
193 views

Better generic binary search tree in C++

Introduction This is a follow-up to my previous question. This code rocks new ground-up implementation of core functionality, which I made from combining @vnp’s and @JDługosz's answers. The core ...
1
vote
2answers
139 views

Find all root to leaf paths in binary tree

Description: Given a binary tree, return all root-to-leaf paths. Leetcode ...
2
votes
1answer
187 views

Find all k-sum paths in a binary tree

Description: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. Note: A leaf is a node with no children. Leetcode Code: ...
2
votes
1answer
98 views

Find minimum depth in a binary tree

Description: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Note: A leaf is a node ...
2
votes
0answers
44 views

Python multiprocessing for tasks depending on each other

I have a simulation that runs a process on a tree structure, where the slow processes down processes are separate from each other. This looked like a thing that would be a prime candidate for ...
1
vote
1answer
72 views

Looping through a tree to get branch or leaf

This is my solution to a problem from MIT's 6.034 Artificial Intelligence course (see the heading Tree reference on 6.034 Artificial Intelligence, Lab 0). Can you please help me improve my solution (...
3
votes
1answer
71 views

Purely functional brodal heap implementation

I implemented the Brodal heap from Brodal & Osaki 1996: Optimal Purely Functional Priority Queues in haskell. The paper gives an implementation in ML but without higher-kinded types. The goal was ...
2
votes
1answer
59 views

Minimum Sum Path of Pyramid with Data Structures

I have a program that uses ArrayLists to store a pyramid that has been read by a txt file and calculates the minimum sum value of travelling from the top of the pyramid to the bottom. My code's main ...
5
votes
2answers
228 views

Generic in order traversal iterator for binary trees

This actually started off as member type for the binary_search_tree, but after realizing that it doesn't compare to values or use the value objects inside of it, I ...
7
votes
2answers
427 views

Generic binary search tree in C++

Introduction This is yet another data structure I'm going over again for the algorithms course. This time it is binary search tree. Implemented operations: Insert Exists Remove Clear Move ...
4
votes
0answers
371 views

Compression/decompression using Huffman Coding Algorithm

I am writing a program that compresses and decompresses data using the Huffman Coding algorithm. About compression: this program will export 2 files: header file (contains characters' frequency for ...
1
vote
1answer
43 views

Overloading Java function (insert into binary tree)

I have a binary tree below and it seems to make sense to overload the insert() method. If insert() is called without a ...
6
votes
3answers
199 views

Recursive Quadtree implementation in C++

I've been working on a recursive quadtree in C++. How can I improve my code? My primary goals are improving the performance/speed, as well as practice. QuadTree.h ...
0
votes
1answer
81 views

Balanced tree in C#

Given the adjacency list to represent a tree from the root (node 0) to the leaves, I want to write a function that takes this adjacency list as a parameter and return whether the tree is balanced. ...
2
votes
1answer
97 views

Implementing Complete Tree by Layers

I implement a complete tree as a layer for adding a hash, heap, and other things on top of it. The Complete_tree_node templates can be reused regardless of the number of layers added to the tree. ...
3
votes
1answer
127 views

AVL-Tree Implementation in Java

I'm looking for feedback on this AVL tree implementation. Particularly on the overall structure, readability and abstraction. I've tried to keep all AVL specific operations inside the ...