Given a binary tree, I want to find out the largest subtree which is a BST in it.
Naive approach:
I have a naive approach in mind where I visit every node of the tree and pass this node to a isBST function. I will also keep track of the number of nodes in a sub-tree if it is a BST.
Is there a better approach than this ?
...
I'm reading the Cormen algorithms book (binary search tree chapter) and it says that there are two ways to traverse the tree without recursion:
using stack and
a more complicated but elegant
solution that uses no stack but
assumes that two pointers can be
tested for equality
I've implemented the first option (using stack), ...
Using an algorithm Tree-Insert(T, v) that inserts a new value v into a binary search tree T, the following algorithm grows a binary search tree by repeatedly inserting each value in a given section of an array into the tree:
Tree-Grow(A, first, last, T)
1 for i ← first to last
2 do Tree-Insert(T, A[i])
If the tree is i...
I have to create a binary tree using the struct as follows:
struct treenode;
typedef struct treenode* TreeNode;
struct treenode {
void* data;
TreeNode left, right;
};
using void* as the type for the data to be stored at each leaf, so that an object of any type can be inserted into the tree.
When I am inserting a new leaf, I have to...
I have the following code:
struct treenode;
typedef struct treenode* TreeNode;
struct treenode {
void* data;
TreeNode left, right;
};
TreeNode newTreeNode(void* data){
TreeNode node = (TreeNode) malloc(sizeof(struct treenode));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
bool insert(Tre...
Hi, assume this following function:
int binaryTree::findHeight(node *n) {
if (n == NULL) {
return 0;
} else {
return 1 + max(findHeight(n->left), findHeight(n->right));
}
}
Pretty standard recursive treeHeight function for a given binary search tree binaryTree. Now, I was helping a friend (he's taking an al...
I am looking for an algorithm that could find the two most distant elements in a binary tree, not looking for any special language, just for the algorithm.
Thanks.
...
Today, I went for an interview and the interviewer asked me how I would find the index of a given value (number) in a pre-sorted array like this:
$preSortedArr=array(23,32,36,41,45,54);
He also said that using recursion is not allowed.
I think the function should look like this:
function findIndexByValue($preSortedArray,$value){ ...
So, I was just digging around Reflector trying to find the implementation details of HashSet (out of sheer curiosity based on the answer to another question here) and noticed the following:
internal class TreeSet<T> : ICollection<T>, IEnumerable<T>, ICollection,
IEnumerable, ISerializable, IDeserializationCallback
Without looking ...
I wonder how I should use the GTree (from GLib) to store data? Every new value I insert into GTree with g_tree_insert routine is overwrite the previous one!
GTree *tree; //init
tree = g_tree_new( g_str_equal ); //"g_str_equal" is a GLib default compare func
//...
for( i = 0; i < 100; ++i )
g_tree_insert( tree, random_key(), random_v...
I had a quesiton here, but it didn't save. I'm having trouble balancing a fully unbalanced tree (nodes 1-15 along the right side).
I'm having trouble because I get stack overflow.
> // balancing
public void balance(node n) {
if(n != null) {
System.out.println(height(n)-levels);
if (height(n.RCN) != h...
I have a collection of items (big rationals) that I'll be processing. In each case, processing will consist of removing the smallest item in the collection, doing some work, and then adding 0-2 new items (which will always be larger than the removed item). The collection will be initialised with one item, and work will continue until it ...
I have a binary tree of functions and terminal values. I'd like to print this tree as a lisp statement would be represented!
For example, a tree with just a root of "+" and terminals of "2" and 4" would read (+ (2 4)).
...
Using the method presented here: http://cslibrary.stanford.edu/110/BinaryTrees.html#java
12. countTrees() Solution (Java)
/**
For the key values 1...numKeys, how many structurally unique
binary search trees are possible that store those keys?
Strategy: consider that each value could be the root.
Recursively find the size of the le...
Hi all,
I wonder if someone could take a minute out of their day to give their two cents on my problem.
I would like some suggestions on what would be the best data structure for representing, on disk, a large data set of time series data. The main priority is speed of insertion, with other priorities in decreasing order; speed of ret...
I was once asked of this in an interview:
How to write a recursive function that
returns a linked list of nodes, when
given a binary tree of nodes?
(flattening the data) (Update: how about, don't just traverse the tree and add the nodes to a global structure. Make the function totally recursive, and modifying the binary tree...
I have a Find function in order to find an element from a BST
private Node Find(ref Node n, int e)
{
if (n == null)
return null;
if (n.Element == e)
return n;
if (e > n.Element)
return Find(ref n.Right, e);
else
retu...
I have a very simple binary tree structure, something like:
struct nmbintree_s {
unsigned int size;
int (*cmp)(const void *e1, const void *e2);
void (*destructor)(void *data);
nmbintree_node *root;
};
struct nmbintree_node_s {
void *data;
struct nmbintree_node_s *right;
struct nmbintree_node_s *left;
};
So...
I've been trying to implement a delete function for a Binary Search Tree but haven't been able to get it to work in all cases.
This is my latest attempt:
Node* RBT::BST_remove(int c)
{
Node* t = get_node(c);
Node* temp = t;
if(t->get_left() == empty)
*t = *t->get_left();
else if(t->get_right() == empty)
...
I want to make a generic BST, that can be made up of any data type, but i'm not sure how I could add things to the tree, if my BST is generic. All of my needed code is below. I want my BST made up of Locations, and sorted by the x variable. Any help is appreciated.
Major thanks for looking.
public void add(E element)
{
if (root == ...