├── Insertion in AVL tree.cpp ├── binary search tree.cpp ├── linked list program.c++ ├── reverse a string using.cpp └── stack program.cpp /Insertion in AVL tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class BinaryNode { 5 | public: 6 | int key; 7 | int height; 8 | BinaryNode *left, *right; 9 | }; 10 | 11 | BinaryNode* newNode1(int data) { 12 | BinaryNode* temp = new BinaryNode(); 13 | temp->key = data; 14 | temp->left = NULL; 15 | temp->right = NULL; 16 | temp->height = 1; 17 | return temp; 18 | } 19 | 20 | BinaryNode* display(BinaryNode* root) { 21 | if (root == NULL) { 22 | cout << "Tree Empty" << endl; 23 | return root; 24 | } 25 | 26 | queue queue; 27 | queue.push(root); 28 | 29 | while (!queue.empty()) { 30 | BinaryNode* front = queue.front(); 31 | queue.pop(); 32 | cout << front->key << " -> "; 33 | 34 | if (front->left != NULL) { 35 | queue.push(front->left); 36 | } 37 | if (front->right != NULL) { 38 | queue.push(front->right); 39 | } 40 | } 41 | cout << "NULL" << endl; 42 | return root; 43 | } 44 | 45 | int getHeight(BinaryNode* node) 46 | { 47 | if (node == NULL) 48 | { 49 | return 0; 50 | } 51 | return node->height; 52 | } 53 | int getBalance(BinaryNode* node) 54 | { 55 | if (node == NULL) 56 | { 57 | return 0; 58 | } 59 | return getHeight(node->left) - getHeight(node->right); 60 | } 61 | 62 | 63 | BinaryNode* rotateRight(BinaryNode* disbalancedNode) { 64 | BinaryNode* newRoot = disbalancedNode->left; 65 | disbalancedNode->left = newRoot->right; 66 | newRoot->right = disbalancedNode; 67 | disbalancedNode->height = 1 + max(getHeight(disbalancedNode->left), getHeight(disbalancedNode->right)); 68 | newRoot->height = 1 + max(getHeight(newRoot->left), getHeight(newRoot->right)); 69 | 70 | return newRoot; 71 | } 72 | 73 | BinaryNode* rotateLeft(BinaryNode* disbalancedNode) { 74 | BinaryNode* newRoot = disbalancedNode->right; 75 | disbalancedNode->right = newRoot->left; 76 | newRoot->left = disbalancedNode; 77 | disbalancedNode->height = 1 + max(getHeight(disbalancedNode->left), getHeight(disbalancedNode->right)); 78 | newRoot->height = 1 + max(getHeight(newRoot->left), getHeight(newRoot->right)); 79 | 80 | return newRoot; 81 | } 82 | 83 | 84 | 85 | BinaryNode* insert(BinaryNode* node, int key) { 86 | if (node == NULL) { 87 | return newNode1(key); 88 | } 89 | if (key < node->key) 90 | { 91 | node->left = insert(node->left, key); 92 | } 93 | else if (key > node->key) 94 | { 95 | node->right = insert(node->right, key); 96 | } 97 | else 98 | { 99 | return node; 100 | } 101 | 102 | node->height = 1 + max(getHeight(node->left), getHeight(node->right)); 103 | 104 | int balance = getBalance(node); 105 | 106 | if (balance > 1 && key < node->left->key) { 107 | return rotateRight(node); 108 | } 109 | 110 | if (balance < -1 && key > node->right->key) { 111 | return rotateLeft(node); 112 | } 113 | 114 | if (balance > 1 && key > node->left->key) { 115 | node->left = rotateLeft(node->left); 116 | return rotateRight(node); 117 | } 118 | 119 | if (balance < -1 && key < node->right->key) { 120 | node->right = rotateRight(node->right); 121 | return rotateLeft(node); 122 | } 123 | 124 | return node; 125 | } 126 | 127 | BinaryNode* minValueNode(BinaryNode* node) { 128 | BinaryNode* current = node; 129 | while (current->left != NULL) { 130 | current = current->left; 131 | } 132 | return current; 133 | } 134 | 135 | 136 | /*BinaryNode* deleteNode(BinaryNode* root, int key) { 137 | if (root == NULL) { 138 | return root; 139 | } 140 | 141 | if (key < root->key) 142 | { 143 | root->left = deleteNode(root->left, key); 144 | } 145 | else if (key > root->key) 146 | { 147 | root->right = deleteNode(root->right, key); 148 | } 149 | else 150 | { 151 | if ((root->left == NULL) || (root->right == NULL)) 152 | { 153 | BinaryNode* temp = root->left ? root->left : root->right; 154 | if (temp == NULL) 155 | { 156 | temp = root; 157 | root = NULL; 158 | } 159 | else 160 | { 161 | *root = *temp; 162 | } 163 | delete temp; 164 | } 165 | else 166 | { 167 | BinaryNode* temp = minValueNode(root->left); 168 | root->key = temp->key; 169 | root->right = deleteNode(root->right, temp->key); 170 | } 171 | } 172 | 173 | if (root == NULL) 174 | { 175 | return root; 176 | } 177 | 178 | root->height = 1 + max(getHeight(root->left), getHeight(root->right)); 179 | 180 | int balance = getBalance(root); 181 | 182 | if (balance > 1 && getBalance(root->left) >= 0) 183 | { 184 | return rotateRight(root); 185 | } 186 | 187 | if (balance > 1 && getBalance(root->left) < 0) 188 | { 189 | root->left = rotateLeft(root->left); 190 | return rotateRight(root); 191 | } 192 | 193 | if (balance < -1 && getBalance(root->right) <= 0) 194 | { 195 | return rotateLeft(root); 196 | } 197 | 198 | if (balance < -1 && getBalance(root->right) > 0) 199 | { 200 | root->right = rotateRight(root->right); 201 | return rotateLeft(root); 202 | } 203 | 204 | return root; 205 | } 206 | */ 207 | int main() 208 | { 209 | BinaryNode* root = NULL; 210 | 211 | root = insert(root, 5); 212 | root = insert(root, 10); 213 | root = insert(root, 15); 214 | root = insert(root, 20); 215 | root = insert(root, 25); 216 | root = insert(root, 30); 217 | root = insert(root, 35); 218 | display(root); 219 | root = deleteNode(root,15); 220 | display(root); 221 | return 0; 222 | } 223 | -------------------------------------------------------------------------------- /binary search tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node 4 | { 5 | public: 6 | int key; 7 | Node*left; 8 | Node*right; 9 | }; 10 | Node*newnode(int data) 11 | { 12 | Node*k=new Node(); 13 | k->key=data; 14 | k->left=NULL; 15 | k->right=NULL; 16 | return k; 17 | } 18 | 19 | 20 | Node*Insert(int data,Node*root) 21 | { 22 | Node*nn=newnode(data); 23 | if(root==NULL) 24 | { 25 | root=nn; 26 | } 27 | else 28 | { queueq; 29 | q.push(root); 30 | Node*pre=NULL; 31 | while(!q.empty()) 32 | { 33 | pre=q.front(); 34 | q.pop(); 35 | if(pre->key > nn->key) 36 | { 37 | 38 | if(pre->left==NULL) 39 | { 40 | pre->left=nn; 41 | break; 42 | } 43 | else if(pre->left!=NULL) 44 | 45 | { 46 | q.push(pre->left); 47 | } 48 | 49 | } 50 | if(pre->key < nn->key) 51 | { 52 | if(pre->right==NULL) 53 | { 54 | pre->right=nn; 55 | break; 56 | } 57 | else if(pre->right!=NULL) 58 | { 59 | q.push(pre->right); 60 | } 61 | } 62 | } 63 | } 64 | return root; 65 | } 66 | 67 | Node*display(Node*root) 68 | { 69 | if(root==NULL) 70 | { 71 | cout<<"tree is empty"<q; 76 | q.push(root); 77 | Node*pre=NULL; 78 | while(!q.empty()) 79 | { 80 | pre=q.front(); 81 | q.pop(); 82 | cout<key<<" "; 83 | if(pre->left!=NULL) 84 | { 85 | q.push(pre->left); 86 | } 87 | if(pre->right!=NULL) 88 | { 89 | q.push(pre->right); 90 | } 91 | } 92 | cout<<"NULL"< 2 | using namespace std; 3 | class Node//declaring address and data 4 | { 5 | public: 6 | int key; 7 | Node*next; 8 | }; 9 | Node*newnode(int data)//creating new node 10 | { 11 | Node*n=new Node(); 12 | n->key=data; 13 | n->next=NULL; 14 | return n; 15 | } 16 | Node*insert_begin(int data,Node*head) 17 | { 18 | Node*nn=newnode(data); 19 | if(head==NULL) 20 | { 21 | head=nn; 22 | } 23 | else 24 | { 25 | nn->next=head; 26 | head=nn; 27 | } 28 | return head; 29 | } 30 | Node*insert_end(int data,Node*head) 31 | { 32 | Node*nn=newnode(data); 33 | if(head==NULL) 34 | { 35 | nn=head; 36 | } 37 | else 38 | { 39 | Node*temp=head; 40 | while(temp->next!=NULL) 41 | { 42 | temp=temp->next; 43 | } 44 | temp->next=nn; 45 | } 46 | return head; 47 | } 48 | Node*insert_mid(int data,Node*head,int pos) 49 | { 50 | Node*nn=newnode(data); 51 | if(head==NULL) 52 | { 53 | nn=head; 54 | } 55 | 56 | else 57 | { int count=1; 58 | Node*temp=head; 59 | while(temp->next!=NULL) 60 | { 61 | count++; 62 | temp=temp->next; 63 | if(count ==pos) 64 | { 65 | break; 66 | } 67 | } 68 | 69 | 70 | if(count == pos) 71 | { 72 | nn->next=temp->next; 73 | temp->next=nn; 74 | } 75 | else 76 | { 77 | cout<<"invalid position"<key<<"->"; 88 | temp=temp->next; 89 | } 90 | cout<next; 102 | } 103 | return head; 104 | } 105 | 106 | Node* delete_end(Node* head) 107 | { 108 | Node* temp = head; 109 | if(head == NULL) 110 | { 111 | cout << "Invalid" << " "; 112 | } 113 | else if(head->next == NULL) 114 | { 115 | head = NULL; 116 | } 117 | else 118 | { 119 | while(temp->next->next != NULL) 120 | { 121 | temp = temp->next; 122 | } 123 | temp->next = NULL; 124 | } 125 | return head; 126 | } 127 | Node* delete_pos(int pos,Node* head) 128 | { 129 | if(head == NULL) 130 | { 131 | cout << "Invalid" << " "; 132 | } 133 | else if(pos==1) 134 | { 135 | head=delete_first(head); 136 | } 137 | else 138 | { 139 | int count = 1; 140 | Node* temp = head; 141 | while(temp != NULL) 142 | { 143 | Node* temp1 = temp->next; 144 | count++; 145 | if(count == pos) 146 | { 147 | temp->next = temp1->next; 148 | temp1->next = NULL; 149 | break; 150 | } 151 | temp = temp->next; 152 | } 153 | } 154 | return head; 155 | } 156 | 157 | Node* search(Node*head,int data) 158 | { 159 | if(head==NULL) 160 | { 161 | cout<<"element not present"<next; 170 | if(temp->key==data) 171 | { 172 | cout<<"element found"<next==NULL) 189 | cout<<"List contain one value"; 190 | else{ 191 | Node*temp1=NULL; 192 | Node*temp2=head; 193 | Node*temp3=head->next; 194 | while(temp3!=NULL){ 195 | temp2->next=temp1; 196 | temp1=temp2; 197 | temp2=temp3; 198 | temp3=temp2->next; 199 | } 200 | temp2->next=temp1; 201 | head=temp2; 202 | } 203 | return head; 204 | } 205 | int main() 206 | { 207 | Node*head=NULL; 208 | head=insert_begin(20,head); 209 | head=insert_begin(15,head); 210 | head=insert_begin(10,head); 211 | head=insert_end(70,head); 212 | head=insert_mid(60,head,3); 213 | //head=delete_pos(3,head); 214 | 215 | //head=search(head,70); 216 | head=display(head); 217 | head=reverse(head); 218 | //head=display(head); 219 | } 220 | -------------------------------------------------------------------------------- /reverse a string using.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void output(string s) 4 | { 5 | stackstack; 6 | string ans=""; 7 | int count =0; 8 | for(char c:s) 9 | { 10 | if(c!=' ') 11 | { 12 | ans=ans+c; 13 | count++; 14 | } 15 | else if(c==' '&& count!=0) 16 | { 17 | stack.push(ans); 18 | count=0; 19 | ans=""; 20 | } 21 | } 22 | stack.push(ans); 23 | while(!stack.empty()) 24 | { 25 | cout< 2 | using namespace std; 3 | //Stack------Program 4 | class Node 5 | { 6 | public: 7 | char key; 8 | Node* next; 9 | }; 10 | 11 | Node* newnode(char data) 12 | { 13 | Node* n = new Node(); 14 | n->key = data; 15 | n->next = NULL; 16 | return n; 17 | } 18 | 19 | Node* push(int data,Node* head) 20 | { 21 | Node* nn = newnode(data); 22 | if(head==NULL) 23 | { 24 | head = nn; 25 | } 26 | else 27 | { 28 | nn->next = head; 29 | head = nn; 30 | } 31 | return head; 32 | } 33 | 34 | Node* pop(Node*head) 35 | { 36 | if(head == NULL) 37 | { 38 | cout << "Not possible" << " "; 39 | } 40 | else 41 | { 42 | head = head->next; 43 | } 44 | return head; 45 | } 46 | 47 | Node* display(Node* head) 48 | { 49 | if(head == NULL) 50 | { 51 | cout << "No elements" << " "; 52 | } 53 | else 54 | { 55 | Node* temp = head; 56 | while(temp!=NULL) 57 | { 58 | cout << temp->key << endl; 59 | temp = temp->next; 60 | } 61 | } 62 | return head; 63 | } 64 | 65 | int main() { 66 | Node* head = NULL; 67 | string s; 68 | cin >> s; 69 | for(int i=0;i