I had an assignment that I completed and it was just inserting values into a binary tree and then displaying it. I was wondering what the code would be if I wanted to delete a number in the binary tree, or if I wanted to search for a number. Here is my code for my assignment.
So I have this code that I wrote that pretty much makes a binary search tree with one node per element that holds the data int, one pointer for left, and one pointer for right. My tree is set that if you give it a number it starts as root, then afterwards any number you give it will either be placed left or right if it smaller than the current node or bigger respectively until it hits a pointer that points to NULL.
I was able to get my code to display the numbers in order no matter how bad you inserted the numbers to throw me off. My question now is this, how can I make it count how many levels there are? I'm not sure if this is clear or unclear but I want it to take all the paths and return to me the longest path and that should be how many levels there are.
#include <iostream> using namespace std; class binarySearchTree { private: class node {
I am having an issue when i try to delete a node with 2 children it either doesn't delete anything, or wigs out in various manners deleting the wrong node or replacing a node with a various memory location. As follows, here is the delete function:
void BST::dele(){ bool found = false;//initialize a bool type to "find" the element to be deleted if(root == NULL) return;//well if the tree's empty, nothing to be found right? current = root;//set the current to the root to traverse the tree in search of the element node* parent;//create a parent node for use once the node has been deleted while(current != NULL){//traverse the tree
This is the delete function of binary search tree. However it won't enter the if-else statement that checks whether the node to be deleted is the left child or right child.
void DeleteNode(node* T, int number) { node* x = new node; node* current = new node; node* dele = new node; node* finder = new node; finder = root;
Below is my implementation of a Binary Search Tree, but there is a bug in the DeleteNode() function. It does not set the parent's pointer to NULL as I intended.
I am working on a program that needs to take any infix expression as an input. And the display the expression tree of it on the console as an output. For example the input goes (a+-(c*d) should output:
I'm having some trouble with my binary tree for school. It is a data structures class so we are working on learning how to make our own binary trees and encrypt messages. Everything so far is working, except for my delete node function. I'm trying to do it recursively. Parts of my code.
/******** Node *********/ struct node { char data; node* right; node* left; }; /******** Binary Tree Class *********/ class BinaryTree
My colleague and I have been given a task to create a list with the employes of a company using binary trees. Each employee/node has to contain the following information:
- Employe name - Rank - ID Number - monthly Salary
We have to write a C program (using Codeblocks) which builds the company tree with the information above and afterwards lists all the employes who have a salary bigger then a number specified by the user.
So far so good. We've had a look on the examples on the internet and after finding one which looked friendly enough we started working on that one to suit our needs. Credits for the original source code to: C Program to Implement Binary Search Tree Traversal - Basic C Programs | C Programming Examples
We've reached the point where we're able to insert the required information but being able to list all the employes who's salary is bigger then the number given by the user seems to be rather difficult to achieve. I'll include the code next. We have left almost the entire pieces of the original code and we are aware that the are still things which are not required for our assignment, which only requires to enter the 4 information above and afterwards display the employes with the before mentioned condition concerning their salary.
Code: # include <stdio.h> # include <conio.h> # include <stdlib.h> typedef struct BST
Im working on a BST remove function. I think I'm on the right track but I'm not sure. From what I understand there are 3 possible cases. A Node with no children, one child, or 2 children(this being the most complex).
I am unable to implement the insert function properly, every time i run the program i just get the first value and name, I am not getting other Id's and name.
"(Header File)" #include <iostream> #include <string> using namespace std; class node{ public: int ID; string name; class node *left, *right, *parent;
I am unable to implement the insert function properly,every time i run the program i just get the first value and name,i am not getting other Id's and name.
Code: "(Header File)" #include <iostream> #include <string> using namespace std; class node { public: int ID; node (string StudentName, int IDNumber) {
I was instructed to write a binary search function which would return true if an element, inputted by the user, was found in the array, and false if it was not. I'm not sure why, but my function always returns false. My code is as follows.
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; //binary search function bool search (int array[], int item)
inputting a search array. I tried putting a binary search but I can't get it to work. everything else works up until I put the value I am searching for in the array, then it just crashes.
How it suppose to work: input 2 coordinates with a value each then it calculates the distance between them then it suppose to let user search the coordinates for a value and state if found which coordinate it is at.
Just doing some work where i need to produce a database in C++ for DVD now I have done most of it but I'm stuck on some bits. I have split the database up into different files but I will post the files which are important. How to do a search function. I got told it's called "Bubble search" and then a delete function which i think is called "Vector delete".
My header file
Code: #ifndef DVD_DB_H #define DVD_DB_H #include "dvd.h" #include <vector> class dvdDB { private: std::vector<DVD> dvds; // A container that contains an arry of DVDs
I have to manage a Clinic. I need to delete a booking (for example, if John said he's coming on March 22nd at 15:30 but then he say he's not going to come, I have to delete that booking so another person can use it).
idSearched: the id of the person that is not going to come. I have a lot of specialties and each one has a list. So I ask for the speciality to delete the node (the node contains John's booking). If I don't find it, I return (-1). searchSpecByID return a pointer to the list where the speciality is. So head will point to the first node of the list. In nodeToDelete I have the node I want to delete.
The program detects OK when is the first in the list and when not, but it doesn't delete the node.
It has been a while since I built a binary tree from scratch so I decided to do it. Everything works fine but this one function. When I enter a number to search it just keeps running and allowing me to keep enter numbers.
Code: void tree::search(int key,Node* leaf) { if (leaf == NULL) { std::cout<<"The tree is empty
I'm trying to use the biSearch function to search for a keyword in the dictionary.
Code: int biSearch(Dict DictEntries[MAXENTRIES],int start, int finish,char *keyword) { int mid = (start+finish)/2; int index = strcmp(DictEntries[mid].key,keyword); printf("%s=%s ",DictEntries[mid].key,keyword);