binary-search-tree

Deleting the lowest value in a binary search tree

牧云@^-^@ 提交于 2020-01-25 17:26:54
问题 I wrote this and somehow it's ending up deleting all my left side on the tree not only the most left leaf. I can't find my mistake, can someone help? struct Node *delMin(struct Node **root) { struct Node *current = *root; struct Node *b4Current; while ((current->m_ls) == NULL) { b4Current = current; current = current->m_ls; } if ((current->m_rs) == NULL) { b4Current->m_ls = 0; free(current); } else { b4Current->m_ls = current->m_rs; free(current); } return *root; } 回答1: Let's look at your

Deleting the lowest value in a binary search tree

寵の児 提交于 2020-01-25 17:22:21
问题 I wrote this and somehow it's ending up deleting all my left side on the tree not only the most left leaf. I can't find my mistake, can someone help? struct Node *delMin(struct Node **root) { struct Node *current = *root; struct Node *b4Current; while ((current->m_ls) == NULL) { b4Current = current; current = current->m_ls; } if ((current->m_rs) == NULL) { b4Current->m_ls = 0; free(current); } else { b4Current->m_ls = current->m_rs; free(current); } return *root; } 回答1: Let's look at your

In order BST traversal: find

99封情书 提交于 2020-01-25 00:09:05
问题 I am trying to find the kth smallest element of binary search tree and I have problems using recursion. I understand how to print the tree inorder/postorder etc. but I fail to return the rank of the element. Can someone point where I am making a mistake? In general, I am having hard time understanding recursion in trees. Edit: this is an exercise, so I am not looking for using built-in functions. I have another solution where I keep track of number of left and right children as I insert nodes

Binary Tree root is null

拈花ヽ惹草 提交于 2020-01-24 19:01:05
问题 I'm trying to create a binary search tree but it doesn't seem to be working. I debugged it, and it says the root is null. I don't understand why it is null. I set it to null initially in the constructor, but then when I call the insert() method, it is no longer null, right? Can someone help me understand this. Thanks. #include "stdafx.h" #include <iostream> using namespace std; struct node { public: int value; node * left; node * right; }; class bTree { public: node * root; public: bTree();

What changes when implementing lazy deletion into a binary search tree exactly?

半世苍凉 提交于 2020-01-17 05:27:37
问题 Okay so I've been looking into this for days and everytime I think I've gotten it down I start writing code and I get to a point where I just can't figure out what exactly to do. The tree isn't recursive, so I can follow everything really until I start trying to modify it so it uses lazy deletion instead of real deletion. (Right now it nulls out the node it deletes) What I have managed to figure out: I added a flag to the node class to set them as deleted I've implemented a search method that

Add nodes to binary search tree c++

前提是你 提交于 2020-01-17 00:37:09
问题 I am building a binary search tree. Now I am having a problem with adding a node to the tree. void BinaryTree::add(int value, Node* node) { if(!node) node = new Node(value); else if(node->key < value) this->add(value, node->rightNode); else if(node->key > value) this->add(value, node->leftNode); } This code does not seem to work when I call: BinaryTree test; test.add(4, test.root); test.add(1, test.root); test.add(5, test.root); test.add(2, test.root); test.add(3, test.root); test.add(7, test

How do I delete from a binary search tree in Lisp

南笙酒味 提交于 2020-01-15 18:32:28
问题 How can I delete a node from a BST? I need an algorithm to do that in Dr. Scheme. 回答1: You basically toss the BST you have now, and create a new one sans the element. You can do this by recursively descending the tree. If your item is less than the root datum, create a BST whose root and greater-than branch is copied from what you have now, but whose less-than branch is the result from a recursive call. It's very similar to how you add a node, but when you get to the one you were searching

Why is my BST root pointer changing for some unknown reason?

别来无恙 提交于 2020-01-15 08:23:09
问题 I am trying to implement a Binary Search Tree data structure in C, and I have run into a bug. My pointer value changes for a reason I do not understand. (Please see bottom of post for strange output [Delete function and main functions clarify where output comes from] ) My Test function below: int main(void) { Bst *bst = ( Bst* ) calloc( 1, sizeof( Bst ) ); BstInsert( bst, 7 ); BstInsert( bst, 8 ); BstInsert( bst, 2 ); BstInsert( bst, 1 ); BstTraverse( bst ); BstRemove( bst, 7); printf("======

What is the paradigmatic way to create a Rust tree with a parent pointer? [duplicate]

不羁岁月 提交于 2020-01-15 06:31:06
问题 This question already has answers here : How do I express mutually recursive data structures in safe Rust? (3 answers) How to model complex recursive data structures (graphs)? (1 answer) Closed 2 years ago . I need to define a binary search tree where each node has access to the parent: enum Tree<'a> { Leaf, Node { left: Box<Tree<'a>>, right: Box<Tree<'a>>, parent: &'a Tree<'a>, data: u64, } } impl <'a> Tree<'a> { pub fn new(data: u64, parent: &'a Tree) -> Tree<'a> { Tree::Node { left: Box:

C Program to copy one binary search tree to another

丶灬走出姿态 提交于 2020-01-15 06:02:29
问题 So, here i have come up with Binary search tree prgram, where i am creating 2 binary trees tmp and tmp2, where i am trying to copy whole tmp2 to tmp, the node which is taken as input from user. But i am getting some segmentation fault, also i am not so sure if the logic is right. Here is the whole program, please lemme know where is it going wrong in t_cpy() or please just fix it for me.. #include<stdio.h> #include<stdlib.h> struct node { int data; struct node *rlink; struct node *llink; }