binary-tree

Function to traverse a binary tree

本小妞迷上赌 提交于 2020-01-05 05:44:09
问题 I'm just starting with trees and am writing a function that traverses a binary tree and visits every node. I'm calling a function called doSomething(TreeNode *thisNode) for each node in the tree. I want to make sure if what I have is correct and that I'm on the right track? Thanks! void MyTree::Traverse(TreeNode *rt) { If(rt != NULL) Traverse(rt -> left); doSomething (rt); Traverse(rt -> right); } 回答1: Almost, but not quite. The if statement in C++ is not capitalized, and you must add

Data structure for inverting a subarray in log(n)

主宰稳场 提交于 2020-01-05 03:34:43
问题 Build a Data structure that has functions: set(arr,n) - initialize the structure with array arr of length n . Time O(n) fetch(i) - fetch arr[i] . Time O(log(n)) invert(k,j) - (when 0 <= k <= j <= n ) inverts the sub-array [k,j] . meaning [4,7,2,8,5,4] with invert(2,5) becomes [4,7,4,5,8,2] . Time O(log(n)) How about saving the indices in binary search tree and using a flag saying the index is inverted? But if I do more than 1 invert, it mess it up. 回答1: Here is how we can approach designing

Convert binary tree to linked list

两盒软妹~` 提交于 2020-01-04 12:17:28
问题 I am trying to create a linked list from a binary tree. The thing is, is it possible to use a simple linked list instead of the doubly linked list? I tried this: typedef struct arvbin* ABin; typedef struct arvbin { int value; ABin right; ABin left; } arvb; typedef struct slist { int value; struct slist* next; } *SList; void preorder(ABin tree, SList *l) { if(tree) { (*l)->value = tree->value; (*l)->next = (SList) malloc(sizeof(struct slist)); l = &((*l)->next); printf("tese\n"); preorder(tree

V8 JavaScript Object vs Binary Tree

北城余情 提交于 2020-01-04 06:09:39
问题 Is there a faster way to search data in JavaScript (specifically on V8 via node.js , but without c/c++ modules) than using the JavaScript Object ? This may be outdated but it suggests a new class is dynamically generated for every single property. Which made me wonder if a binary tree implementation might be faster, however this does not appear to be the case. The binary tree implementation isn't well balanced so it might get better with balancing (only the first 26 values are roughly

Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE

巧了我就是萌 提交于 2020-01-04 05:39:09
问题 This is a follow-up question of my previous one: Better Class-Structure for logical expression parsing and evaluation Brief introduction: rules as strings combinations of logical-and , logical-or , logical-negation and grouping by parenthesis of identifiers (ID's) Example: "{100} AND (({101} OR {102}) OR ({103} AND {104})) AND NOT ({105} OR {106})" This gets currently evaluated into a binary-tree of nodes, that looks like this: Code taken from here: How to parse a boolean expression and load

Creating all binary trees with unique permutations

蹲街弑〆低调 提交于 2020-01-04 02:48:27
问题 I have a rather silly question which I swear is not homework. For the life of me, I cannot remember whether I ever studied an algorithm to do this, and my mind's eye / creativity is failing me. I have a list of unique nodes. I need to generate all unique permutations of a binary tree containing these nodes. Chirality, in case you are wondering, matters; a binary tree flipped on its axis (left / right) is not the same. Some background information, in case you are wondering: it's for a seed

Recursive Tree Walk with ES6 Promise

核能气质少年 提交于 2020-01-03 04:46:25
问题 I'm looking to walk an object tree of unknown depth and return a given node via an ES6 promise. (used lodash here, obviously not necessary, I realize). I've got the tree walking working fine but I'm a bit unclear the proper method to ensure that the top-level scope variable promise is passed into the recursive function calls so that it's available when calling .resolve( data ) . Right now it attempts to execute on a successful find but fails to resolve the promise since the recursive function

Binary Representation of N-ary Tree

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-03 03:15:09
问题 I am currently trying to make a tree structure with Nodes having an unknown amount of children but I also have to keep track of parent Nodes. I looked at this question N-ary trees in C and made a structure similar to that advised in the link: template <class T> struct treeNode { public: T * data; treeNode *parent; treeNode *kids; //left treeNode *siblings; //right treeNode(); ~treeNode(); treeNode(T data); void treeInsert(T newItem); }; It says that by making the tree in this way, it makes

Construct binary tree from inorder and level traversal

☆樱花仙子☆ 提交于 2020-01-02 14:48:10
问题 Need help finding a way to construct a binary tree given the inorder and level traversal. Is it possible to do it using recursion since the level traversal has to be done by using a queue? 回答1: Here's how you can approach this problem. It's easier to think of how to approach each step by looking from reverse: 8 / \ 4 9 / \ \ 2 6 10 / 1 You have the following: Inorder: 1 2 4 6 8 9 10 Level: 8 4 9 2 6 10 1 Level 1 - Root A level traversal is a left to right, top to down traversal of the tree

JAVA: binary trees

泪湿孤枕 提交于 2020-01-02 04:24:05
问题 Here I am trying to practice making binary trees so that I can do different operations with them. import java.util.*; import java.lang.*; public class Main { public static void main(String[] args) { } } //Building Binary Trees class bTree { static class Node { //remember to initilize a root String value; Node left, right; Node(String value, Node left, Node right) { this.value = value; this.left = left; this.right = right; } Node(String value) //THIS IS A SIBLING CONSTRUCTOR { this(value, null