binary-search-tree

Delete an element from a binary search tree in F#

半世苍凉 提交于 2019-12-02 04:19:02
I'm trying to write a method to delete an element from a BST. So far, this is what I have. I'm not sure if I'm on the right track or if there is a better way to do it by using pattern matching to match the different delete cases ie: no children, 1 child, 2 children. type 'a bst = NL | BinTree of 'a * 'a bst * 'a bst;; let rec smallest = function | NL -> failwith "tree is empty" | BinTree(m, lst, rst) -> if lst = NL then BinTree(m, lst, rst) else smallest lst;; let rec smallest2 = function | NL -> failwith "tree is empty" | BinTree(m, lst, rst) -> if lst = NL then m else smallest2 lst;; let rec

Binary search tree insertion - root always null

梦想的初衷 提交于 2019-12-02 02:48:24
I have ds code for inserting values in a binary search tree using recursion. The problem is that the root always remains null. Upon execution, the 1st printf() prints 10 but the 2nd printf (after insertRec(10)) does not print anything as root is null. #include<stdio.h> #include<malloc.h> struct llist { int data; struct llist *left; struct llist *right; }; typedef struct llist node; void insertRec(node *r, int num) { if(r==NULL) { r=(node*)malloc(sizeof(node)); r->data=num; r->left=NULL; r->right=NULL; printf("%d ",r->data); //1st printf } else { if(num < r->data) insertRec(r->left, num); else

Binary Search Tree Recursive insert not displaying anything

我是研究僧i 提交于 2019-12-02 02:46:51
问题 I'm doing a small Java work on Binary Search Tree but when I'm implementing the recursive insert of a node into the tree and display it, I don't get anything. I've been on it for a while now, I don't know for sure but I think it's a pass-by-reference problem. Here's my code: public class BST { private BSTNode root; public BST() { root = null; } public BSTNode getRoot() { return root; } public void insertR( BSTNode root, Comparable elem ) { if ( root == null ) { root = new BSTNode( elem ); }

Binary search tree insertion - root always null

人走茶凉 提交于 2019-12-02 02:12:23
问题 I have ds code for inserting values in a binary search tree using recursion. The problem is that the root always remains null. Upon execution, the 1st printf() prints 10 but the 2nd printf (after insertRec(10)) does not print anything as root is null. #include<stdio.h> #include<malloc.h> struct llist { int data; struct llist *left; struct llist *right; }; typedef struct llist node; void insertRec(node *r, int num) { if(r==NULL) { r=(node*)malloc(sizeof(node)); r->data=num; r->left=NULL; r-

Binary Search Tree Recursive insert not displaying anything

孤人 提交于 2019-12-02 01:50:51
I'm doing a small Java work on Binary Search Tree but when I'm implementing the recursive insert of a node into the tree and display it, I don't get anything. I've been on it for a while now, I don't know for sure but I think it's a pass-by-reference problem. Here's my code: public class BST { private BSTNode root; public BST() { root = null; } public BSTNode getRoot() { return root; } public void insertR( BSTNode root, Comparable elem ) { if ( root == null ) { root = new BSTNode( elem ); } else { if ( elem.compareTo( root.element ) < 0 ) { insertR( root.left, elem ); } else { insertR( root

non-void function works fine even without executing return

微笑、不失礼 提交于 2019-12-01 23:31:43
I'm a computer science college student. Yesterday, I have a class about Binary Search Tree using C++. We are taught by lab assistants in that class. They define the node in the tree as a struct like this : struct Data{ char name[15]; int age; Data *left,*right; }; and they give us a code to search within the BST like this: // temp is current node, name is the value of the node to be searched for. Data* search(Data *temp,char name[]) { if(strcmp(temp->name,name)>0) search(temp->left,name); else if(strcmp(temp->name,name)<0) search(temp->right,name); else return temp; } I notice that code is

Haskell IO: convert IO String to “Other type”

泄露秘密 提交于 2019-12-01 23:09:43
问题 I have a Haskell program which takes a file as an input and convert it into a binary search tree. import System.IO data Tree a = EmptyBST | Node a (Tree a) (Tree a) deriving (Show, Read, Eq) ins :: Ord a => a -> (Tree a) -> (Tree a) ins a EmptyBST = Node a EmptyBST EmptyBST ins a (Node p left right) | a < p = Node p (ins a left) right | a > p = Node p left (ins a right) | otherwise = Node p left right lstToTree :: Ord a => [a] -> (Tree a) lstToTree = foldr ins EmptyBST fileRead = do file <-

Cannot borrow node as mutable more than once while implementing a binary search tree

旧时模样 提交于 2019-12-01 21:12:11
问题 I'm trying to implement a binary search tree in Rust and I am running into problems with inserting an element. What is an idiomatic way of doing this in Rust? Here is my implementation: use std::cmp::Ordering; pub struct BinarySearchTree { root: OptNode, size: u32, } type OptNode = Option<Box<Node>>; struct Node { key: i32, left: OptNode, right: OptNode, } impl BinarySearchTree { pub fn new() -> Self { BinarySearchTree { root: None, size: 0, } } pub fn is_empty(&self) -> bool { self.size == 0

Count number of smaller values while inserting into binary search tree (BST)

无人久伴 提交于 2019-12-01 20:56:16
I'm currently implementing an algorithm in which I need to know how many numbers, from the ones that have already been read, are smaller than the one that's currently being processed. A way to do that is through merge sort, but I'm more interested in a BST approach. This procedure is meant to compute in O(log n) the number of nodes with value less than the key node's value : countLessThan(int x, node T) if T = null return if T.value >= x countLessThan(x, T.left) // T.left contains only numbers < T.value and T.right only numbers > T.value else globalResult += T.numLeft countLessThan(x, T.right)

Deletion in binary search tree

混江龙づ霸主 提交于 2019-12-01 14:47:32
So when I delete in binary search tree, do I need to have like 7 different cases i.e. Left Leaf; Right Leaf; Left child with only left child. //i.e the node to be deleted is the left child of it's parent and it has only left child. Left Child with only right child. Right child with only left child. Right child with only right child. Node to be deleted has both the children i.e. right and left. Now when this code is using if-else it gets pretty nasty.. is there any other way of doing this. Here is my code snippet if(current->left==NULL && current->right==NULL && current->key<prev->key) //left