Showing posts with label Computer. Show all posts
Showing posts with label Computer. Show all posts

Monday, October 14, 2013

DIFFERENCE BETWEEN CORE I3, CORE I5, CORE I7

-> Core i3:
* Entry level processor.
* 2-4 Cores...
* 4 Threads
* Hyper-Threading (efficient use of processor resources)
* 3-4 MB Cache
* 32 nm Silicon (less heat and energy)
.
-> Core i5:
* Mid range processor.
* 2-4 Cores
* 4 Threads
* Turbo Mode (turn off core if not used)
* Hyper-Threading (efficient use of processor resources)
* 3-8 MB Cache
* 32-45 nm Silicon (less heat and energy)
.
-> Core i7:
* High end processor.
* 4 Cores
* 8 Threads
* Turbo Mode (turn off core if not used)
* Hyper-Threading (efficient use of processor resources)
* 4-8 MB Cache
* 32-45 nm Silicon (less heat and energy)

Monday, June 25, 2012

Program Test Koneksi dengan database

Kali ini saya mau membagi program Java.

yg mana fungsinya adalah :

1. Test Koneksi dengan database di PC anda

2. Menampilkan error di database

3. DLL

http://www.mediafire.com/?0p3xk68upaa88bq

ini Linknya.

Silahkan Di donlot.

Tuesday, June 19, 2012

Contoh program Java input/output untuk copy isi text ke text yang lain

import java.io.*;

public class Simple{
public static void main(String [] args){
try {
FileReader fileRead = new FileReader("Test.txt");//sediakan file dengan extensi .txt dan nama file Test
BufferedReader bufferRead = new BufferedReader(fileRead);//

FileWriter fileWrite = new FileWriter("test2.txt");
BufferedWriter bufferWrite = new BufferedWriter(fileWrite);
String line;

//Read 1st line
line = bufferRead.readLine();
while ( line != null ) {
//write the line out to the Output file
bufferWrite.write(line,0,line.length());
bufferWrite.newLine();
//Read next line
line = bufferRead.readLine();
}
bufferRead.close();
bufferWrite.close();
} catch( Exception e){
e.printStackTrace();
}
}
}

---------------------------

Kode diatas akan mencopy text di Test.txt ke test2.txt

Bufferedreader akan membaca file langsung satu line dari text dalam satu waktu.

method close digunakan untuk menghentikan penggunaan bufferedreader itu sendiri.

Salam-

Luthfan

-Usaha dengan Doa

Tuesday, March 20, 2012

Guna folder dan file-file dari suatu project android-programming

Buat kamu programmer yang baru “mampir” di aplikasi android. Biasanya ketika kamu membuat suatu project android application. Bakalan Nampak beberapa folder. Taukah kamu, folder apa saja itu? kali ini saya akan berbagi pengetahuan mengenai beberapa file project dan direktori-direktori sederhana dari sebuah project android. Semoga dapat membuka pemahaman kamu tentang aplikasi android ini.

saya tidak membahas apa itu android dan sebagainya. kamu bisa search di om kesayangan kita semua(baca: Google) dan bakal nemu. ato tanya sama papah wiki.

Dalam kasus ini saya memiliki suatu project bernama Andr. Dan package andr.cool;

gambar bisa dilihat disini : http://i1161.photobucket.com/albums/q519/Luthfan30/gambar1.png

AndroidManifest.xml : File yang mendeskripsikan bagaimana kemampuan aplikasi kamu dan bagaimana ketika aplikasi itu dijalankan;


project.properties : file ini dibuat otomatis ketika project dibuat. Menentukan target build buat aplikasi kamu dan pilihan sistem-sistem build lainnya;


src/andr.cool /AndrActivity.java : file pusat yang mendefinisikan titik awal dari aplikasi android kamu. Intinya kalau kamu buat suatu project. File ini akan dibuat;


gen/andr.cool/R.java : file pusat managemen penyimpanan aplikasi yang dihasilkan untuk aplikasi kamu. Sebaiknya jangan diedit;


FOLDER :


src : folder dimana semua source code buat aplikasi kamu berada;


gen (Generated Java Files) : Folder yang diperlukan. Berisi auto-generated file untuk aplikasi kamu;


res : folder yang diperlukan dimana semua aplikasi diatur. Folder penyimpanan hal-hal yang berkaitan dengan aplikasi seperti animasi, image untuk gambar, file layout, file-file XML, penyimpanan data seperti strings, dan file lainnya;


assests : folder untuk semua aset aplikasi disimpan.(aset suatu aplikasi adalah data-data aplikasi(file, direktori) ya[ng kamu nggak mau kamu masukan kedalam kategori penyimpanan aplikasi(res Folder).


res/drawable-*/icon.png : folder yang menyimpan resolusi berbeda untuk tiap gambar.(coba diklik dan lihat perbedaannya). Nama “icon” bisa jadi apa saja (ex : ikan, iken, niken, dsb);



res/layout/main.xml : file yang mengurus design dari suatu program. Ibarat kalau kamu pake netbeans. Netbeans bisa mengakomodasi design buat program java kamu. Tinggal drag tiap component dan Bububbb, jadi suatu aplikasi dengan design yang keren!;


res/values/strings.xml : penyimpan string suatu aplikasi.


*************


Mungkin itu saja dari saya kali ini. Semoga bermanfaat. Wassalam.

Thursday, July 28, 2011

Height Balance Tree with coord

//compiled with Code:Block 10.05
#include<iostream>
#include<cmath>
#include<conio.h>
#include<windows.h>
using namespace std;

struct Node{
int data;
int height;
Node *left;
Node *right;};
typedef Node* ptr;

ptr Insert(int x, ptr p);
ptr findMin(ptr root);
ptr Delete(int x, ptr p);
int GetMaxDepth(ptr root, int depth);
void GoToXY(int x,int y);
void Print(ptr root,int d,int c,int swing);
ptr RotateLeft(ptr x);
ptr RotateRight(ptr x);
int BalanceFactor(ptr x);
ptr RebalanceTree(ptr x);
template<class T>
void tempStore(T* R);

int main()
{
char j = '1';
ptr Root = 0;
int h;
char in[8];
in[0] = 'C' ;
//cout<<"1.Input\n2.Delete\n   Type answer : ";
do
{
cout<<"1.Input\n2.Delete\n  Type answer : ";
cin>>j;
if (j == '1')
{
system("cls");
while (true)
{
system("cls");
cout<<"Enter integer number, X or x for select menu: ";
cin>>in;
if (in[0] == 'x' || in[0] == 'X')
break;
h= atoi(in);
Root  = Insert(h,Root);
tempStore(Root );
getch();
}
}
else
{
if (j =='2')
{
while(true)
{
system("cls");
cout<<"Enter Value to Delete, X or x for select menu: ";
cin>>in;
if (in[0] == 'x' || in[0] == 'X')
break;
h= atoi(in);
Root  = Delete(h,Root);
tempStore(Root );
getch();
system("cls");
}
}

}
}while(j>= 'a' || j<='z' || j>='A' || j<='Z' );
}

ptr Insert (int x, ptr p)
{
ptr temp;
if (!p)
{
temp = new Node;
temp->data = x;
temp->left = temp->right = 0;
temp->height= 0;
return temp;
}
else
{
if( x < p->data)
p->left=Insert(x,p->left);
else
{
if( x > p->data)
p->right=Insert(x,p->right);
}
}
p->height = BalanceFactor(p);
if (abs(p->height) >1 )
p = RebalanceTree(p);
return p;
}

ptr findMin(ptr root)
{
if (root->left)
return findMin(root->left);
else
return root;
}

ptr Delete(int x, ptr p)
{
ptr temp;
if (!p)
return 0;
else
{
if ( x > p->data)
{
temp = Delete(x,p->right);
p->right = temp;
}
else
{
if (  x < p->data)
{
temp = Delete(x,p->left);
p->left= temp;
}
else
{
if (p->left && p->right)
{
ptr swap = findMin(p->right);
p->data = swap->data;
temp = Delete(swap->data,p->right);
p->right = temp;
}
else
{
ptr a = (p->right)?p->right:p->left;
p = 0;
delete p;
return a;

}
}
}
}
p->height = BalanceFactor(p);
if (abs(p->height) >1 )
p = RebalanceTree(p);
return p;
}

int GetMaxDepth(ptr root, int depth)
{
if (!root)
return depth -1;
else
{
int a,b;
a = GetMaxDepth(root->left,depth+1);
b = GetMaxDepth(root->right,depth +1);
return (a>b)?a:b;
}
}

void GoToXY(int x,int y)
{
COORD coord;
coord.X = x ;coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}

void Print(ptr root,int d,int c,int swing)
{
if (root)
{
if (root->left)
{
GoToXY(swing + pow(2,d-c-1),(c+1)*2);
cout<<'|';

GoToXY(swing + pow(2,d-c-1),(c+1)*2-1);
for(int i = pow(2,d-c-1);i< pow(2,d-c);i++)
cout<<'_';
}
if (root->right)
{
GoToXY(swing + pow(2,d-c-1) + pow(2,d-c),(c+1)*2);
cout<<'|';

GoToXY(swing + pow(2,d-c-1) + pow(2,d-c),(c+1)*2-1);
for(int i = pow(2,d-c-1);i<= pow(2,d-c);i++)
{
cout<<'_';GoToXY(swing + pow(2,d-c-1) + pow(2,d-c)-(i-pow(2,d-c-1)),(c+1)*2-1);
}
}
GoToXY(swing + pow(2,d-c),(c+1)*2 -1);
cout<<root->data;
Print(root->left,d,c+1,swing);
Print(root->right,d,c+1,swing + pow(2,d-c));
}
}

ptr RotateLeft(ptr x)
{
ptr temp = x->right;
x->right = x->right->left;
temp->left = x;
return temp;
}

ptr RotateRight(ptr x)
{
ptr temp = x->left;
x->left = x->left->right;
temp->right = x;
return temp;
}

int BalanceFactor(ptr x)
{
return GetMaxDepth(x->left,1) - GetMaxDepth(x->right,1);
}
ptr RebalanceTree(ptr x)
{
if (x->height == 2)
{
if (x->left->height ==1)
return RotateRight(x);
else
{
x->left = RotateLeft(x->left);
return RotateRight(x);
}
}
else
{
if (x->right->height ==-1)
return RotateLeft(x);
else
{
x->right = RotateRight(x->right);
return RotateLeft(x);
}
}
}

template<class T>
void tempStore(T* R)
{
Print(R,GetMaxDepth(R,1),0,0);
GoToXY(0,GetMaxDepth(R,1)+1);
}

//L

//Jangan cumacopy-paste doang!!pelajari!!

//Project ini dibuat semata-mata untuk pembelajaran

Height Balance Tree with coord

//compiled with Code:Block 10.05
#include<iostream>
#include<cmath>
#include<conio.h>
#include<windows.h>
using namespace std;

struct Node{
int data;
int height;
Node *left;
Node *right;};
typedef Node* ptr;

ptr Insert(int x, ptr p);
ptr findMin(ptr root);
ptr Delete(int x, ptr p);
int GetMaxDepth(ptr root, int depth);
void GoToXY(int x,int y);
void Print(ptr root,int d,int c,int swing);
ptr RotateLeft(ptr x);
ptr RotateRight(ptr x);
int BalanceFactor(ptr x);
ptr RebalanceTree(ptr x);
template<class T>
void tempStore(T* R);

int main()
{
char j = '1';
ptr Root = 0;
int h;
char in[8];
in[0] = 'C' ;
//cout<<"1.Input\n2.Delete\n   Type answer : ";
do
{
cout<<"1.Input\n2.Delete\n  Type answer : ";
cin>>j;
if (j == '1')
{
system("cls");
while (true)
{
system("cls");
cout<<"Enter integer number, X or x for select menu: ";
cin>>in;
if (in[0] == 'x' || in[0] == 'X')
break;
h= atoi(in);
Root  = Insert(h,Root);
tempStore(Root );
getch();
}
}
else
{
if (j =='2')
{
while(true)
{
system("cls");
cout<<"Enter Value to Delete, X or x for select menu: ";
cin>>in;
if (in[0] == 'x' || in[0] == 'X')
break;
h= atoi(in);
Root  = Delete(h,Root);
tempStore(Root );
getch();
system("cls");
}
}

}
}while(j>= 'a' || j<='z' || j>='A' || j<='Z' );
}

ptr Insert (int x, ptr p)
{
ptr temp;
if (!p)
{
temp = new Node;
temp->data = x;
temp->left = temp->right = 0;
temp->height= 0;
return temp;
}
else
{
if( x < p->data)
p->left=Insert(x,p->left);
else
{
if( x > p->data)
p->right=Insert(x,p->right);
}
}
p->height = BalanceFactor(p);
if (abs(p->height) >1 )
p = RebalanceTree(p);
return p;
}

ptr findMin(ptr root)
{
if (root->left)
return findMin(root->left);
else
return root;
}

ptr Delete(int x, ptr p)
{
ptr temp;
if (!p)
return 0;
else
{
if ( x > p->data)
{
temp = Delete(x,p->right);
p->right = temp;
}
else
{
if (  x < p->data)
{
temp = Delete(x,p->left);
p->left= temp;
}
else
{
if (p->left && p->right)
{
ptr swap = findMin(p->right);
p->data = swap->data;
temp = Delete(swap->data,p->right);
p->right = temp;
}
else
{
ptr a = (p->right)?p->right:p->left;
p = 0;
delete p;
return a;

}
}
}
}
p->height = BalanceFactor(p);
if (abs(p->height) >1 )
p = RebalanceTree(p);
return p;
}

int GetMaxDepth(ptr root, int depth)
{
if (!root)
return depth -1;
else
{
int a,b;
a = GetMaxDepth(root->left,depth+1);
b = GetMaxDepth(root->right,depth +1);
return (a>b)?a:b;
}
}

void GoToXY(int x,int y)
{
COORD coord;
coord.X = x ;coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}

void Print(ptr root,int d,int c,int swing)
{
if (root)
{
if (root->left)
{
GoToXY(swing + pow(2,d-c-1),(c+1)*2);
cout<<'|';

GoToXY(swing + pow(2,d-c-1),(c+1)*2-1);
for(int i = pow(2,d-c-1);i< pow(2,d-c);i++)
cout<<'_';
}
if (root->right)
{
GoToXY(swing + pow(2,d-c-1) + pow(2,d-c),(c+1)*2);
cout<<'|';

GoToXY(swing + pow(2,d-c-1) + pow(2,d-c),(c+1)*2-1);
for(int i = pow(2,d-c-1);i<= pow(2,d-c);i++)
{
cout<<'_';GoToXY(swing + pow(2,d-c-1) + pow(2,d-c)-(i-pow(2,d-c-1)),(c+1)*2-1);
}
}
GoToXY(swing + pow(2,d-c),(c+1)*2 -1);
cout<<root->data;
Print(root->left,d,c+1,swing);
Print(root->right,d,c+1,swing + pow(2,d-c));
}
}

ptr RotateLeft(ptr x)
{
ptr temp = x->right;
x->right = x->right->left;
temp->left = x;
return temp;
}

ptr RotateRight(ptr x)
{
ptr temp = x->left;
x->left = x->left->right;
temp->right = x;
return temp;
}

int BalanceFactor(ptr x)
{
return GetMaxDepth(x->left,1) - GetMaxDepth(x->right,1);
}
ptr RebalanceTree(ptr x)
{
if (x->height == 2)
{
if (x->left->height ==1)
return RotateRight(x);
else
{
x->left = RotateLeft(x->left);
return RotateRight(x);
}
}
else
{
if (x->right->height ==-1)
return RotateLeft(x);
else
{
x->right = RotateRight(x->right);
return RotateLeft(x);
}
}
}

template<class T>
void tempStore(T* R)
{
Print(R,GetMaxDepth(R,1),0,0);
GoToXY(0,GetMaxDepth(R,1)+1);
}

//L

//Jangan cumacopy-paste doang!!pelajari!!

//Project ini dibuat semata-mata untuk pembelajaran

Binary Search Tree with Coord

//Compiled with CodeBlock 10.05
#include<iostream>
#include<cmath>
#include<conio.h>
#include<windows.h>
#include<cstdlib>
using namespace std;

struct Node{
int data;
Node* left;
Node* right;};
typedef Node* ptr;

ptr Insert(int dat, ptr root);
ptr findMin(ptr root);
ptr Delete(int value, ptr root);
int GetMaxDepth(ptr root, int depth);
void GoToXY(int x,int y);
void Print(ptr root,int d,int c,int swing);
template<class T>
void tempStore(T* R);

int main()
{
char j = \'1\';
ptr Root = 0;
int h;
char in[8];
in[0] = \'C\' ;
//cout<<\"1.Input\\n2.Delete\\n   Type answer : \";
do
{
cout<<\"1.Input\\n2.Delete\\n  Type answer : \";
cin>>j;
if (j == \'1\')
{
system(\"cls\");
while (true)
{
system(\"cls\");
cout<<\"Enter integer number, X or x for select menu: \";
cin>>in;
if (in[0] == \'x\' || in[0] == \'X\')
break;
h= atoi(in);
Root  = Insert(h,Root);
tempStore(Root );
getch();
}
}
else
{
if (j ==\'2\')
{
while(true)
{
system(\"cls\");
cout<<\"Enter Value to Delete, X or x for select menu: \";
cin>>in;
if (in[0] == \'x\' || in[0] == \'X\')
break;
h= atoi(in);
Root  = Delete(h,Root);
tempStore(Root );
//               cout<<\"Successfull deletion\";
getch();
system(\"cls\");
}
}

}
}while(j>= \'a\' || j<=\'z\' || j>=\'A\' || j<=\'Z\' );
return EXIT_SUCCESS;
}

ptr Insert(int dat, ptr root)
{
ptr temp1;
ptr temp2;
ptr current;
current = new Node;
current->data = dat;
current->left = NULL;
current->right = NULL;
if(root==NULL)
{
root = current;
}
else
{
temp1 = root;
while(temp1 != NULL)
{
temp2 = temp1;
if(current->data < temp1->data)
temp1 = temp1->left;
else
if(current->data > temp1->data)
temp1 = temp1->right;
else
{
//cout<<\"Duplicate value!!\";
delete current;
break;
}
}
if(temp1==NULL)
{
if(current->data<temp2->data)
temp2->left = current;
else
temp2->right = current;
}
//     cout<<\"Insert data success\";
}
return (root);
}

ptr findMin(ptr root)
{
ptr prev = root;
ptr cur = root->right;
if (root->left)
{
prev = cur;
cur = cur->left;
}
if( prev == root )
return prev->right = cur->right;
else
prev->left = cur->right;
root->data =  cur->data;
delete cur;
}

int isLeft(ptr parent, ptr cur)
{
int ans;
if(parent->left == cur)
ans = 0;
else
ans=1;
return ans;
}

int isRight(ptr parent, ptr cur)
{
int ans;
if(parent->right==cur)
ans = 0;
else
ans =1;
return ans;
}

ptr Delete(int value,ptr root)
{
ptr temp1=NULL;
ptr temp2=NULL;
ptr current=NULL;
int val;
int ans;
temp1=root;
while(value!= temp1->data)
{
temp2=temp1;
if(value<temp1->data)
temp1=temp1->left;
else
temp1=temp1->right;
if(temp1==NULL)
break;
}

if(temp1==NULL)
{
//cout<<\"\\nThere is no value in Tree\";
}
else
{
current= temp1;
if(current->left==NULL && current->right==NULL)
{
//cout<<\"\\ndeleting data\"<<current->data;
if(temp2==NULL)
{
//cout<<\"\\nDeleting leaf node in tree\";
}
else
{
ans=isLeft(temp2,current);
if(ans==0)
temp2->left=NULL;
ans=isRight(temp2,current);
if(ans==0)
temp2->right=NULL;
}
delete current;
}
else
if(current->left==NULL || current->right==NULL)
{
//cout<<\"\\nDelete one node with one child: \"<<current->data;
if(current->left !=NULL)
{
if(temp2==0)
{
//   cout<<\"Delete root with one left subtree\";
root=current->left;
}
else
{
ans=isLeft(temp2,current);
if(ans==0)
temp2->left=current->left;
ans=isRight(temp2,current);
if(ans==0)
temp2->right=current->left;
}
}
if(current->right !=NULL)
{
if(temp2==0)
{
// cout<<\"\\nDelete root with right subtree\";
root=current->right;
}//end of if
else
{
ans=isLeft(temp2,current);

if(ans==0)
temp2->left=current->right;
ans=isRight(temp2,current);

if(ans==0)
temp2->right=current->right;
}
}
delete current;
}
else if(current->left!=NULL && current->right!=NULL)
{
//cout<<\"\\nDelete node with two children\";
//cout<<\"\\nMinimum value\";
temp1=current->right;
while(temp1->left !=NULL)
temp1=temp1->left;
//cout<<\"deleting and reconstruct\";
val=temp1->data;
Delete(val,root);
current->data=val;
}
}
return(root);
}

int GetMaxDepth(ptr root, int depth)
{
if (!root)
return depth -1;
else
{
int a,b;
a = GetMaxDepth(root->left,depth+1);
b = GetMaxDepth(root->right,depth +1);
return (a>b)?a:b;
}
}

void GoToXY(int x,int y)
{
COORD coord;
coord.X = x ;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}

void Print(ptr root,int d,int c,int swing)
{
if (root)
{
if (root->left)
{
GoToXY(swing + pow(2,d-c-1),(c+1)*2);
cout<<\'|\';

GoToXY(swing + pow(2,d-c-1),(c+1)*2-1);
for(int i = pow(2,d-c-1);i< pow(2,d-c);i++)
cout<<\'_\';
}
if (root->right)
{
GoToXY(swing + pow(2,d-c-1) + pow(2,d-c),(c+1)*2);
cout<<\'|\';

GoToXY(swing + pow(2,d-c-1) + pow(2,d-c),(c+1)*2-1);
for(int i = pow(2,d-c-1);i<= pow(2,d-c);i++)
{
cout<<\'_\';GoToXY(swing + pow(2,d-c-1) + pow(2,d-c)-(i-pow(2,d-c-1)),(c+1)*2-1);
}
}
GoToXY(swing + pow(2,d-c),(c+1)*2 -1);
cout<<root->data;
Print(root->left,d,c+1,swing);
Print(root->right,d,c+1,swing + pow(2,d-c));
}
}

template<class T>
void tempStore(T* R)
{
Print(R,GetMaxDepth(R,1),0,0);
GoToXY(0,GetMaxDepth(R,1)+1);
}

//L

//Jangan cuma copy-paste doang! Majukan negeri ini!!!

Binary Search Tree with Coord

//Compiled with CodeBlock 10.05
#include<iostream>
#include<cmath>
#include<conio.h>
#include<windows.h>
#include<cstdlib>
using namespace std;

struct Node{
int data;
Node* left;
Node* right;};
typedef Node* ptr;

ptr Insert(int dat, ptr root);
ptr findMin(ptr root);
ptr Delete(int value, ptr root);
int GetMaxDepth(ptr root, int depth);
void GoToXY(int x,int y);
void Print(ptr root,int d,int c,int swing);
template<class T>
void tempStore(T* R);

int main()
{
char j = \'1\';
ptr Root = 0;
int h;
char in[8];
in[0] = \'C\' ;
//cout<<\"1.Input\\n2.Delete\\n   Type answer : \";
do
{
cout<<\"1.Input\\n2.Delete\\n  Type answer : \";
cin>>j;
if (j == \'1\')
{
system(\"cls\");
while (true)
{
system(\"cls\");
cout<<\"Enter integer number, X or x for select menu: \";
cin>>in;
if (in[0] == \'x\' || in[0] == \'X\')
break;
h= atoi(in);
Root  = Insert(h,Root);
tempStore(Root );
getch();
}
}
else
{
if (j ==\'2\')
{
while(true)
{
system(\"cls\");
cout<<\"Enter Value to Delete, X or x for select menu: \";
cin>>in;
if (in[0] == \'x\' || in[0] == \'X\')
break;
h= atoi(in);
Root  = Delete(h,Root);
tempStore(Root );
//               cout<<\"Successfull deletion\";
getch();
system(\"cls\");
}
}

}
}while(j>= \'a\' || j<=\'z\' || j>=\'A\' || j<=\'Z\' );
return EXIT_SUCCESS;
}

ptr Insert(int dat, ptr root)
{
ptr temp1;
ptr temp2;
ptr current;
current = new Node;
current->data = dat;
current->left = NULL;
current->right = NULL;
if(root==NULL)
{
root = current;
}
else
{
temp1 = root;
while(temp1 != NULL)
{
temp2 = temp1;
if(current->data < temp1->data)
temp1 = temp1->left;
else
if(current->data > temp1->data)
temp1 = temp1->right;
else
{
//cout<<\"Duplicate value!!\";
delete current;
break;
}
}
if(temp1==NULL)
{
if(current->data<temp2->data)
temp2->left = current;
else
temp2->right = current;
}
//     cout<<\"Insert data success\";
}
return (root);
}

ptr findMin(ptr root)
{
ptr prev = root;
ptr cur = root->right;
if (root->left)
{
prev = cur;
cur = cur->left;
}
if( prev == root )
return prev->right = cur->right;
else
prev->left = cur->right;
root->data =  cur->data;
delete cur;
}

int isLeft(ptr parent, ptr cur)
{
int ans;
if(parent->left == cur)
ans = 0;
else
ans=1;
return ans;
}

int isRight(ptr parent, ptr cur)
{
int ans;
if(parent->right==cur)
ans = 0;
else
ans =1;
return ans;
}

ptr Delete(int value,ptr root)
{
ptr temp1=NULL;
ptr temp2=NULL;
ptr current=NULL;
int val;
int ans;
temp1=root;
while(value!= temp1->data)
{
temp2=temp1;
if(value<temp1->data)
temp1=temp1->left;
else
temp1=temp1->right;
if(temp1==NULL)
break;
}

if(temp1==NULL)
{
//cout<<\"\\nThere is no value in Tree\";
}
else
{
current= temp1;
if(current->left==NULL && current->right==NULL)
{
//cout<<\"\\ndeleting data\"<<current->data;
if(temp2==NULL)
{
//cout<<\"\\nDeleting leaf node in tree\";
}
else
{
ans=isLeft(temp2,current);
if(ans==0)
temp2->left=NULL;
ans=isRight(temp2,current);
if(ans==0)
temp2->right=NULL;
}
delete current;
}
else
if(current->left==NULL || current->right==NULL)
{
//cout<<\"\\nDelete one node with one child: \"<<current->data;
if(current->left !=NULL)
{
if(temp2==0)
{
//   cout<<\"Delete root with one left subtree\";
root=current->left;
}
else
{
ans=isLeft(temp2,current);
if(ans==0)
temp2->left=current->left;
ans=isRight(temp2,current);
if(ans==0)
temp2->right=current->left;
}
}
if(current->right !=NULL)
{
if(temp2==0)
{
// cout<<\"\\nDelete root with right subtree\";
root=current->right;
}//end of if
else
{
ans=isLeft(temp2,current);

if(ans==0)
temp2->left=current->right;
ans=isRight(temp2,current);

if(ans==0)
temp2->right=current->right;
}
}
delete current;
}
else if(current->left!=NULL && current->right!=NULL)
{
//cout<<\"\\nDelete node with two children\";
//cout<<\"\\nMinimum value\";
temp1=current->right;
while(temp1->left !=NULL)
temp1=temp1->left;
//cout<<\"deleting and reconstruct\";
val=temp1->data;
Delete(val,root);
current->data=val;
}
}
return(root);
}

int GetMaxDepth(ptr root, int depth)
{
if (!root)
return depth -1;
else
{
int a,b;
a = GetMaxDepth(root->left,depth+1);
b = GetMaxDepth(root->right,depth +1);
return (a>b)?a:b;
}
}

void GoToXY(int x,int y)
{
COORD coord;
coord.X = x ;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}

void Print(ptr root,int d,int c,int swing)
{
if (root)
{
if (root->left)
{
GoToXY(swing + pow(2,d-c-1),(c+1)*2);
cout<<\'|\';

GoToXY(swing + pow(2,d-c-1),(c+1)*2-1);
for(int i = pow(2,d-c-1);i< pow(2,d-c);i++)
cout<<\'_\';
}
if (root->right)
{
GoToXY(swing + pow(2,d-c-1) + pow(2,d-c),(c+1)*2);
cout<<\'|\';

GoToXY(swing + pow(2,d-c-1) + pow(2,d-c),(c+1)*2-1);
for(int i = pow(2,d-c-1);i<= pow(2,d-c);i++)
{
cout<<\'_\';GoToXY(swing + pow(2,d-c-1) + pow(2,d-c)-(i-pow(2,d-c-1)),(c+1)*2-1);
}
}
GoToXY(swing + pow(2,d-c),(c+1)*2 -1);
cout<<root->data;
Print(root->left,d,c+1,swing);
Print(root->right,d,c+1,swing + pow(2,d-c));
}
}

template<class T>
void tempStore(T* R)
{
Print(R,GetMaxDepth(R,1),0,0);
GoToXY(0,GetMaxDepth(R,1)+1);
}

//L

//Jangan cuma copy-paste doang! Majukan negeri ini!!!

Saturday, May 7, 2011

Fibonacci Array dengan C++

Dikasih tugas kampus sama dosen. Suruh bikin Fibonacci sama array. Ane baru denger. Fibonacci digabung sama array. Oke, let's see my little code:

#include <iostream>
#include <conio.h>
using namespace std;

long fibo(unsigned long n) {
if (n <= 1) {
return n;
} else {
return fibo(n-1)+fibo(n-2);
}
}

int main() {
long input;
long n1 = 0;
long n2 = 1;
cout<<"Enter Number : ";
cin >> input;

cout << "\n\nWith Iteration(C++) : \t0 1 ";

long fibonacci[input];
for (int i = 2; i < input; i++) {

fibonacci[i] = n1 + n2;
n1 =  n2;
n2 = fibonacci[ i ];
cout << fibonacci[i] << " ";
}

cout<<"\n\nWith Recursion(C++) : \t";
for( int i = 0; i < input; i++)
{
fibonacci[i] = fibo( i );
cout<<fibonacci[i]<<" ";
}
return *fibonacci;
getch();
}

disini ane make long biar lebih luas cakupan angka yang bisa ditampung sama program ini...

kalau ada pertanyaan, silahkan bertanya. Kalau mau copy juga boleh.

MencobaBelajar untuk masa depan yang lebih cemerlang. Bismillah....

Fibonacci Array dengan C++

Dikasih tugas kampus sama dosen. Suruh bikin Fibonacci sama array. Ane baru denger. Fibonacci digabung sama array. Oke, let's see my little code:

#include <iostream>
#include <conio.h>
using namespace std;

long fibo(unsigned long n) {
if (n <= 1) {
return n;
} else {
return fibo(n-1)+fibo(n-2);
}
}

int main() {
long input;
long n1 = 0;
long n2 = 1;
cout<<"Enter Number : ";
cin >> input;

cout << "\n\nWith Iteration(C++) : \t0 1 ";

long fibonacci[input];
for (int i = 2; i < input; i++) {

fibonacci[i] = n1 + n2;
n1 =  n2;
n2 = fibonacci[ i ];
cout << fibonacci[i] << " ";
}

cout<<"\n\nWith Recursion(C++) : \t";
for( int i = 0; i < input; i++)
{
fibonacci[i] = fibo( i );
cout<<fibonacci[i]<<" ";
}
return *fibonacci;
getch();
}

disini ane make long biar lebih luas cakupan angka yang bisa ditampung sama program ini...

kalau ada pertanyaan, silahkan bertanya. Kalau mau copy juga boleh.

MencobaBelajar untuk masa depan yang lebih cemerlang. Bismillah....