Data Structures - 20E029 - Assignment2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

DATA STRUCTURES ASSIGNMENT-2

NAME: JAYA
DHARSHINI H
REG NO:20E029
Problem 1:
Develop a program using any programming language to calculate the balance factor for a
given node in an AVL tree.
Note: Balance factor for a node can be calculated based on the difference between the
height of the left subtree and the height of the right subtree.

public static int height(Treenode root){


if(root==null||root.right==null&&root.left==null)
return 0;
return 1+Math.max(height(root.right),height(root.left));
}
public static int balancefactor(Treenode root){
return height(root.left)-height(root.right);
}
Problem 2:
Consider a hash table of size 11 that uses open addressing with linear probing. Let the hash
function H(k) be k mode 11. A sequence of records with keys 43 36 92 87 11 4 71 13 and 14
is inserted in to an initially empty hash table, the bins of which are indexed from zero to ten.
Apply linear probing and figure out the index of the bin into which the last record is
inserted. Demonstrate every step in detail.
public class Hashmap {
int size=11;
Integer[] ht;
public Hashmap(){
this.ht=new Integer[size];
}
public void insert(int x){
for(int i=0;i<size;i++){
int key=(x+i)%size;
if(this.ht[key]==null){
this.ht[key]=x;
return;
}
}
System.out.println("Hashtable is full");
}
public void print(){
for (int i=0;i<size;i++){
if(this.ht[i]==null)
System.out.println(i+"-null");
else
System.out.println(i+"-"+this.ht[i]);
}
}
Problem 3:
Develop a program using any programming language to demonstrate the deletion operation
in a Binary Search Tree (BST). You should implement logic for the following 3 cases:
a) Delete a node with 0 child.
b) Delete a node with 1 child.
c) Delete a node with 2 child.

public static Treenode DeleteBST(Treenode root,int val) {


if (root == null) return root;
if (val < root.data) root.left = DeleteBST(root.left, val);
else if (val > root.data) root.right = DeleteBST(root.right, val);
else {
if (root.right == null && root.left == null)//case 1: no child;
root = null;
else if (root.right == null)//case2:1 Child
root=root.left;
else if (root.left == null)
root=root.right;
else { //case3:2 Child
root.data=min(root.right);
root.right=DeleteBST(root.right,root.data);
}
}
return root;
}
public static int min(Treenode root){
if(root.left==null) return root.data;
return min(root.left);
}
Problem 4:
a. Develop a program using any programming language to determine the smallest value in a
given binary search tree.
public static int min(Treenode root){
if(root.left==null) return root.data;
return min(root.left);
}

b. Develop a program using any programming language to determine the k th largest


element in a given Binary search tree (BST)
public static int Kthlargest(Treenode root,int k){
ArrayList<Integer> l=InorderIterativeTraversal(root);
return l.get(l.size()-k);
}
public static ArrayList InorderIterativeTraversal(Treenode root){
if(root==null) return new ArrayList<>();
Stack<Treenode> st=new Stack<>();
ArrayList<Integer> l=new ArrayList<>();
Treenode temp=root;
while (!st.isEmpty()||temp!=null){
if(temp!=null){
st.push(temp);
temp=temp.left;
}
else{
temp=st.pop();
l.add(temp.data);
temp=temp.right;
}
}
return l;
}

Problem 5:
a. Develop a program using any programming language to print those nodes in a binary
tree that does not have a sibling.
public static void NodeWithoutSibilings(Treenode root){
if(root==null)
return;
if(root.right!=null&&root.left!=null){
NodeWithoutSibilings(root.left);
NodeWithoutSibilings(root.right);
}
else if(root.left!=null){
System.out.print(root.left.data+" ");
NodeWithoutSibilings(root.left);
}
else if(root.right!=null){
System.out.print(root.right.data+" ");
NodeWithoutSibilings(root.right);
}
}

b. Develop a program using any programming language to count the number of leaf nodes
in a binary tree of size N.
public static int LeafNodecount(Treenode root){
if(root==null)
return 0;
if(root.left==null && root.right==null)
return 1;
return LeafNodecount(root.left)+LeafNodecount(root.right);
}

You might also like