binary-search-tree

BST with duplicates

百般思念 提交于 2019-12-04 21:47:38
问题 I know that, BST does not allow duplicates. For example, if I have a word "RABSAB". The Binary search tree for the above string is: R /\ A S \ B What if we wanted to include the duplicates in the tree. How the tree gonna change? I was asked this question in an interview. They asked me to draw: a binary tree an unbalanced Binary Search Tree a binary search tree without duplicates a binary search tree with duplicates Any Help is appreciated! PS: Help me by drawing the related trees 回答1: Rule to

Prove the efficiency of repeated calls to successor() in binary trees?

落爺英雄遲暮 提交于 2019-12-04 21:38:55
问题 I need a hint for this exercise from the CLRS Algorithms book: Prove that no matter what node we start at in a height-h binary search tree, k successive calls to Tree-Successor take O(k+h) time. 回答1: Let x be the starting node and z be the ending node after k successive calls to TREE-SUCCESSOR. Let p be the simple path between x and z inclusive. Let y be the common ancestor of x and z that p visits. The length of p is at most 2h , which is O(h) . Let output be the elements that their values

Parsing and building S-Expressions using Sets and binary search tree

别说谁变了你拦得住时间么 提交于 2019-12-04 21:29:12
This is pseudo homework (it's extra credit). I've got a BST which is an index of words that point to the lines (stored somewhere else) that contain the words. I need to implement a way to search using s-expressions so I can combine and (&) and or (|). At the command prompt a user could type something like: QUERY ((((fire)&(forest))|((ocean)&(boat)))&(water)) Essentially that should return all lines that contain the words fire, forest and water as well as all lines that contain ocean, boat and water. What I really need help with is the logic for parsing and inserting nodes into the tree to

Haskell label a binary tree through depth-first in-order traversal

被刻印的时光 ゝ 提交于 2019-12-04 20:53:24
I need to label a binary tree through depth-first in-order traversal and I figured therefore I'd need to first go through the left branches of the tree and label those, then do the same for the right branches. My binary tree only stores values at the internal nodes (not at end nodes / leaves): label :: MonadState m Int => Tree a -> m (Tree (Int, a)) label (Branch l x r) = do n <- get l' <- label l r' <- label r return (Branch l' (n, x) r') label (Leaf) = return Leaf *EDIT: Need to use the State Monad however I'm not really grasping the usage of it. My current code is shown above but is not

Java : How do I implement a generic Binary Search Tree?

穿精又带淫゛_ 提交于 2019-12-04 20:22:01
问题 Until now, I have been writing a Node class as class Node { private value; private Node left; private Node right; public int getValue() { return value; } public void setValue(int value) { this.value = value; } public Node getLeft() { return left; } public void setLeft(Node left) { this.left = left; } public Node getRight() { return right; } public void setRight(Node right) { this.right = right; } } and Binary Search Tree as public class BinarySearchTree { private Node root; public

To print the boundary of Binary Tree

懵懂的女人 提交于 2019-12-04 17:51:44
问题 I have been asked in an interviews to print the boundary of the Binary Tree. For example. 1 / \ 2 3 / \ / \ 4 5 6 7 / \ \ 8 9 10 Answer will be : 1, 2, 4, 8, 9, 10, 7, 3 I have given the following answer. First Method : I have used a Bool variable to solve the above problem. void printLeftEdges(BinaryTree *p, bool print) { if (!p) return; if (print || (!p->left && !p->right)) cout << p->data << " "; printLeftEdges(p->left, print); printLeftEdges(p->right, false); } void printRightEdges

convert Sorted Linked List to Balanced BST

浪子不回头ぞ 提交于 2019-12-04 15:14:12
I am working on below interview question: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. I am trying to understand below solution and its complexity? Can someone help me understand how it works? Is below solution O(n) time complexity and O(log n) space complexity? Also is below algorithm better than "counting the number of nodes in the given Linked List. Let that be n. After

Flatten binary search to in order singly linked list [C]

醉酒当歌 提交于 2019-12-04 13:34:48
问题 I am trying to flatten a binary search tree to a singly linked list. Binary search tree: 6 / \ 4 8 / \ \ 1 5 11 / 10 Flattened Singly Linked List: 1 -> 4 -> 5 -> 6 -> 8 -> 10 -> 11 I can't seem to figure this out for some reason. I have a struct for the tree nodes: typedef stuct node { int key; struct node *left; struct node *right; } Node; I have a function to create and allocate memory to the tree node: Node* newNode (int key) { Node *new = malloc (sizeof(Node)); new->left = NULL; new-

How many permutations of a given array result in BST's of height 2?

佐手、 提交于 2019-12-04 12:08:44
问题 A BST is generated (by successive insertion of nodes) from each permutation of keys from the set {1,2,3,4,5,6,7}. How many permutations determine trees of height two? I been stuck on this simple question for quite some time. Any hints anyone. By the way the answer is 80. 回答1: Consider how the tree would be height 2? -It needs to have 4 as root, 2 as the left child, 6 right child, etc. How come 4 is the root? -It needs to be the first inserted. So we have one number now, 6 still can move

Array to Binary Search Trees Quick

霸气de小男生 提交于 2019-12-04 10:04:29
Given an array of integers, is there a way to convert it into Binary Search Tree (unbalanced) quickly? I have tried inserting it one by one for each element, but this means that I have to traverse from the beginning for each insertion. It works perfectly, but I think the worst case is O(N^2) for being unbalanced, e.g. the array is sorted. Given a large N, I think it is going to take some time. Back to my question, is there a way to do this faster than the algorithm that I stated? For example, given array [4,5,2,3,1], is there a fast way to create this? 4 / \ 2 5 / \ 1 3 Yes, there is easy way