binary-search-tree

C++ Binary Search Tree status access violation error with adding nodes

天涯浪子 提交于 2019-12-11 18:17:57
问题 I'm working on a program that works with a binary tree. I'm getting an error with adding new nodes into the tree. I can add one node, but after adding another, I get a STATUS_ACCESS_VIOLATION error. I think the error could be with the function arguments dealing with the search function. Please help me if you can. Here is he .h file that i've written: #ifndef P4_H #define P4_H #include <iostream> #include <iomanip> #include <fstream> #include <cctype> #include <string> using namespace std;

How to validate a Binary Search Tree?

前提是你 提交于 2019-12-11 17:59:34
问题 Here is the code that I have written to validate a BST. Is it correct? If not, how would I do this? int validate(node *root) { if(root==NULL) return 1; else if(root->lchild!=NULL && (root->lchild)->data >=root->data) return 0; else if(root->rchild!=NULL && (root->rchild)->data <=root->data) return 0; validate(root->lchild); validate(root->rchild); return 1; } 回答1: Let's say you have the following tree: 20 / \ 10 30 / 15 and start at the root with your code: 1 int validate(node *root) { 2 if

Unique random numbers in C#

假装没事ソ 提交于 2019-12-11 17:57:46
问题 I'm trying to create a binary search tree with unique random numbers. I'm using SortedSet to represent my tree and then I make it into an array and then I use Contains to see if a certain number is in the tree. My problem is that I can't figure out how to get all the random numbers different in a simple way. I used the methods Unik and Nålen_Unik but in this code it only generates 1 number to the array Random random = new Random(); Program Tal = new Program(); string nål = Tal.Nålen_Unik();

Im stuck with filling my Array Based BST in C++

喜你入骨 提交于 2019-12-11 17:27:46
问题 Im trying to build an array based, "Binary Search Tree" by following the algorithm at: http://highered.mcgraw-hill.com/olcweb/cgi/pluginpop.cgi?it=gif::600::388::/sites/dl/free/0070131511/25327/tree_insert.gif::TREE-INSERT. ...using the algorithm I came up with the following code: void BST::insert( const data &aData ) { item *y = &items[root_index]; // Algorithm calls for NULL assignment.. item *x = &items[root_index]; while ( ! items[root_index].empty ) { y->theData = x->theData; // Ptrs are

Getting 0 instead of null for binary search tree

限于喜欢 提交于 2019-12-11 17:27:14
问题 I have been trying to clear my binary tree and when doing so I get a 0 instead of null from the code below. The tree is defined to be empty when it references NULL but that's not happening. The error can be seen in the traversing over the tree. How can I change it so that when the second set of numbers are inserted, the 0 doesn't show? #include <stdio.h> #include <stdlib.h> typedef struct Node{ int value; struct Node * left; struct Node * right; } Node; Node * insert(Node * node, int value){

Parasoft error: objects to manage resources should be used instead “x” pointer

限于喜欢 提交于 2019-12-11 16:12:52
问题 I've created a binary search tree, each node of my binary tree is setup in a struct containing the key, and a pointer to the left and right nodes. In my copy constructor for this binary search tree, I call a helper method to recurs through the tree that looks like so: Node* BinaryTree::copyHelper(const Node* other) { if(other == NULL) { return NULL; // If there's no Node to copy, return NULL. } Node* newNode = new Node; if(newNode) { newNode->name = other->name; newNode->left = copyHelper

Inserting in a Binary Search Tree

 ̄綄美尐妖づ 提交于 2019-12-11 15:19:08
问题 I've made a binary search tree struct BTNode { int info; struct BTNode *left,*right; }; I've wrote a code to insert a node in the tree void insert(struct BTNode *root,int data) { struct BTNode *ptr; struct BTNode *n=malloc(sizeof(struct BTNode)); n->info=data; n->left=NULL; n->right=NULL; if(root==NULL) root=n; else{ ptr=root; while(ptr!=NULL){ if(data<ptr->info){ if(ptr->left==NULL) ptr->left=n; else ptr=ptr->left; } else if(data>ptr->info){ if(ptr->right==NULL) ptr->right=n; else ptr=ptr-

What does this pseudo code mean?- Binary Search Tree Successor Function

耗尽温柔 提交于 2019-12-11 14:49:34
问题 if right[x] != NIL then return TREE-MINIMUM(right[x]) y<-p[x] while y!= NIL and x = right[y] do x<-y y<-p[y] return y I know what "if right[x] != NIL then return tree-min" means and I've translated it to: if(p->RChild) return fMinValue(p->RChild);//returns the min value of the sub-tree starting at the right child node of p The rest I'm having trouble understanding. 回答1: <- is most likely the assignment operator. p I would guess is parent. What else are you confused about? 回答2: Here p[] almost

Binary Search Tree having issues with insert function

爱⌒轻易说出口 提交于 2019-12-11 14:43:05
问题 I'm using recursive functions to insert a node into my binary search tree. The program works by creating a root node if there is none. Root is a pointer to a node struct. If the root already exists I call the worker function. Note: Key is int and Item is a string. When calling the worker function, current->key(-858993460) and current->item(Error reading characters of string) are not their expected values (1, "Harrold") . Recursion continues until this Exception occurs: "Exception thrown: read

Java Binary Search Tree Delete node with no children

余生颓废 提交于 2019-12-11 10:31:24
问题 I'm working on the case where the node to be deleted is a node. I'm not sure if I need to keep track of the parent so that when I find the node to delete so I can set its parents pointer to null. But then how would I know which child the node to be deleted is? Do I need more if statements? Any help is appreciated, I feel its not too complicated but I'm just confused on how to actually get rid of the node. This is what I have so far: public void insert(E s) { root = insert(s, root); } private