Selamat datang di post
saya yang ke-36. Kali ini saya akan memposting tentang Source Code Java dari
tugas kuliah saya sendiri, Source Code ini tentang Tree, Tree ini memiliki
kemampuan untuk Insert Node, dan Mengganti current node. Untuk lebih jelasnya silahkan
anda coba sendiri di komputer anda.
Untuk
Main Classnya:
/**
*
*
@author 5211100021
*/
import java.util.*;
public class TestTree{
public static void main( String[] args ){
Tree< Integer > tree = new Tree< Integer >();
int
value = 0;
Scanner sc = new Scanner (System.in);
boolean on= true;
while
(on){
System.out.println("============================================================");
System.out.println("Menu : ");
System.out.println("============================================================");
System.out.println("1. Insert Node");
System.out.println("2. Change Node");
System.out.println("3. Exit");
System.out.println("============================================================");
System.out.print("Masukkan Pilihan : ");
int pil = sc.nextInt();
switch (pil){
case 1 :{
System.out.print("Jumlah Node yang akan dimasukkan : ");
int n = sc.nextInt();
System.out.println( "Masukkan Nilai Tree : " );
for ( int i = 1; i <= n; i++ ){
if (tree.getTree()== null){
System.out.print("Nilai root = ");
}
else
System.out.print("Nilai ke-"+i+" = ");
value = sc.nextInt();
tree.insertNode( value );
}
System.out.println (
"\n\nPreorder traversal" );
tree.preorderTraversal();
// perform preorder traversal of tree
break;
}
case 2 : {
System.out.print("Massukkan nilai yang
akan diganti :");
int ganti = sc.nextInt();
System.out.print("Masukkan nilai yang baru : ");
int masuk = sc.nextInt();
tree.ChangeNode(
tree.getTree(),ganti, masuk);
System.out.println (
"\n\nPreorder traversal" );
tree.preorderTraversal();
// perform preorder traversal of tree
System.out.println("");
break;
}
case 3 : {
System.exit(0);
}
}
}
} // end
main
} // end class TreeTest
Untuk Class Treenya:
/**
*
*
@author 5211100021
*/
public class Tree< X extends Comparable<
X > >{
private TreeNode< X > root;
//
constructor initializes an empty Tree of integers
public Tree(){root = null;} // end Tree no-argument constructor
public void insertNode( int insertValue ){
if ( root == null )
root = new TreeNode< X >( insertValue ); // create root node
else
root.insert( insertValue ); // call the insert method
}
public TreeNode< X > getTree (){return root;}
public void ChangeNode(TreeNode< X > node, int change, int replace
){
if (
node != null ){
if (node.data == change)node.data = replace; // output node data
else if (node.data > change)
ChangeNode (node.leftNode, change, replace);
else
ChangeNode (node.rightNode, change, replace);
}
else
System.out.println("data tidak ada dalam tree !");;
}
public void preorderTraversal(){preorderHelper( root );}
private void preorderHelper( TreeNode< X > node ){
if (
node == null )
return;
System.out.printf( "%s ", node.data ); // output node data
preorderHelper( node.leftNode ); // traverse left subtree
preorderHelper( node.rightNode ); // traverse right subtree
}
/*
public void inorderTraversal(){ inorderHelper( root );} // end method
inorderTraversal
private void inorderHelper( TreeNode< T > node ){
if ( node == null )
return;
inorderHelper( node.leftNode ); // traverse left subtree
System.out.printf( "%s ", node.data ); // output node data
inorderHelper( node.rightNode ); // traverse left subtree
}
public void postorderTraversal(){postorderHelper( root );}
private void postorderHelper( TreeNode< T > node ){
if ( node == null )
return;
postorderHelper( node.leftNode ); // traverse left subtree
postorderHelper( node.rightNode ); // traverse left subtree
System.out.printf( "%s ", node.data ); // output node data
}*/
}
Untuk class Nodenya:
/**
*
*
@author 5211100021
*/
public class TreeNode< X extends
Comparable< X > > {
//
package access members
TreeNode< X > leftNode; // left node
int
data;
TreeNode< X > rightNode; // right node
public TreeNode(){
data = 0;
leftNode = rightNode = null; // node has no children
}
public TreeNode( int nodeData ){
data = nodeData;
leftNode = rightNode = null; // node has no children
} //
end TreeNode constructor
public int getData (){return data;}
public void setData (int data){insert(data);}
public void insert( int insertValue ){
if ( insertValue == data )
System.out.println("nilai yang anda masukkan sudah ada dalam tree,
coba masukkan nilai yang lain !");
// insert in left subtree
if ( insertValue < data )
{
// insert new TreeNode
if ( leftNode == null )
leftNode = new TreeNode< X >( insertValue );
else // continue traversing left subtree recursively
leftNode.insert( insertValue );
}
// end if
if ( insertValue > data )
{
// insert new TreeNode
if ( rightNode == null )
rightNode = new TreeNode< X >( insertValue );
else // continue traversing left subtree recursively
rightNode.insert( insertValue );
}
// end if
}
}
Terima
Kasih Semoga Membantu
Ya Ampunnn...
BalasHapusIni pelajaran apaan :O..
Keren keren!
Smoga jadi programmer handal, Mas ^^..
amin!
Hapusdan terima kasih sudah mampir!
keren bro.... punya tutorial coding AI bro?
BalasHapusshare ya... :)