Google Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
In Binary tree it does not matter your can insert the node any where, but it is BST i.e. Binary search tree then it has satisfy the Binary search property that left child key has to be smaller then root and right child key has to be larger then root.
duplicate key is not allowed in BS Tree. because when you want to search a node you will always hit the first occurrence so the second node with the same key will have no value.
I wrote functions for doing it
/**
* Function: insert_rec
* Input : root node and the data to be inserted
* Output : Tree with the inserted node
* Author : Qamar Alam (Vasil Technologies)
* Description: recursive function to insert a node
*/
TreeNode* Tree::insert_rec(TreeNode* ptr, int x) {
size++;
if (ptr == NULL) {
ptr=new TreeNode(x);
return ptr;
}
if(x < ptr->getDataInt()) ptr->left=insert_rec(ptr->left, x);
else if (x >ptr->getDataInt()) ptr->right=insert_rec(ptr->right, x);
else {
cout << "cant have two nodes with same key"<<endl;
size--;
}
return ptr;
}
/**
* Function: insert
* Input : root node and the data to be inserted
* Output : Tree with the inserted node
* Author : Qamar Alam(Vasil Technologies)
* Description: non recursive function to insert a node
*/
void Tree::insert(int x, int y) {
root = insert_rec(root, x);
return;
// to use non recursive comment the above two lines
TreeNode* ptr=root;
size++;
// if tree is empty then make it a root node
if (ptr == NULL) {
ptr=new TreeNode(x);
root=ptr;
return;
}
//find the place where it can be added as leaf node
while (ptr != NULL) {
if( x < ptr->getDataInt() ) //search the place in the left sub-tree
{
if (ptr->left == NULL)
{
ptr->left= new TreeNode(x);
return;
}
else
{
ptr=ptr->left;
}
}
else if(x > ptr->getDataInt())// search the place in the right sub-tree
{
if (ptr->right == NULL)
{
ptr->right= new TreeNode(x);
return;
}
else
{
ptr=ptr->right;
}
}
else //key exist in the tree and error out or ignore
{
cout << "cant have two nodes with same key"<<endl;
size--;
return;
}
}
return;
}
Yes,
Binary tree can have duplicates where as Binary Search Tree (BST) cannot. However, Binary tree cannot have both child node values equal at the same level.
Inserting a element which is equal to root node value, it will put the value into left child first.
e.g.
5
/ \
3 7
/ /
3 7
Its a binary tree not a binary "search" tree. It does not matter what and how you insert. All that binary tree guarantees is that every parent has two children. So inserting duplicates on left or right does not matter.
So Axecapone,
Can duplicates be allowed in a binary search tree? If yes, why , if no why? Please explain.
In binary search tree, duplicate will inserted as the right node of the node which is having same value.
Why couldn't a BST have duplicates? Even if your existing BST design did not allow for them, you could easily accommodate duplicates simply by giving each node an integer that would store a count.
http: //cslibrary.stanford.edu/110/BinaryTrees + ".html"
I really dont know why can't we have duplicates.
Stanford will prove it for others who are confused.
http: //cslibrary.stanford.edu/110/BinaryTrees + ".html"
I really dont know why can't we have duplicates.
Stanford will prove it for others who are confused.
Scofield: I'm not sure what you're referring to on that page. Are you arguing for or against duplicates? If against, I've given a rather ironclad argument above for why we can rest assured that we can make a design that accommodates duplicates.
Extending the response from ‘axecapone’, I think too that binary tree is just a representation of parent child relationship and thus duplicate key is most likely unnecessary. However if a duplicate key in a particular case tends to break the invariant of parent child relation in that case each node should maintain collection of values corresponding to colliding keys (keys with same value). If for a non-duplicate binary tree, a node is represented by three parameters – leftChild, rightChild and value then with duplicate keys a node should be modified to hold a collection of values. In addition appropriate data structure such as linked list or set or the likes should be used for holding collection of values corresponding to duplicate keys.
In a binary tree the value can go anywhere as long as each node in resulting tree has between 0 and 2 children. In this question, BST is most likely meant. The choise where to place value equal to a given node in BST is up to the implementation. One may decide that the "left side is less than or equal to" while right sid is "more than" or the other way around.
- Anonymous November 24, 2012