I have the following Binary tree 3 / \ 5 2 / \ / 1 4 6 My weekness is recursion, so please bear with me, and I need your help to trace through it to get it right. I have the following code, and what it does is print...

I want to clear a BST so as to take advantage of the garbage collector. So, to clear a BST, is it sufficient to set the root to null, so that I end up with a bunch of abandoned nodes with no pointers to them? Or is it better to...

I have these instance methods in my Java implementation of Binary Tree and Search Binary Tree: getSize(), getHeight(), getDepth(), getPreOrder(), getInOrder(), getPostOrder() and getLevelOrder(). These methods use the root of the tree in others recursive methods that have a parameter Node. Which is more appropiate to use from the point...

Lets say I have the following datatype: data BinaryTree a = EmptyTree | Node a (BinaryTree a) (BinaryTree a) deriving (Eq, Ord, Show) and the following function member a EmptyTree = False member a (Node x l r) | a < x = member a l | a > x...

I am trying to compare two strings to see where it should go as a leaf node from the root and so fourth. I keep trying to use string compare and I get an error. I am very unfamiliar with binary trees in c and need help inserting nodes. Here...

This is homework. Please don't give me just code. The assignment was to implement several methods that a BST uses; among those, was add(T data) and remove(T data). I was able to successfully implement them. Here were the guidelines for the two methods: public void add(T data) If the data...

But I ran into a challenge when I took an exam some days ago. If we have balanced binary tree with n vertices and store the number of children in the sub-tree for each node (i.e number of children that this node as a root has). How we can do...

I am trying to figure out what the time complexity of my algorithm is, I have algorithm with binary search, which is in general O(log n), I know. But I search between two constants, namely x=1 and x = 2^31 - 1 (size of integer). I think that in the...

i have this question : implement function int countSingle(), which uses Breadth-First Traversal (BFT) in order to count how many nodes in the tree have a single child. so the code below is what i thought of to solve it , is there another way of doing this or a...

This a question from a coding competition The original question can be found here http://www.olympiad.org.za/olympiad/wp-content/uploads/2014/03/2013-PO-Question-Paper.pdf Question 5 SHORTEST PATH THROUGH THE HALL [by Alan Smithee of Hulsbos High] The hall is packed wall to wall with rows of chairs, but in each row there are exactly two chairs missing. The...

Assume I had some binary search tree that worked and declared two like this: BinaryTree<string, queue<int> > * tree1 = new BinaryTree<string, queue<int>>; BinaryTree<string, int> * tree2 = new BinaryTree<string, int >; And lets say for the second one this worked perfectly: tree2->Add("Yoyo", 10); How would you add values to...

So I'm currently stuck in the intermediate level of racket, and have looked at various codes, but most of them I can't figure out I'm trying to delete a node from a binary search tree So, I've so far come up with this, which includes an empty node and the...

I realize a code for an inorder traversal would look something like if left[x] != NULL recurse left process right[x] !=NULL recurse right I coded everything up, works fine. I then started to think more and have over-thought the process and now am lost in how the recursion actually works....

I'm trying to implement a binary search tree in C, more specifically looking for the predecessor. However, whenever I try to run the program I get the segmentation vault. Here's the code in question: #include <stdio.h> #include <stdlib.h> struct tree { int a; tree *left; tree *right; tree *prev; }*root=NULL;...

I am referring to THIS problem and solution. Firstly, I did not get why sum of frequencies is added in the recursive equation. Can someone please help understand that with an example may be. In Author's word. We add sum of frequencies from i to j (see first term in...

This is homework; please don't just give me code I have two methods: remove(T data) and removeRec(Node<T> node, T data). In its current state, it seems my code only removes the root node of the BST. @Override public T remove(T data) { if (data == null) { throw new IllegalArgumentException("Data...

I know what Binary search tree is and I know how they work. But what does it take for it to become a skewed tree? What I mean is, do all nodes have to go on one side? or is there any other combination? Having a tree in this shape...

My code is not printing the elements of binary search tree: //x is the element to be inserted //structure of program typedef struct BST { int info; struct BST *left; //pointer to left node struct BST *right; //pointer to right node } bst; //global root variable bst *root; void insert(int...

Although the purpose of this question will probably not make a difference, I will state it anyway. I am working on making a method that performs Dijkstra's Algorithm on a graph. The algorithm requires a set U of unvisited cities. When a city is visited, it must be removed from...

I have a scheme function where I have a list and I am trying to put the numbers into a Binary Search Tree one by one. However, I keep getting "unspecified return value" (define (insertB L) (if (not (null? L)) (begin (let() BST (insert (car L) BST)) (insertB (cdr L))...

PROCEDURE CreateNode (info: CHAR): BST; VAR bst : BST; BEGIN NEW(bst); bst^.elem := info; bst^.left := NIL; bst^.right := NIL; RETURN bst; END CreateNode; PROCEDURE Insert (info: CHAR; VAR t: BST); BEGIN IF t = NIL THEN t := CreateNode(info); ELSIF Compare(info, t^.elem) = greater THEN Insert(info, t^.right); ELSIF Compare(info,...

I want to create an External Memory Binary Search Tree Data Structure whose data sits in the external memory using stxxl as a library. For this purpose, which Data Type in STXXL is suitable to use as nodes in the Tree. If we use stxxl:Vector as the Nodes of the...

So Basically I am trying to "populate" a file with 10^3 completely random numbers, so I could add them later to a Binary search tree. Here is the populate function I worked on so far: void populateFile(BinarySearchTree b) { int random_integer; srand( time( NULL ) ); std::ofstream myfile; string line;...

Is there a formula to calculate what the maximum and minimum height for an AVL tree, if given a certain number of nodes? (this is for a intro java prog. class). For example, there is a question : Textbook question: What is the maximum/minimum height for an AVL tree of...

How is it possible to write generic c? I started writing a collection of balanced trees (scapegoat, splay, aa and so on) and find many things in common. Example is destroy function found below. Can such function and those similar be defined with void pointers without causing "dereferencing void* pointer...

Write the implementation of the function T ComputeMedian() const that computes the median value in the tree in O(n) time. Assume that the tree is a BST but is not necessarily balanced. Recall that the median of n numbers is defined as follows: If n is odd, the median is...

I'm trying to teach myself Python, coming from C++, so I decided to try and build a simple BST. I managed to get my insert method working correctly, but I do not know why my printTree method fails. The interpreter gives me the error: Traceback (most recent call last): File...

I'm new to Java, I want to create a Binary Search Tree class with insertion and preorder traversal, but when I finish the insertion the root object remains null and the compiler throws NullPointerException during the preorder traversal. My node class: class Node { int info; Node left; Node right;...

I am creating a binary search tree that needs to be able to delete a node and return true if the node was properly deleted, or false if the value doesn't exist in the tree. I am doing this because I need to delete more than one number and I...

Hi i am trying to learn haskell and compare its performance to other languages when i run the following code.. module BST ( Tree, singletonTree, insert, member ) where import System.IO import System.IO.Error hiding (try) import Control.Exception import Data.Char import System.CPUTime -- -- Take the string and convert it to...

i've trying to implement an AVL tree just for the sake of learning. I managed to make the insertion, but im stuck at the deletion. I have a problem deleting a leaf node (the one-child case works just fine). the problem is that when i delete the node it doesn't...

Write the implementation of the function T findMedian() const that computes the median value in the tree in O(n) time. Assume that the tree is a BST but is not necessarily balanced. Recall that the median of n numbers is defined as follows: If n is odd, the median is...

In a BST pre-order traversal,I do not see the result I would have expected. Please help confirm whether it is an issue with the code or my understanding of how pre-order traversal would work. Node: class node implements Comparable<node>{ int v; node left; node right; node(int v){ this.v=v;} boolean equals(node...

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...

I'm fairly new to c++ and I've recently received a project to create my own Binary Search Tree using a Template. The goal is for the Binary Tree to be able to take in any kind of data type. IntBinaryTree.h should be able to take in object of EmployeeInfo. I've...

I'm coding up a search_key algorithm for a binary search tree and am having issues. I'm traversing the binary search tree and comparing the nodes using an in-order traversal algorithm using the basic recursion technique. However, when the conditional hits (search_key == node[x]->value) and I return the node, it seems...

I'm making a BST (Binary Search Tree). I would like some help with my delete method, it seem that when I set that node to null it stills appear when I display it with my display methods (preorder,inorder,postorder). Here is my node linked list class public class Node<T> { public...

Okay so i am trying to pass an array of chars through a function, then assign that string of chars to the member of a structure for a binary search tree. my files are to be split up and i cant seem to figure out what i am doing wrong....

Consider this example of binary search tree. n =10 ;and if base = 2 then log n = log2(10) = 3.321928. I am assuming it means 3.321 steps(accesses) will be required at max to search for an element. Also I assume that BST is balanced binary tree. Now to access...

My task is to calculate depth of each node and store it in 'depth' given in Node class. But I don't know how I should approach this task. I was looking for some example in the internet but haven't found any appropriate to my task. This is the code of...

The program creates a binary search tree from a sorted array of numbers and adds elements to it. struct Tree { Tree *left_son, *right_son; int head, key; }; Tree *tree(int *a, int left, int right) { Tree *tree1 = new Tree; int mid = middle(left,right); tree1->head = a[mid]; if (left...

I need some help in C Help me to extend the binary tree sort on C. I need to return a sorted array in sort function. here it is: #include <stdio.h> #include <stdlib.h> struct btreenode { struct btreenode *leftchild ; int data ; struct btreenode *rightchild ; } ; void...

In Java to compare two objections for equality, you must implement both the equals method and the hashCode method. I need to compare two BSTs for equality. How do I implement the hashCode method in such a case? Now, implementing the hashCode on the Node class is simple enough: imagine...

I have a dictionary file that contains a list of words, and I read that file into the array list sArray. I then have a book where I use a string parser to take every string from this book and send it to the binary search method, bSearch. bSearch will...

In BST, which traversal is required to visit all keys in decreasing order, assuming as a comparator? The answer is Reverse In- And I was wondering why this is the case. And what if it is to visit all keys in increasing order, what would be the answer from the...

Here is an excerpt of a working BST: class BinaryTree(): def __init__(self,rootid): self.left = None self.right = None self.rootid = rootid def getLeftChild(self): return self.left def getRightChild(self): return self.right def setNodeValue(self,value): self.rootid = value def getNodeValue(self): return self.rootid I decided not to display every function of the class above, only...

THIS IS HOMEWORK, DO NOT POST CODE. Please and thank you. I have been assigned to create a method that calculates the depth of a particular in a BST. To do this, I am to @Override a method public int depth(T data). So, to find it recursively, I need to...

Like the question says Im trying to make an array list which has all the data in each of the nodes in a binary search tree. public List storeKeyValues(){ List keyvalues = new ArrayList(); boolean notdone = false; temp = root; if(temp == null){ return null; } else{ list.add(temp.data); while(!notdone){...

I am trying to build a simple tree program and traverse it in inorder,preorder and postorder formats. The code I am using is this : #include <stdio.h> #include <stdlib.h> struct node { int info; struct node* lchild; struct node* rchild; }; typedef struct node* NODE; NODE getNode() { NODE temp;...

I have an insert function for a Binary Search Tree in Haskel. My insert functions first two lines look like this insert :: (Ord a) => BST a -> a -> BST a insert Nil x = Node x Nil Nil I know the function works, I have tested it...

so I'm stuck on a question in my computer science class, it asks for me to extend our previously written binary search tree class, and add a way to delete nodes from the tree, this will have to take into consideration if that node has 0, 1, or 2 children....

Okay so I am writing code for a binary search tree that must read in strings from an input file: "input.txt" and I am trying to feed my program command line arguments. My program is supposed to take two command line args, the name of the program then the name...

I'm struggling with following problem: Write function which for given binary tree returns a root of minimal height which is not BST or NIL when tree is BST. I know how to check if tree is BST but don't know how to rewrite it. I would be grateful for an...

So I want to find the parent node of a Node in a binary tree. Suppose that I input 30,15,17,45,69,80,7 in the tree through a text file. The tree should be 30 15 45 7 17 69 80 And here is my code : Node* BST::searchforparentnode(Node* pRoot, int value) {...

I have an arraylist called sArray that holds a large list of correctly spelled words. I need to send a word to this recursive binary search method (key) and determine whether or not it is spelled correctly or not. I understand how a recursive binary search works however I am...

I have a tree with a set of numbers, where each number has 2 strings associated :a and b. So the structure looks like: a-number-b for each node. I want to get the maximum number in the tree where a=b in O(log n) worst case run time. My approach: Tried...

I was working on a problem regarding to binary search tree using java. The name of the built-in class in TreeNode. In the solution, it shows However, when I did the implementation myself, I am not able to do "root.left" or "root.right". I was wondering if I missed anything? Thanks!...

this code is a binary search tree I'm doing for my school assignment. #include "binary_tree.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXLEN 10 Node* create_tree() { return NULL; } void insert_tree(Node** root, char* nname, char* number) { if (*root==NULL){ Node* node = (Node *)malloc(sizeof(Node)); strncpy(node->name , nname, MAXLEN); strncpy(node->number...

Studying for an algorithms exam, and I read that the height of every BST is not O(log n). Does this fact have something to do with the tree being balanced? Is the height of every balanced BST O (log n), and unbalanced tree something else (if so what is it)?...

This is the implementation of add in Binary Search Tree from BST Add private IntTreeNode add(IntTreeNode root, int value) { if (root == null) { root = new IntTreeNode(value); } else if (value <= root.data) { root.left = add(root.left, value); } else { root.right = add(root.right, value); } return root;...

For example, given the tree: 10 5 15 0 6 12 20 // full -5 2 8 14 16 22 4 18 24 26 The value returned by the function highestFull(BinaryNodeX<Comparable> *t) would be 3 as the height of the deepest full level is three....

It is kind of like a hybrid tree/linked list structure. Here's how I defined the structure struct node { nodeP sibling; nodeP child; nodeP parent; char name[100]; }; a node has a child, that is connected to a linked list. Other elements on the linked list may have their...

I am totally stuck on this problem. I need to output the position of a value in an inorder list (First index 0). The caveat is that I can't create the list and search through it. For each node I have a variable that contains information about how many nodes...

I ran into a perplex situation while I was trying to understand the binary search tree. I am baffled by the way the method recursion is happening here when a method is called, say inOrder(). Below is the code: public class Node { int data; Node left; Node right; public...

According to Ron Wein your able to do split and concatenation of red-black tree's in O(log(n)) time. See his artikle: Efficient Implementation of Red-Black Trees with Split and Catenate Operations However I'm still not convinced that the running time of split really is true. The idea is that split uses...

I want to get the maximum width of a Binary tree. In this method using Pre-order traversal width can be calculated. But I can't understand it properly. Please explain how the function getMaxWidthRecur() works. // A utility function that returns maximum value in arr[] of size n int getMax(int arr[],...

I'm supposed to search for a given album from a binary tree and return it. Here's what I have so far: public AlbumNode getAlbum(AlbumNode root, String name) { if (root != null) { if(root.left != null) getAlbum(root.left, name); if(root.right != null) getAlbum(root.right, name); } if(root.getName().equals(name)) return root; return null; }...

Once I was interviewed by "One well known company" and the interviewer asked me to find the median of BST. int median(treeNode* root) { } I started to implement the first brute-force solution that I came up with. I fill all the data into a std::vector<int> with inorder traversal (to...

I wrote a phonebook search using binary trees. Everything in the code works fine, except for when I'm prompted to type in whose number I'm searching. Then the search_phonebook function return NULL by default. I can't crack what it is. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MAX_NUM...

This is from Heap lecture slide 8. Add, peek, and remove make sense to me as to why those operations are O(log n)- traversing through BST cuts tree in half after each move. Can anyone explain the intuition behind the last sentence though, that "The tree tends to become unbalanced...

I have a slight confusion on inorder successor/predecessor if the BST is flipped. What I meant when the BST is flipped/reversed is that when all elements in the right subtree is smaller and all elements in the left subtree is greater. Normally the right subtree has greater value. What if...

I am trying to write a method to delete a Node from a Binary Search Tree. Here is my method to delete a node. public void delete(int deletionNodeValue) { Node<Integer> nodeToBeDeleted = getNode(deletionNodeValue); if(nodeToBeDeleted == null) return; // No node with such value exists throw an error if(isLeafNode(nodeToBeDeleted)) { nodeToBeDeleted...

In other words, If you were to read the values of the leaves in a red black tree from left-to-right immediately after an insertion, would that order remain the same after performing balancing operations on the tree?

I've search around SOF for a while and read similar post. I didn't find any of those answers satisfactory. The issue i'm having is that I want to traverse a tree until 'the root node' and a target node are the same. If the 'root' == target, I want to...

I have a report which shows 2-4 million records. I get the records from oracle to java and push it to an excel report. All this is already done! Now, I also need to add a new tab with top 10 and last 10 records. What would be the best...

I want to insert, delete and print strings in this BST using a public method, as opposed to a boolean or void - so I have to return. In my insert method I am trying to check for null, and left and right sides of the tree. In the case...

Given a BST, and a node in BST, make that node, as new root of the tree. But still maintain the tree as BST after making this node as root. I tried as follows: Take given node as root, if its on left of original root then make original root...

I am trying to find the largest value in a binary search tree. The sample solution is given below, but I'm trying to understand if there's anything wrong with my solution? My problem with the sample solution is that it seems to check whether each node is not None twice:...

What are the uses of heaps? Whatever a heap can do can also be done by a self-balancing binary search tree like an AVL tree. The most common use of heap is to find the minimum (or maximum) element in O(1) time (which is always the root). This functionality can...

I have 2 binary search trees T1 and T2 with same number of nodes n >= 1. For each node P we have LEFT(P) and RIGHT(P) for links between nodes and KEY(P) for value off the node. The root of T1 is R1 and root of T2 is R2. I...

The function to delete a node in a BST with the given key is as follows: struct node* deleteNode(struct node* root, int key) { // base case if (root == NULL) return root; // If the key to be deleted is smaller than the root's key, // then it lies...

Beyond the root, I couldn't add any more nodes permanently. Once a tree object is created in the insert function, on the next entry into the insert function it is being nullified, so only root is present. I can't find the problem and it's driving me crazy. Someone please help,...

private Node put(Node x, Float longitude, Float latitude, String place, String address) { if (x == null) return new Node(longitude, latitude, place, address, 1); int cmpX = longitude.compareTo(x.longitude); int cmpY = latitude.compareTo(x.latitude); if (cmpX < 0 | (cmpX == 0 && cmpY < 0)) { if (x.left == null) {...

The object of this function is to get user input to find a word that has been added to the Binary Search Tree. Then using my search algorithm in BST, if the word is found, then print out the frequency of the word. My approach was to take user input...

I have a member function in Haskell for a BST. I am trying to use foldr to check if every element in the list is in the BST. My member functions works for single elements and the first line looks like this. member ::(Ord a) => BST a -> a...

I know how to insert integers into binary search tree Smaller to the left Larger to the right But that formula would be applicable only if Data/Key is integer. So i want to know how do i insert strings into BST I have searched the google but couldn't find any...

Update: how best can I add up the durations without modifying the interface? I cannot access a method of an instance of an anonymous inner class. I get a compile-time error for the sum.totalTime() call -- "the method totalTime() is undefined for the type Traverser" I have a Traverser interface....