From c0c8f3f09ab37c04bc87c66f47752de76c23acbd Mon Sep 17 00:00:00 2001 From: rg1107 Date: Sat, 19 Oct 2019 11:50:37 +0530 Subject: [PATCH] Solved issues --- c/ds/avl_tree.c | 271 +++++++++++++++++++++++++++++++++++++++ java/ds/HashTable.java | 175 +++++++++++++++++++++++++ python/ds/linked_list.py | 35 +++++ 3 files changed, 481 insertions(+) create mode 100644 c/ds/avl_tree.c create mode 100644 java/ds/HashTable.java create mode 100644 python/ds/linked_list.py diff --git a/c/ds/avl_tree.c b/c/ds/avl_tree.c new file mode 100644 index 000000000..bc2e47022 --- /dev/null +++ b/c/ds/avl_tree.c @@ -0,0 +1,271 @@ +#include + +typedef struct node +{ + int data; + struct node *left,*right; + int ht; +}node; + +node *insert(node *,int); +node *Delete(node *,int); +void preorder(node *); +void inorder(node *); +int height( node *); +node *rotateright(node *); +node *rotateleft(node *); +node *RR(node *); +node *LL(node *); +node *LR(node *); +node *RL(node *); +int BF(node *); + +int main() +{ + node *root=NULL; + int x,n,i,op; + + do + { + printf("\n1)Create:"); + printf("\n2)Insert:"); + printf("\n3)Delete:"); + printf("\n4)Print:"); + printf("\n5)Quit:"); + printf("\n\nEnter Your Choice:"); + scanf("%d",&op); + + switch(op) + { + case 1: printf("\nEnter no. of elements:"); + scanf("%d",&n); + printf("\nEnter tree data:"); + root=NULL; + for(i=0;idata=x; + T->left=NULL; + T->right=NULL; + } + else + if(x > T->data) // insert in right subtree + { + T->right=insert(T->right,x); + if(BF(T)==-2) + if(x>T->right->data) + T=RR(T); + else + T=RL(T); + } + else + if(xdata) + { + T->left=insert(T->left,x); + if(BF(T)==2) + if(x < T->left->data) + T=LL(T); + else + T=LR(T); + } + + T->ht=height(T); + + return(T); +} + +node * Delete(node *T,int x) +{ + node *p; + + if(T==NULL) + { + return NULL; + } + else + if(x > T->data) // insert in right subtree + { + T->right=Delete(T->right,x); + if(BF(T)==2) + if(BF(T->left)>=0) + T=LL(T); + else + T=LR(T); + } + else + if(xdata) + { + T->left=Delete(T->left,x); + if(BF(T)==-2) //Rebalance during windup + if(BF(T->right)<=0) + T=RR(T); + else + T=RL(T); + } + else + { + //data to be deleted is found + if(T->right!=NULL) + { //delete its inorder succesor + p=T->right; + + while(p->left!= NULL) + p=p->left; + + T->data=p->data; + T->right=Delete(T->right,p->data); + + if(BF(T)==2)//Rebalance during windup + if(BF(T->left)>=0) + T=LL(T); + else + T=LR(T);\ + } + else + return(T->left); + } + T->ht=height(T); + return(T); +} + +int height(node *T) +{ + int lh,rh; + if(T==NULL) + return(0); + + if(T->left==NULL) + lh=0; + else + lh=1+T->left->ht; + + if(T->right==NULL) + rh=0; + else + rh=1+T->right->ht; + + if(lh>rh) + return(lh); + + return(rh); +} + +node * rotateright(node *x) +{ + node *y; + y=x->left; + x->left=y->right; + y->right=x; + x->ht=height(x); + y->ht=height(y); + return(y); +} + +node * rotateleft(node *x) +{ + node *y; + y=x->right; + x->right=y->left; + y->left=x; + x->ht=height(x); + y->ht=height(y); + + return(y); +} + +node * RR(node *T) +{ + T=rotateleft(T); + return(T); +} + +node * LL(node *T) +{ + T=rotateright(T); + return(T); +} + +node * LR(node *T) +{ + T->left=rotateleft(T->left); + T=rotateright(T); + + return(T); +} + +node * RL(node *T) +{ + T->right=rotateright(T->right); + T=rotateleft(T); + return(T); +} + +int BF(node *T) +{ + int lh,rh; + if(T==NULL) + return(0); + + if(T->left==NULL) + lh=0; + else + lh=1+T->left->ht; + + if(T->right==NULL) + rh=0; + else + rh=1+T->right->ht; + + return(lh-rh); +} + +void preorder(node *T) +{ + if(T!=NULL) + { + printf("%d(Bf=%d)",T->data,BF(T)); + preorder(T->left); + preorder(T->right); + } +} + +void inorder(node *T) +{ + if(T!=NULL) + { + inorder(T->left); + printf("%d(Bf=%d)",T->data,BF(T)); + inorder(T->right); + } +} diff --git a/java/ds/HashTable.java b/java/ds/HashTable.java new file mode 100644 index 000000000..f33e2be5c --- /dev/null +++ b/java/ds/HashTable.java @@ -0,0 +1,175 @@ +import java.util.ArrayList; + +// A node of chains +class HashNode +{ + K key; + V value; + + // Reference to next node + HashNode next; + + // Constructor + public HashNode(K key, V value) + { + this.key = key; + this.value = value; + } +} + +// Class to represent entire hash table +class Map +{ + // bucketArray is used to store array of chains + private ArrayList> bucketArray; + + // Current capacity of array list + private int numBuckets; + + // Current size of array list + private int size; + + // Constructor (Initializes capacity, size and + // empty chains. + public Map() + { + bucketArray = new ArrayList<>(); + numBuckets = 10; + size = 0; + + // Create empty chains + for (int i = 0; i < numBuckets; i++) + bucketArray.add(null); + } + + public int size() { return size; } + public boolean isEmpty() { return size() == 0; } + + // This implements hash function to find index + // for a key + private int getBucketIndex(K key) + { + int hashCode = key.hashCode(); + int index = hashCode % numBuckets; + return index; + } + + // Method to remove a given key + public V remove(K key) + { + // Apply hash function to find index for given key + int bucketIndex = getBucketIndex(key); + + // Get head of chain + HashNode head = bucketArray.get(bucketIndex); + + // Search for key in its chain + HashNode prev = null; + while (head != null) + { + // If Key found + if (head.key.equals(key)) + break; + + // Else keep moving in chain + prev = head; + head = head.next; + } + + // If key was not there + if (head == null) + return null; + + // Reduce size + size--; + + // Remove key + if (prev != null) + prev.next = head.next; + else + bucketArray.set(bucketIndex, head.next); + + return head.value; + } + + // Returns value for a key + public V get(K key) + { + // Find head of chain for given key + int bucketIndex = getBucketIndex(key); + HashNode head = bucketArray.get(bucketIndex); + + // Search key in chain + while (head != null) + { + if (head.key.equals(key)) + return head.value; + head = head.next; + } + + // If key not found + return null; + } + + // Adds a key value pair to hash + public void add(K key, V value) + { + // Find head of chain for given key + int bucketIndex = getBucketIndex(key); + HashNode head = bucketArray.get(bucketIndex); + + // Check if key is already present + while (head != null) + { + if (head.key.equals(key)) + { + head.value = value; + return; + } + head = head.next; + } + + // Insert key in chain + size++; + head = bucketArray.get(bucketIndex); + HashNode newNode = new HashNode(key, value); + newNode.next = head; + bucketArray.set(bucketIndex, newNode); + + // If load factor goes beyond threshold, then + // double hash table size + if ((1.0*size)/numBuckets >= 0.7) + { + ArrayList> temp = bucketArray; + bucketArray = new ArrayList<>(); + numBuckets = 2 * numBuckets; + size = 0; + for (int i = 0; i < numBuckets; i++) + bucketArray.add(null); + + for (HashNode headNode : temp) + { + while (headNode != null) + { + add(headNode.key, headNode.value); + headNode = headNode.next; + } + } + } + } + + // Driver method to test Map class + public static void main(String[] args) + { + Mapmap = new Map<>(); + map.add("this",1 ); + map.add("coder",2 ); + map.add("this",4 ); + map.add("hi",5 ); + System.out.println(map.size()); + System.out.println(map.remove("this")); + System.out.println(map.remove("this")); + System.out.println(map.size()); + System.out.println(map.isEmpty()); + } +} diff --git a/python/ds/linked_list.py b/python/ds/linked_list.py new file mode 100644 index 000000000..fcd3d6b18 --- /dev/null +++ b/python/ds/linked_list.py @@ -0,0 +1,35 @@ +class Node: + + # Function to initialise the node object + def __init__(self, data): + self.data = data # Assign data + self.next = None # Initialize next as null + + +# Linked List class contains a Node object +class LinkedList: + + # Function to initialize head + def __init__(self): + self.head = None + + +# Code execution starts here +if __name__=='__main__': + + # Start with the empty list + llist = LinkedList() + + llist.head = Node(1) + second = Node(2) + third = Node(3) + + + + llist.head.next = second; # Link first node with second + + + + second.next = third; # Link second node with the third node + +