binary-tree

Are there any efficient ways to populate a balanced tree structure

这一生的挚爱 提交于 2021-02-10 15:52:20
问题 I have a balanced binary tree structure: Node 0 at depth 0 is the root. The root's left child is 1 and right child is 2 , and so on. Please see image: The total depth of the tree is given as N . This N is the only parameter of the problem. Nodes at level N are designated as leaf nodes. I am storing this tree using the following node structure. struct node_s{ int n, depth, parent;//n is node number int nodescendents;//number of descendents of the current node std::vector<int> descendents;/

Binary Tree preorder, postorder and inorder with java

纵然是瞬间 提交于 2021-02-05 11:28:05
问题 I am studying my second semester in computer science, in my data structures class we are seeing binary trees with recursion. We have to make the preorder postorder and inorder traversal with recursion based on the following Java code: public class BinaryTree { Node root; public void addNode(int key, String name) { // Create a new Node and initialize it Node newNode = new Node(key, name); // If there is no root this becomes root if (root == null) { root = newNode; } else { // Set root as the

All Paths for a Sum with return issues

放肆的年华 提交于 2021-02-05 08:17:17
问题 I have a question in finding the all paths for a sum. The question is: Given a binary tree and a number ‘S’, find all paths from root-to-leaf such that the sum of all the node values of each path equals ‘S’. My approach with recursion is: def all_sum_path(root, target): result = [] find_sum_path(root, target, result, []) return result def find_sum_path(root, target, result, new_path): if not root: return None new_path.append(root.value) diff = target - root.value if not root.left and not root

Binary Tree: summation of all nodes between a min and max value

旧巷老猫 提交于 2021-01-29 16:46:00
问题 I have an assignment where I am given the root of a randomly generated BST. I am given randomly generated test cases for this assignment. The assignment description is the following: You are given the root node of a binary search tree, T, and two integers: min, and max. Note that min and max are not necessarily stored in the tree. Determine the sum of all keys stored in T that are larger than or equal to min, and smaller than or equal to max. Implement your algorithm recursively I am not

Create array tree of nth level form array list

荒凉一梦 提交于 2021-01-29 13:02:46
问题 I have only Nth number of level array return, this function return all children list $arr = array( array('id'=>100, 'parentid'=>0, 'name'=>'a'), array('id'=>101, 'parentid'=>100, 'name'=>'a'), array('id'=>102, 'parentid'=>101, 'name'=>'a'), array('id'=>103, 'parentid'=>101, 'name'=>'a'), ); $new = array(); foreach ($arr as $a){ $new[$a['parentid']][] = $a; } $tree = createTree($new, array($arr[0])); print_r($tree); function createTree(&$list, $parent){ $tree = array(); foreach ($parent as $k=

Function that determines the validity of a binary tree by checking if there are loops

半城伤御伤魂 提交于 2021-01-27 19:37:17
问题 I have to complete the function bool HasLoop(Node* root) { } which determines the validity of a binary tree by checking if it has any loops. So an Example: Valid: X1 / \ X4 X5 \ \ X3 X7 Invalid: X1 / \ X4 X5 \ / \ X3 X7 My idea is to mark each node we traverse as visited, and if we come across a visited node again, we would know a loop exists. But most of the examples involve sets, and our class hasn't gone over that yet. How do I proceed? EDIT: WHAT I CAME UP WITH: struct Node { int data;

Yield all root-to-leaf branches of a Binary Tree

淺唱寂寞╮ 提交于 2021-01-27 14:50:22
问题 Sorry if this is a common question but I haven't found an appropriate answer for my particular problem. I'm trying to implement a walk method that walks a binary tree from its root node to each of its leaf nodes, yielding the root-to-leaf path whenever I get to a leaf node. For example, walking the binary tree represented by: __a__ / \ b d / \ / \ - c - - Would yield: ['a', 'b', 'c'] ['a', 'd'] My idea is that BinaryTree.walk calls Node.traverse on the root node, which in turn calls the

binary search tree path list

僤鯓⒐⒋嵵緔 提交于 2021-01-27 13:47:36
问题 This is code to get all the root to leaf paths in a Binary Tree but it puts in all the paths concatenated into one path. What is going wrong with the recursive call? private void rec(TreeNode root,List<Integer> l, List<List<Integer>> lists) { if (root == null) return; if (root.left == null && root.right == null ) { l.add(root.val); lists.add(l); } if (root.left != null) { l.add(root.val); rec(root.left,l,lists); } if (root.right != null) { l.add(root.val); rec(root.right,l,lists); } } 回答1:

What is the most efficient way to build an unsorted binary tree in java?

末鹿安然 提交于 2021-01-27 04:42:11
问题 I need to create an unsorted binary tree (one requirement is that it is unsorted) that holds a String as its value. My class outline looks like this: public class Node { private String desc; private Node leftNode = null; private Node rightNode = null; public Node(String desc) { this.desc = desc; } public String getDesc() { return desc; } public Node getLeftNode() { return leftNode; } public Node getRightNode() { return rightNode; } } Eventually I want to be able to replace any node that

Binary Tree - Method to recursively count the number of nodes on a level without a count parameter (Java)

不羁的心 提交于 2021-01-08 05:29:40
问题 I'm looking to take some code I've written in Java for a binary tree class and remove the count parameter from the arguments, but keep the whole thing recursive. So, given a class with these variables: public class BinaryTree<E> { protected E data; protected BinaryTree<E> left,right; How could I do that for: public int levelCount(int count, int level){ if (data == null) {return 0;} if (count == level) {return 1;} else { return this.getRight().levelCount(count+1,level) + this.getLeft()