Microsoft Interview Question
Software Engineer / DevelopersTeam: hyd
Country: India
Interview Type: In-Person
@shondik . but yours is not sorted .. if you want to sort u hav to compr prv elmnts with the elmnt being encountered cpx wud b o(n2);;;
What is generated as a result of inorder traversal of BST?
Of course, A sorted list of data. I am changing the pointers in midway.
What made you think that the list will not be sorted?
public static class BiNode
{
public BST.Node first, last;
}
public static BiNode convert(BST.Node root)
{
BiNode ret= new BiNode();
BiNode fl= new BiNode();
if(root.left!=null)
{
fl= convert(root.left);
fl.last.right= root;
root.left= fl.last;
}
ret.first= fl.first!=null?fl.first:root;
if(root.right!=null)
{
fl= convert(root.right);
fl.first.left= root;
root.right= fl.first;
}
ret.last= fl.last!=null?fl.last:root;
return ret;
}
// tree2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
struct node
{
int data;
struct node* left;
struct node *right;
};
struct link
{
int data;
struct link* next;
struct link* pre;
};
struct node* root;
struct link *head;
void insert(struct node **root,int data)
{
if((*root)==NULL)
{
(*root)=(struct node*)malloc(sizeof(struct node));
(*root)->data=data;
(*root)->left=NULL;
(*root)->right=NULL;
}
else
{
if(((*root)->data) > data)
insert(&((*root)->left),data);
else
insert(&((*root)->right),data);
}
}
void build_link(struct link **head,int data)
{
struct link *temp=NULL,*temp1=NULL;
if((*head)==NULL)
{
(*head)=(struct link*)malloc(sizeof(struct link));
(*head)->data=data;
(*head)->next=NULL;
(*head)->pre=NULL;
}
else
{
temp1=(*head);
while((temp1->next)!=NULL)
temp1=temp1->next;
temp=(struct link*)malloc(sizeof(struct link));
temp->data=data;
temp->next=NULL;
temp->pre=temp1;
temp1->next=temp;
}
}
void inorder(struct node *root)
{
if(root==NULL)
return;
inorder(root->left);
printf("%d",root->data);
build_link(&head,root->data);
inorder(root->right);
}
void main()
{
struct link* temp;
root=NULL;
int arr[10]={4,5,6,7,1,2,10,9,8};
int i=0;
while(i<10)
{
insert(&root,arr[i]);
i++;
}
inorder(root);
temp=head;
while(temp)
{
printf("\n%d",temp->data);
temp=temp->next;
}
return;
}
delete the minimum node from tree (by removing the leftmost node, which is easy) and then insert the node at the end of the linked list.
node * deleteSmallest(node *root) {
node* temp = root;
node* ptemp = NULL;
while(temp->left != NULL) {
ptemp = temp;
temp = temp->left;
}
ptemp->left = temp->right; //temp->left is always NULL.
return temp;
}
/* This function will return a linked list */
node* insertLeastNode(node *root) {
node *head = deleteSmallest(node *root);
node *running_node = head;
while((running_node->left = deleteSmallest(node *root)) != NULL) {
running_node = running_node->left;
}
return head;
}
Just in case we want our tree back, we have to point node->right of linked list to the parent.
We can further improve the performance by saving the last accessed node, since we have to return leftmost node from there.
node * deleteSmallest(node **root) {
node* temp = *root;
node* ptemp = NULL;
while(temp->left != NULL) {
ptemp = temp;
temp = temp->left;
}
ptemp->left = temp->right; //temp->left is always NULL.
*root = ptemp; //Store the root of the new subtree from which search of smallest node will start.
return temp;
}
/* This function will return a linked list */
node* insertLeastNode(node *root) {
node *head = deleteSmallest(node *root);
node *running_node = head;
while((running_node->left = deleteSmallest(&root)) != NULL) {
running_node = running_node->left;
}
return head;
}
void inorder( *root )
{
if( root==NULL)
return;
else
{
inorder( root->leftchild)
DLL( root->data );
inorder( root->rightchild );
}
void DLL(int t )
{
node* n;
n=new node;
n->data=t;
n->next=n->prev=NULL;
if ( list==NULL )
list=n;
else
{
n->next=list;
list->prev=n;
list=n;
}
}
This is modified inorder traversal converting BST to DLL.
bsttodll(struct node *root,struct node **prev,struct node **head)
{
if(root==NULL)
return;
bsttodll(root->left,prev,head);
if(*prev==NULL)
{
(*head)=root;
*prev=*head;
}
else
{
root->left=(*prev);
(*prev)->right=root;
*prev=root;
}
bsttodll(root->right,prev,head);
}
the call to the function will be bsttodll(root,NULL,NULL);
This is modified inorder traversal converting BST to DLL.
bsttodll(struct node *root,struct node **prev,struct node **head)
{
if(root==NULL)
return;
bsttodll(root->left,prev,head);
if(*prev==NULL)
{
(*head)=root;
*prev=*head;
}
else
{
root->left=(*prev);
(*prev)->right=root;
*prev=root;
}
bsttodll(root->right,prev,head);
}
the call to the function will be bsttodll(root,NULL,NULL);
perform iterative inorder traversal of the tree and arrange the left and right pointers accordingly!
inorder(NODEPTR start)
{
NODEPTR ptr=start;
NODEPTR prev;
while(1)
{
while(ptr->left!=NULL)
{
push(ptr);
ptr=ptr->left;
}
while(ptr->right==NULL)
{
if(prev==NULL)
{
start=ptr;//first node that is smallest node
prev=ptr;
}
else
{
ptr->left=prev;
prev->right=ptr;
prev=ptr;
}
if(stackempty())
return start;
ptr=pop();
}
prev->right=ptr;
ptr->left=prev;
prev=ptr;
ptr=ptr->right;
}//end of infinite while loop
public void treeToDoublyLinkedList() {
TreeNode head = null;
TreeNode prev = null;
treeToDoublyLinkedListT(root, head, prev);
}
private void treeToDoublyLinkedListT(TreeNode p, TreeNode head,
TreeNode prev) {
if (p == null) {
return;
}
treeToDoublyLinkedListT(p.left, head, prev);
p.left = prev;
if (prev != null) {
prev.right = p;
} else {
head = p;
}
TreeNode right = p.right;
head.left = p;
p.right = head;
prev = p;
treeToDoublyLinkedListT(right, head, prev);
}
Call BST2DLL(root,start);
- Aashish July 27, 2012Where start is the head of the DLL.