cover photo
profile picture

mazen-mohamed

Mazen Mohamed

Male

Egypt
ad-Daqahliyah
Aja

Binary Search Tree (BTS) in Java and some of its common methods

Binary Search Tree

Tags: data, binarysearchtree, java, bts, programming, datastructure, structure

Values of keys in the left sub-tree of the root should be smaller than or equal to the values of the key at the root and the keys in the right sub-tree of the root should be larger than or equal to the values of the key at the root. The left and right subtree each must also be a binary search tree.

What is JSON and what are the advantages of it

JSON

Tags: programming, json, developer

JSON is a language-independent data format. It was derived from JavaScript, it stands for JavaScript Object Notation and it is often used when data is sent from a server to a web page

LinkedList in Java and some of its common methods

Linked List

Tags: data, java, programming, linkedlist, structure, data-structure

a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses. Each element is known as a node. Due to the dynamicity and ease of insertions and deletions, they are preferred over the arrays. It also has a few disadvantages like the nodes cannot be accessed directly instead we need to start from the head and follow through the link to reach a node we wish to access.

```java /* * * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javaapplication17; /** * * @author FUE */ public class Node { int data; Node next ; public Node(int data) { this.data=data; next=null; } } ``` ```java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javaapplication17; /** * * @author FUE */ public class linkedlist { Node head ; int elementscount; public linkedlist() { head=null; elementscount=0; } boolean Isempty() { return (elementscount==0); } void insertFirst(int data) { Node mynode = new Node(data); // mynode. if(Isempty()) { head=mynode; } else { mynode.next=head; head=mynode; } elementscount++; } void insertLast(int data) { Node mynode = new Node(data); if(Isempty()) { head=mynode; } else { Node temp =head; while(temp.next!=null) { temp=temp.next; } temp.next=mynode; } elementscount++; } int removeFirst() { int val ; if(head.next==null) { val=head.data; head=null; } else { val=head.data; head=head.next; } elementscount--; return val; } int removeLast() { int val ; if(head.next==null) { val=head.data; head=null; } else { Node temp=head; while (temp.next.next!=null) { temp =temp.next; } val=temp.next.data; temp.next=null; } elementscount--; return val; } int Count() { return elementscount; } void printLinkedlist() { Node temp=head; while (temp!=null) { System.out.println(temp.data); temp=temp.next; } } void insertAt (int index , int data) { Node mynode = new Node(data); if(index==0) { if(Isempty()) { head=mynode; } else { mynode.next=head; head=mynode; } } else { Node temp=head; for(int i=0; i < index-1;i++) { temp=temp.next; } mynode.next=temp.next; temp.next=mynode; } elementscount++; } int deleteAt (int index) { int val ; if(index==0) { if(head.next==null) { val=head.data; head=null; } else { val=head.data; head=head.next; } } else { Node temp=head; //Node tobedeleted; for(int i=0; i < index-1;i++) { temp=temp.next; } val=temp.next.data; temp.next=temp.next.next; } elementscount--; return val; } } ``` ```java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javaapplication17; /** * * @author FUE */ public class MyStack { int arr[]; int numberofelements; int stacktop; int maxsize; public MyStack (int maxsize) { this.maxsize=maxsize; arr= new int[maxsize]; stacktop=-1; numberofelements=0; } boolean isfull() { return numberofelements==maxsize; } void push(int element) { if(!isfull()) { stacktop++; arr[stacktop]=element; numberofelements++; } else { System.out.println("stack is full"); } } boolean isempty() { return stacktop==-1; } void pop () { if(!isempty()) { int poppednumber=arr[stacktop]; stacktop--; System.out.println("The number you popped is" + poppednumber); } else { System.out.println("Stack is empty !!!"); } } void print () { for(int i=stacktop; i>0 ; i--) { System.out.println(arr[i]); } } } ```

```java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javaapplication17; /** * * @author FUE */ public class JavaApplication17 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here } public static class Node { int data; Node next ; public Node(int data) { this.data=data; next=null; } } public static class linkedlist { Node head ; int elementscount; public linkedlist() { head=null; elementscount=0; } boolean Isempty() { return (elementscount==0); } void insertFirst(int data) { Node mynode = new Node(data); // mynode. if(Isempty()) { head=mynode; } else { mynode.next=head; head=mynode; } elementscount++; } void insertLast(int data) { Node mynode = new Node(data); if(Isempty()) { head=mynode; } else { Node temp =head; while(temp.next!=null) { temp=temp.next; } temp.next=mynode; } elementscount++; } int removeFirst() { int val ; if(head.next==null) { val=head.data; head=null; } else { val=head.data; head=head.next; } elementscount--; return val; } int removeLast() { int val ; if(head.next==null) { val=head.data; head=null; } else { Node temp=head; while (temp.next.next!=null) { temp =temp.next; } val=temp.next.data; temp.next=null; } elementscount--; return val; } int Count() { return elementscount; } void printLinkedlist() { Node temp=head; while (temp!=null) { System.out.println(temp.data); temp=temp.next; } } void insertAt (int index , int data) { Node mynode = new Node(data); if(index==0) { if(Isempty()) { head=mynode; } else { mynode.next=head; head=mynode; } } else { Node temp=head; for(int i=0; i < index-1;i++) { temp=temp.next; } mynode.next=temp.next; temp.next=mynode; } elementscount++; } int deleteAt (int index) { int val ; if(index==0) { if(head.next==null) { val=head.data; head=null; } else { val=head.data; head=head.next; } } else { Node temp=head; //Node tobedeleted; for(int i=0; i < index-1;i++) { temp=temp.next; } val=temp.next.data; temp.next=temp.next.next; } elementscount--; return val; } } } ```

Test ```cpp #include <windows.h> #include <gl/gl.h> #include <gl/glut.h> #include <ctime> #include <cstdio> #include<synchapi.h> using namespace std; #pragma comment(lib, "opengl32.lib") #pragma comment(lib, "glu32.lib") #pragma comment(lib, "glut32.lib") /*** Global Variables ***/ float fXpos = 0, fYpos = 0, fRot = 0, fScale = 1; /** Creates the main window, registers event handlers, and initializes OpenGL stuff. */ void InitGraphics(int argc, char* argv[]); void OnKeyPress(unsigned char key, int x, int y); void OnSpecialKeyPress(int key, int x, int y); /** Sets the logical coordinate system we will use to specify our drawings. */ void SetTransformations() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-100, 100, -100, 100); } void Draw_Cordinates() { glLineWidth(5); glBegin(GL_LINES); glColor3f(0, 1, 0); // X-axis glVertex3f(-100, 0, 0); glVertex3f(100, 0, 0); //Y-axis glVertex3f(0, -100, 0); glVertex3f(0, 100, 0); glEnd(); } void OnDisplay() { //set the background color to white glClearColor(1, 1, 1, 1); glClear(GL_COLOR_BUFFER_BIT); SetTransformations(); Draw_Cordinates(); glTranslatef(fXpos, fYpos, 0); glRotatef(fRot, 0, 0, 1); glScalef(fScale, fScale, 0); glBegin(GL_QUADS); glColor3f(1, 0, 0); glVertex2f(-50, -50); glVertex2f(50, -50); glVertex2f(50, 50); glVertex2f(-50, 50); glEnd(); glFlush(); glutSwapBuffers(); glutPostRedisplay(); } /** Creates the main window, registers event handlers, and initializes OpenGL stuff. */ void InitGraphics(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutInitWindowPosition(550, 100); //pass (-1, -1) for Window-Manager defaults glutInitWindowSize(800, 600); glutCreateWindow("OpenGL Lab"); glutDisplayFunc(OnDisplay); glutIdleFunc(OnDisplay); glutKeyboardFunc(OnKeyPress); glutSpecialFunc(OnSpecialKeyPress); glutMainLoop(); } int main(int argc, char* argv[]) { InitGraphics(argc, argv); return 0; } void OnKeyPress(unsigned char key, int x, int y) { if (key == 27) { exit(0); } switch (key) { case 'a': case 'A': { fXpos -= 2; break; } case 'd': case 'D': { fXpos += 2; break; } case 'w': case 'W': { fYpos += 2; break; } case 's': case 'S': { fYpos -= 2; break; } case 'e': case 'E': { fRot -= 0.9; break; } case 'q': case 'Q': { fRot += 0.9; break; } default: break; } }; void OnSpecialKeyPress(int key, int x, int y) {}; ```

```cpp #include <windows.h> #include <gl/gl.h> #include <gl/glut.h> #include <ctime> #include <cstdio> #include<synchapi.h> using namespace std; #pragma comment(lib, "opengl32.lib") #pragma comment(lib, "glu32.lib") #pragma comment(lib, "glut32.lib") /*** Global Variables ***/ float fXpos = 0, fYpos = 0, fRot = 0, fScale = 1; /** Creates the main window, registers event handlers, and initializes OpenGL stuff. */ void InitGraphics(int argc, char* argv[]); void OnKeyPress(unsigned char key, int x, int y); void OnSpecialKeyPress(int key, int x, int y); /** Sets the logical coordinate system we will use to specify our drawings. */ void SetTransformations() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-100, 100, -100, 100); } void Draw_Cordinates() { glLineWidth(5); glBegin(GL_LINES); glColor3f(0, 1, 0); // X-axis glVertex3f(-100, 0, 0); glVertex3f(100, 0, 0); //Y-axis glVertex3f(0, -100, 0); glVertex3f(0, 100, 0); glEnd(); } void OnDisplay() { //set the background color to white glClearColor(1, 1, 1, 1); glClear(GL_COLOR_BUFFER_BIT); SetTransformations(); Draw_Cordinates(); // glPushMatrix(); glTranslatef(fXpos, fYpos, 0); glRotatef(fRot, 0, 0, 1); glScalef(fScale, fScale, 0); glBegin(GL_QUADS); glColor3f(1, 0, 0); glVertex2f(-50, -50); glVertex2f(50, -50); glVertex2f(50, 50); glVertex2f(-50, 50); glEnd(); // glPopMatrix(); glFlush(); glutSwapBuffers(); glutPostRedisplay(); } /** Creates the main window, registers event handlers, and initializes OpenGL stuff. */ void InitGraphics(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutInitWindowPosition(550, 100); //pass (-1, -1) for Window-Manager defaults glutInitWindowSize(800, 600); glutCreateWindow("OpenGL Lab"); glutDisplayFunc(OnDisplay); glutIdleFunc(OnDisplay); glutKeyboardFunc(OnKeyPress); glutSpecialFunc(OnSpecialKeyPress); glutMainLoop(); } int main(int argc, char* argv[]) { InitGraphics(argc, argv); return 0; } void OnKeyPress(unsigned char key, int x, int y) { if (key == 27) { exit(0); } switch (key) { case 'a': case 'A': { fXpos -= 2; break; } case 'd': case 'D': { fXpos += 2; break; } case 'w': case 'W': { fYpos += 2; break; } case 's': case 'S': { fYpos -= 2; break; } case 'e': case 'E': { fRot -= 0.9; break; } case 'q': case 'Q': { fRot += 0.9; break; } default: break; } }; void OnSpecialKeyPress(int key, int x, int y) { switch (key) { case GLUT_KEY_LEFT: { fXpos -= 2; break; } case GLUT_KEY_RIGHT: { fXpos += 2; break; } case GLUT_KEY_UP: { fYpos += 2; break; } case GLUT_KEY_DOWN: { fYpos -= 2; break; } default: break; } }; ```

Queue in Java and some of its common methods

Queue

Tags: data, java, programming, queue, structure, data-structure

Queue in Java and implement some of its common methods A Queue is a linear structure that follows a particular order in which the operations are performed. The order is First In First Out (FIFO). A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first. Here we will implement the queue in Java with some of its common methods like (enqueue, dequeue, peak, and print).

Bio:

A web developer with over 2 years of experience in Next.js, Node.js, React.js, JavaScript, CSS, and HTML!