Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all c++ data structure code with explanation #348

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions dsa-code/LinkedListH1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <bits/stdc++.h>
#include "header/linkedList.h"
using namespace std;

void printLinkedList(Node* head){
Node* temp = head;
while(temp!=NULL){
cout<<temp->data<<endl;
temp=temp->next;
}
}

Node* takeInput(){
int data;
cin>>data;
Node* head = NULL;
while(data != -1){
Node* newNode = new Node(data);
if(head == NULL){
head = newNode;
}
else{
Node* temp = head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next = newNode;
}
cin>>data;
}
return head;
}

Node* takeInputOptimised(){
int data;
cin>>data;
Node* head=NULL;
Node* tail=NULL;
while(data != -1){
Node* newNode = new Node(data);
if(head==NULL){
head=newNode;
tail=newNode;
}
else{
tail->next=newNode;
tail=tail->next;

}
cin>>data;
}
return head;
}

void insertNode(Node* head,int i,int data){
Node* newNode = new Node(data);
int counter=0;
Node* temp = head;
while(counter<i-1){
temp=temp->next;
couter++;
}
Node* a =temp->next;
temp->next = newNode;
newNode->next = a;
}

void deleteNode(Node* head,int i){
int counter=0;
Node* temp = head;
while(counter<i-1){
temp=temp->next;
counter++;
}
Node* a = temp->next;
Node* b = a->next;
temp->next = b;
delete a;
}

int main() {
// Write C++ code here
cout<<"---------program started---------"<<endl;
Node n1(1);
Node n2(2);
Node n3(3);

n1.next = &n2;
n2.next = &n3;

Node* head = &n1;
printLinkedList(head);

//Node* head1=takeInput();
Node* head2 = takeInputOptimised();
printLinkedList(head2);


return 0;
}

9 changes: 9 additions & 0 deletions dsa-code/Tries&Huffman_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <iostream>

using namespace std;

int main()
{
cout<<"Program started"<<endl;
return 0;
}
55 changes: 55 additions & 0 deletions dsa-code/binaryTree_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <iostream>
using namespace std;

template <typename T>


class BinaryTreeNode
{
public:
T data;
BinaryTreeNode* left;
BinaryTreeNode* right;

BinaryTreeNode(T data){
this->data=data;
left=NULL;
right=NULL;
}

~BinaryTreeNode(){
delete left;
delete right;
}
};

//printing binary tree
void printTree(BinaryTreeNode<int>* root)
{
if(root==NULL)
return;
cout<<root->data<<":";
if(root->left){
cout<<"L"<<root->left->data<<" ";
}
if(root->right){
cout<<"R"<<root->right->data<<" ";
}
cout<<endl;
printTree(root->left);
printTree(root->right);
}

int main()
{
BinaryTreeNode<int>* root=new BinaryTreeNode<int>(1);
BinaryTreeNode<int>* node1=new BinaryTreeNode<int>(2);
BinaryTreeNode<int>* node2=new BinaryTreeNode<int>(3);
root->left=node1;
root->right=node2;

printTree(root);
delete root;
return 0;
}

117 changes: 117 additions & 0 deletions dsa-code/binaryTree_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include <iostream>
#include <queue>
using namespace std;

template <typename T>


class BinaryTreeNode
{
public:
T data;
BinaryTreeNode* left;
BinaryTreeNode* right;

BinaryTreeNode(T data){
this->data=data;
left=NULL;
right=NULL;
}

~BinaryTreeNode(){
delete left;
delete right;
}
};

//printing binary tree
void printTree(BinaryTreeNode<int>* root)
{
if(root==NULL)
return;
cout<<root->data<<":";
if(root->left){
cout<<"L"<<root->left->data<<" ";
}
if(root->right){
cout<<"R"<<root->right->data<<" ";
}
cout<<endl;
printTree(root->left);
printTree(root->right);
}

//taking input
BinaryTreeNode<int>* takeInput(){
int rootData;
cout<<"Enter root data"<<endl;
cin>>rootData;
if(rootData==-1) return NULL;

BinaryTreeNode<int>* root=new BinaryTreeNode<int>(rootData);
BinaryTreeNode<int>* leftChild=takeInput();
BinaryTreeNode<int>* rightChild=takeInput();
root->left=leftChild;
root->right=rightChild;
return root;
}

//taking input level wise
BinaryTreeNode<int>* takeInputLevelwise(){
int rootData;
cout<<"Enter root data"<<endl;
cin>>rootData;
if(rootData==-1) return NULL;

BinaryTreeNode<int>* root = new BinaryTreeNode<int>(rootData);
queue<BinaryTreeNode<int>*> pendingNodes;
pendingNodes.push(root);

while(pendingNodes.size() != 0){
BinaryTreeNode<int>* front = pendingNodes.front();
pendingNodes.pop();

cout<<"Enter left child of "<<front->data<<endl;
int leftChildData;
cin>>leftChildData;
if(leftChildData!=-1){
BinaryTreeNode<int>* child = new BinaryTreeNode<int>(leftChildData);
front->left=child;
pendingNodes.push(child);
}

cout<<"Enter right child of "<<front->data<<endl;
int rightChildData;
cin>>rightChildData;
if(rightChildData!=-1){
BinaryTreeNode<int>* child = new BinaryTreeNode<int>(rightChildData);
front->right=child;
pendingNodes.push(child);
}
}
return root;

}

//counting nodes
int numNodes(BinaryTreeNode<int>* root){
if(root==NULL)
return 0;
return 1+numNodes(root->left)+numNodes(root->right);
};

// 1 2 3 4 5 6 7 -1 -1 -1 -1 8 9 -1 -1 -1 -1 -1 -1
int main()
{
//normal input
//BinaryTreeNode<int>* root=takeInput();

//levelwise input
BinaryTreeNode<int>* root=takeInputLevelwise();
cout<<"Total number of nodes: "<<numNodes(root)<<endl;
printTree(root);
delete root;
return 0;
}


Loading