binary-search-tree

Number of binary tree shapes of N nodes are there with height N-1?

笑着哭i 提交于 2019-12-13 22:02:32
问题 How many binary tree shapes of N nodes are there with height N-1? Also, how would you go about proofing by induction? So binary tree of height n-1 with node n means all node will have only 1 child, sort of chain like structure? So number of binary tree will be different permutation of n numbers which is n. Am I thinking in the right direction? 回答1: You are thinking in the right direction and you have correctly transformed the original problem to a simple one. However what is strange is that

How to find maximum value in any range of an array in log(n) time? [closed]

半城伤御伤魂 提交于 2019-12-13 21:34:37
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . E.g. Array: {1, 5, 2, 3, 2, 10} Range: 0-1 Answer: 5 Range: 2-4 Answer: 3 Range: 0-5 Answer: 10 etc. 回答1: If the array is not sorted,

Why does this search function return a pointer to a pointer?

感情迁移 提交于 2019-12-13 20:05:03
问题 #ifndef _BST_H_ /* Returns negative (left<right), zero (left==right), or positive (left>right). */ typedef int comparator(void* left, void* right); struct bst_node { void* data; struct bst_node* left; struct bst_node* right; }; struct bst_node* new_node(void* data); void free_node(struct bst_node* node); struct bst_node** search(struct bst_node** root, comparator compare, void* data); void insert(struct bst_node** root, comparator compare, void* data); void delete(struct bst_node** node);

Understanding how to calculate the depth of a Binary Tree

落花浮王杯 提交于 2019-12-13 19:25:53
问题 I am going through the Career Cup guide as a crash course through basic CS principals and stuck on the example which calculates the minimum/maximum depth of a Binary Tree. Since this is the same problem I'm having with almost every example I thought I'd post a question here. The instructions are to implement a method that will check to see if a tree is balanced. In order to do this, you need to compare the minimum depth with the maximum depth, and ensure their difference is no greater than 1.

Intersection of 2 binary search trees

依然范特西╮ 提交于 2019-12-13 19:22:51
问题 Hey, So I want to create a new tree which is basically the intersection (mathematical definition of intersection) of 2 given binary search trees. I have a method that prints out all the nodes at a particular level of the tree and I have a method that can find out the depth of the tree.I am pasting my work so far though it is incomplete and I'm stuck with the logic.Help will be appreciated. public static Bst<Customer> intersect (Bst<Customer> a, Bst<Customer> b){ Bst<Customer> result = new Bst

Binary tree - find position in inorder traversal

拈花ヽ惹草 提交于 2019-12-13 18:45:45
问题 I have a binary search tree where i have to implement a method called int valueAtPosition(int x) The problem is, that i need the position in an in order traversal. To find the in order traversal i have this the following code, but i don't know how i count the recursive calls, to get the right position. public void inOrderTraverseTree(Node root){ if(root != null){ inOrderTraverseTree(root.leftChild); System.out.println(root); inOrderTraverseTree(root.rightChild); } } 回答1: You can also use a

Space complexity of validation of a binary search tree

空扰寡人 提交于 2019-12-13 14:06:25
问题 The best algorithm to verify if a binary tree is a BST is given as follows IsValidBST(root,-infinity,infinity); bool IsValidBST(BinaryNode node, int MIN, int MAX) { if(node == null) return true; if(node.element > MIN && node.element < MAX && IsValidBST(node.left,MIN,node.element) && IsValidBST(node.right,node.element,MAX)) return true; else return false; } The space complexity of this logic is apparently O(logN) which I'm assuming is the cost of recursion. How was the value arrived at? 回答1:

BinarySearchTree remove method malfunctioning when removing integers

萝らか妹 提交于 2019-12-13 08:03:45
问题 I wrote the BinaryTree structure from scratch, which included a remove(Object obj) method to remove elements from said tree. It seems to function correctly for every type except an Integer, and I can't figure out why. public BinaryTree<E> remove(Object obj) { try{ E value = (E)obj; int cmp = value.compareTo(this.value); if(cmp == 0) { List<BinaryTree<E>> kids = children(); if(kids.size() == 0) { return new EmptyBinarySearchTree<E>(); } if(kids.size() == 1) { return kids.get(0); } //2 children

BST intersection, NullPointerException

匆匆过客 提交于 2019-12-13 07:46:27
问题 I am trying to create a new BST from the intersection of 2 known BSTs. I am getting a NullPointerException in the intersect2 method int he second case, at the line "cur3.item.set_account_id(cur1.item.get_accountid()+ cur2.item.get_accountid());". I know you get the error when you try to dereference the variable without initializing it but i think i am initializing it? I'm not really sure. I would appreciate the help. public static Bst<Customer> intersect(Bst<Customer> a, Bst<Customer> b){

Convert sorted array to Binary Search Tree (Java) stack overflow exception

限于喜欢 提交于 2019-12-13 07:35:05
问题 This is a program for converting an array, where elements are sorted in ascending order, to a height balanced BST. I input five element, pass them to an array, sort the array and use methods. It produces this error: Exception in thread "main" java.lang.StackOverflowError at Solution.sortedArrayToBST(Node.java:26) How do I fix this error? import java.util.*; class Node { int val; Node left; Node right; Node(int x) { val = x; } } class Solution { public Node sortedArrayToBST(int[] num) { if