binary-search-tree

Binary Search Tree toString

泄露秘密 提交于 2019-12-08 11:35:52
问题 I am having trouble printing out a binary search tree in the format my professor wants. His format looks like this: {(12,10,13),(10,8,11),(8,6,9),(6,4,7),(4,2,5),(2,1,3),(1,*,*),(3,*,*),(5,*,*),(7,*,*),(9,*,*),(11,*,*),(13,*,*)} The first number in a subset is the root node, and the left and right nodes are the left and right children. The left child then becomes the root node after a loop iterates. everything works fine until I reach where only one node exists in a subset. It just prints (1,

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-

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 write this JavaScript code for finding if a tree is a Binary Search Tree in fewer lines?

不想你离开。 提交于 2019-12-08 05:19:00
问题 In a quiz for my Javascript class, we were told to make a simple tree and write a function that returns true or false whether it is a BST or not. I got a decent grade, but i got 10 points off because the instructor said "It can be done in 6 less lines". This is what I had: function node(value, left, right){ this.Value = value; this.Left = left; this.Right = right; } //this IS a BST, returns true var head = new node(8, new node(9, null, null), new node(10, new node(9, null, null), new node(14,

How to return a node, uniformly at random, from a binary search tree?

末鹿安然 提交于 2019-12-08 05:14:02
问题 Given a BST (may or may not be balanced) how can one return "any" node uniformly at random? A constraint is you cannot use an external indexing data structure. You must traverse the tree in such a manner that every node has an equal chance of being visited. This question has me perplexed for quite a while. If we can indeed use an external hashtable/pointers we could just randomize on those and return the corresponding node. However, my colleague has put forth a rather complex variant of the

InOrder Traversal in Python

為{幸葍}努か 提交于 2019-12-08 05:06:32
The problem I am tackle with is to find the first occurrence node in its inorder traversal in a BST. The code I have is given below def Inorder_search_recursive(node,key): if not node: return None InOrder_search_recursive(node.lChild) if node.value==key: return node InOrder_search_recursive(node.rChild) This code always return None, what's wrong with it. I think I've return node when I find a node with value k. Why cannot python pass this node???Thanks in advance When you call yourself recursively, like this: InOrder_search_recursive(node.lChild) That's just a normal function call, like any

Find optimal binary search tree structure

非 Y 不嫁゛ 提交于 2019-12-08 04:37:38
问题 In reference to the problem HERE, how can we find the structure of optimal binary search tree. Author says: In the above solutions, we have computed optimal cost only. The solutions can be easily modified to store the structure of BSTs also. We can create another auxiliary array of size n to store the structure of tree. All we need to do is, store the chosen ‘r’ in the innermost loop. But above is not clear to me. Talking about loops, we start with chains of length 2,3 .. and so on. So let us

Binary Search Tree to inOrder Array

﹥>﹥吖頭↗ 提交于 2019-12-08 01:55:12
问题 Pretty easy question: Recursively how can I create an array of a binary search tree (in order) which uses this constructor: public class OrderedSet<E extends Comparable<E>> { private class TreeNode { private E data; private TreeNode left, right; public TreeNode(E el) { data = el; left = null; right = null; } } private TreeNode root; public int size = 0; public OrderedSet() { root = null; } 回答1: In-Order means you first have to traverse the left part of the tree, so: TreeNode tree // this is

Understanding stack unwinding in recursion (tree traversal)

不羁的心 提交于 2019-12-08 01:52:16
问题 I am writing a program to traverse a binary search tree.Here's my code: Main.java public class Main { public static void main(String[] args) { BinaryTree binaryTree = new BinaryTree(); binaryTree.add(50); binaryTree.add(40); binaryTree.add(39); binaryTree.add(42); binaryTree.add(41); binaryTree.add(43); binaryTree.add(55); binaryTree.add(65); binaryTree.add(60); binaryTree.inOrderTraversal(binaryTree.root); } } Node.java public class Node { int data; Node left; Node right; Node parent; public