Microsoft Interview Question
InternsCountry: United States
class Node
{
int data;
Node left, right;
public Node(int key)
{
data = key;
left = null;
right = null;
}
}
class unordered
{
Node root;
unordered(int key)
{
root = new Node(key);
}
unordered()
{
root=null;
}
public void uotollrec(Node roo, List<Integer> ll)
{
if(roo==null)
return;
ll.add(roo.data);
uotollrec(roo.left,ll);
uotollrec(roo.right,ll);
}
}
class BinarySearchTree
{
Node root;
BinarySearchTree() {
root = null;
}
void addbinary(int item)
{
root = addrec(root,item);
}
Node addrec(Node root, int item)
{
if(root == null)
{
root = new Node(item);
return root;
}
if (root.data>item)
root.left=addrec(root.left,item);
else
root.right=addrec(root.right,item);
return root;
}
void trav()
{
traverse1(root);
}
void traverse1(Node roo)
{
if(roo!=null)
{
System.out.println(roo.data);
traverse1(roo.left);
traverse1(roo.right);
}
}
}
public class first
{
static List<Integer> ll = new ArrayList<Integer>();
BinarySearchTree bst = new BinarySearchTree();
public void uotobinary(unordered uo)
{
int r;
unordered u1 = new unordered();
u1.uotollrec(uo.root,ll);
Collections.sort(ll);
if(ll.size()%2==0)
r = ll.size()/2-1 ;
else
r = (ll.size()/2);
bst.addbinary(ll.get(r));
for(int i = r-1; i>=00;i--)
bst.addbinary(ll.get(i));
for(int i = r+1; i<ll.size() ; i++)
bst.addbinary(ll.get(i));
bst.trav();
}
public static void main(String[] args)
{
first f1 = new first();
unordered uo = new unordered();
uo.root = new Node(7);
uo.root.left=new Node(6);
uo.root.right=new Node(5);
uo.root.left.left=new Node(4);
uo.root.left.right=new Node(3);
uo.root.right.left=new Node(2);
uo.root.right.right=new Node(1);
f1.uotobinary(uo);
}
}
class Node
{
int data;
Node left, right;
public Node(int key)
{
data = key;
left = null;
right = null;
}
}
class unordered
{
Node root;
unordered(int key)
{
root = new Node(key);
}
unordered()
{
root=null;
}
public void uotollrec(Node roo, List<Integer> ll)
{
if(roo==null)
return;
ll.add(roo.data);
uotollrec(roo.left,ll);
uotollrec(roo.right,ll);
}
}
class BinarySearchTree
{
Node root;
BinarySearchTree() {
root = null;
}
void addbinary(int item)
{
root = addrec(root,item);
}
Node addrec(Node root, int item)
{
if(root == null)
{
root = new Node(item);
return root;
}
if (root.data>item)
root.left=addrec(root.left,item);
else
root.right=addrec(root.right,item);
return root;
}
void trav()
{
traverse1(root);
}
void traverse1(Node roo)
{
if(roo!=null)
{
System.out.println(roo.data);
traverse1(roo.left);
traverse1(roo.right);
}
}
}
public class first
{
static List<Integer> ll = new ArrayList<Integer>();
BinarySearchTree bst = new BinarySearchTree();
public void uotobinary(unordered uo)
{
int r;
unordered u1 = new unordered();
u1.uotollrec(uo.root,ll);
Collections.sort(ll);
if(ll.size()%2==0)
r = ll.size()/2-1 ;
else
r = (ll.size()/2);
bst.addbinary(ll.get(r));
for(int i = r-1; i>=00;i--)
bst.addbinary(ll.get(i));
for(int i = r+1; i<ll.size() ; i++)
bst.addbinary(ll.get(i));
bst.trav();
}
public static void main(String[] args)
{
first f1 = new first();
unordered uo = new unordered();
uo.root = new Node(7);
uo.root.left=new Node(6);
uo.root.right=new Node(5);
uo.root.left.left=new Node(4);
uo.root.left.right=new Node(3);
uo.root.right.left=new Node(2);
uo.root.right.right=new Node(1);
f1.uotobinary(uo);
}
}
class Node
{
int data;
Node left, right;
public Node(int key)
{
data = key;
left = null;
right = null;
}
}
class unordered
{
Node root;
unordered(int key)
{
root = new Node(key);
}
unordered()
{
root=null;
}
public void uotollrec(Node roo, List<Integer> ll)
{
if(roo==null)
return;
ll.add(roo.data);
uotollrec(roo.left,ll);
uotollrec(roo.right,ll);
}
}
class BinarySearchTree
{
Node root;
BinarySearchTree() {
root = null;
}
void addbinary(int item)
{
root = addrec(root,item);
}
Node addrec(Node root, int item)
{
if(root == null)
{
root = new Node(item);
return root;
}
if (root.data>item)
root.left=addrec(root.left,item);
else
root.right=addrec(root.right,item);
return root;
}
void trav()
{
traverse1(root);
}
void traverse1(Node roo)
{
if(roo!=null)
{
System.out.println(roo.data);
traverse1(roo.left);
traverse1(roo.right);
}
}
}
public class first
{
static List<Integer> ll = new ArrayList<Integer>();
BinarySearchTree bst = new BinarySearchTree();
public void uotobinary(unordered uo)
{
int r;
unordered u1 = new unordered();
u1.uotollrec(uo.root,ll);
Collections.sort(ll);
if(ll.size()%2==0)
r = ll.size()/2-1 ;
else
r = (ll.size()/2);
bst.addbinary(ll.get(r));
for(int i = r-1; i>=00;i--)
bst.addbinary(ll.get(i));
for(int i = r+1; i<ll.size() ; i++)
bst.addbinary(ll.get(i));
bst.trav();
}
public static void main(String[] args)
{
first f1 = new first();
unordered uo = new unordered();
uo.root = new Node(7);
uo.root.left=new Node(6);
uo.root.right=new Node(5);
uo.root.left.left=new Node(4);
uo.root.left.right=new Node(3);
uo.root.right.left=new Node(2);
uo.root.right.right=new Node(1);
f1.uotobinary(uo);
}
}
class Node
{
int data;
Node left, right;
public Node(int key)
{
data = key;
left = null;
right = null;
}
}
class unordered
{
Node root;
unordered(int key)
{
root = new Node(key);
}
unordered()
{
root=null;
}
public void uotollrec(Node roo, List<Integer> ll)
{
if(roo==null)
return;
ll.add(roo.data);
uotollrec(roo.left,ll);
uotollrec(roo.right,ll);
}
}
class BinarySearchTree
{
Node root;
BinarySearchTree() {
root = null;
}
void addbinary(int item)
{
root = addrec(root,item);
}
Node addrec(Node root, int item)
{
if(root == null)
{
root = new Node(item);
return root;
}
if (root.data>item)
root.left=addrec(root.left,item);
else
root.right=addrec(root.right,item);
return root;
}
void trav()
{
traverse1(root);
}
void traverse1(Node roo)
{
if(roo!=null)
{
System.out.println(roo.data);
traverse1(roo.left);
traverse1(roo.right);
}
}
}
public class first
{
static List<Integer> ll = new ArrayList<Integer>();
BinarySearchTree bst = new BinarySearchTree();
public void uotobinary(unordered uo)
{
int r;
unordered u1 = new unordered();
u1.uotollrec(uo.root,ll);
Collections.sort(ll);
if(ll.size()%2==0)
r = ll.size()/2-1 ;
else
r = (ll.size()/2);
bst.addbinary(ll.get(r));
for(int i = r-1; i>=00;i--)
bst.addbinary(ll.get(i));
for(int i = r+1; i<ll.size() ; i++)
bst.addbinary(ll.get(i));
bst.trav();
}
public static void main(String[] args)
{
first f1 = new first();
unordered uo = new unordered();
uo.root = new Node(7);
uo.root.left=new Node(6);
uo.root.right=new Node(5);
uo.root.left.left=new Node(4);
uo.root.left.right=new Node(3);
uo.root.right.left=new Node(2);
uo.root.right.right=new Node(1);
f1.uotobinary(uo);
}
}
what is an unordered tree?
- veeru February 09, 2017