binary-tree

c++ my binary tree is well formed [duplicate]

安稳与你 提交于 2019-12-08 10:19:59
问题 This question already has answers here : How do you validate a binary search tree? (30 answers) Closed 5 years ago . i propose a recursive implementation for checking whether binary search tree is valid: /* Return true if binary tree is a binary search tree */ bool BinaryTree::isBinarySearchTree(BinaryTree* tree, int& prev) { if(!isBinarySearchTree(tree->left, tree->data)) // left return false; if(tree->value > prev) // here return false; else prev = tree->value; return isBinaryTree(tree-

Draw a binary tree

南笙酒味 提交于 2019-12-08 08:21:33
问题 I'm looking for a js lib which allows the user to draw a binary tree : add/remove a leaf, add/remove a parent node, etc. I've found many libs but most of them are made for data visualization only (eg: d3), not drawing from the browser. Does this even exist ? Thanks! 回答1: Tree-model-js has animated example on main page by svg. You can copy and use it. 回答2: Have a look at existing stuff to draw graphs (a tree is a graph): Pure JavaScript Graphviz equivalent You may also write it yourself by

Creating a binary search tree in C99

时间秒杀一切 提交于 2019-12-08 08:08:24
问题 I've got a programming class assignment due tonight at 8 PM CDT that I'm having trouble with. We are to take a list of the following numbers via reading a file: 9 30 20 40 35 22 48 36 37 38 place them in an array (easy enough), and then read these into a binary search tree using C. The first number in the list is the number of elements in the tree. The rest are placed into the following struct: typedef struct node_struct { int data; struct node_struct* left; struct node_struct* right; } Node;

Can't Add 1,000,000 Elements to Binary Search Tree in Java

爱⌒轻易说出口 提交于 2019-12-08 07:09:15
问题 I am making an Binary Search Tree as an assignment. I am having problem when i try to add 1,000,000 elements. I'm getting error after 15,000 elements inserted: Exception in thread "main" java.lang.StackOverflowError Something wrong with my code i could not find where i did wrong. public class BinarytreeInsert { public static void main(String[] args) { new BinarytreeInsert().run(); } static class Node { Node left; Node right; int value; public Node(int value) { this.value = value; } } public

How to evaluate any given expression

故事扮演 提交于 2019-12-08 07:04:42
问题 How do you, given any string expression, populate a tree? I have an assignment that I am stuck on. I need to evaluate the following expression (( 5 * 10 ) /2 - (( 2 + 3) + 6)) using any data structure. Using a stack, I am able to verify that the string is well formed. But how can I add the various values into a tree and then evaluate them in order. Please give me any hints you may have on how I can read the string ((( 490 * 9 ) / 2)/5/6 - (( 2/4 + 3) + 6 * 5)) For instance, How do I get the (

Find Lowest Common Ancestor BST when one or both node(s) doesnt exist

[亡魂溺海] 提交于 2019-12-08 06:32:02
问题 We can easily use the code to find LCA in Binary Search Tree:- public static Node FindLCA(Node root, Node a, Node b) { if (root == null) return null; if (root.IData == a.IData || root.IData == b.IData) return root; if (root.RightChild != null && (root.RightChild.IData == a || root.RightChild.IData == b)) return root; if (root.LeftChild != null && (root.LeftChild.IData == a || root.LeftChild.IData == b)) return root; if (root.IData > a.IData && root.IData > b.IData) return FindLCA(root

BIT: Unable to understand update operation in Binary index Tree

情到浓时终转凉″ 提交于 2019-12-08 05:21:01
问题 I have just read answer on this question and was very satisfied and it is indeed a fantastic answer. It taught me the working of BIT. But at the end, the second last paragraph is where I am struggling. It says, Similarly, let's think about how we would do an update step. To do this, we would want to follow the access path back up to the root, updating all nodes where we followed a left link upward. We can do this by essentially doing the above algorithm, but switching all 1's to 0's and 0's

Binary Search Tree Implementation

早过忘川 提交于 2019-12-08 04:56:57
问题 I've searched the forum, and tried to implement the code in the threads I found. But I've been working on this real simple program since about 10am, and can't solve the seg. faults for the life of me. Any ideas on what I'm doing wrong would be greatly appreciated. BST.h (All the implementation problems should be in here, and this has been updated some to reflect further development work, look at the history to see old versions.) #ifndef BST_H_ #define BST_H_ #include <stdexcept> #include

Select a Node at Random from Unbalanced Binary Tree

≯℡__Kan透↙ 提交于 2019-12-08 02:45:17
问题 One of my friends had the following interview question, and neither of us are quite sure what the correct answer is. Does anyone have an idea about how to approach this? Given an unbalanced binary tree, describe an algorithm to select a node at random such that each node has an equal probability of being selected. 回答1: You can do it with a single pass of the tree. The algorithm is the same as with a list. When you see the first item in the tree, you set it as the selected item. When you see

Traversing an imbalanced binary tree in array form

孤人 提交于 2019-12-07 22:28:08
问题 An imbalanced (or non-heap) binary tree can be represented using an array as follows: array = [1, 2, null, 3, 4, 5, 6, null, 7, 8, null] 1 / \ 2 null / \ 3 4 / \ / \ 5 6 null 7 / \ 8 null How can I do a tree traversal using the given array? More specifically, how can I calculate a parent's left and right children's indices? In a balanced tree (such as a heap tree), we can easily calculate two children indices using their parent's index, and vice versa, as follows. leftChildIdx = 2 * parentIdx