before I begin this tutorial i'd just like to say this is 100% MINE i am just transferring it from my old HF account
Hello everybody and welcome to the second part of my Java 'Advanced' Tutorial.
Maze recursion
Definition: maze recursion is fairly self-explanatory. it is recursion used to solve some sort of maze:
example:
X X X
S . . X . . . X
X X . X . X . X
X X . . . X . X
X X . . E
X X X
maze recursion works by testing every possible move at every point. most of te time this involves checking up, down, left and right. depending n the constraints of the problem, however, you may have diagonals involved.
import java.util.*; import java.io.*; public class Maze { public static void main(String[] args){ String[][]MazeMap = {{"X","X","X","X","X","X"}, {"X",".","X","X","X","X"}, {"S",".",".",".",".","X"}, {"X",".","X","X",".","X"}, {"X",".","X","X",".","E"},//E = [5][4] {"X",".",".",".",".","X"}, {"X","X","X","X","X","X"}}; for(int i = 0; i < MazeMap.length;i++){ for(int j = 0; j<MazeMap[i].length;j++) System.out.print(MazeMap[i][j]); System.out.println(); } int[][] Int= {{-1,-1,-1,-1,-1,-1}, {-1,-1,-1,-1,-1,-1}, {-1,-1,-1,-1,-1,-1}, {-1,-1,-1,-1,-1,-1}, {-1,-1,-1,-1,-1,-1}, {-1,-1,-1,-1,-1,-1}, {-1,-1,-1,-1,-1,-1}}; for (int i = 0; i<MazeMap.length;i++){ for(int j = 0; j<MazeMap[i].length;j++){ if(MazeMap[i][j].equals("S")) Int[i][j]=0; //int Counter = 0; //while(Int[5][4]==-1){ //if (Int[i][j]==Counter){ //Counter++; //System.out.println("contains" + "("+i+","+j+")"); if(MazeMap[i][j].contains(".")){ if(bX(MazeMap, i, j) == false && i>0 && j>0){ //if(bX(MazeMap,i,j)==false && Int[i][j]==-1)//i - 1 //Int[i][j] = Int[i][j];//i - 1 if(bX(MazeMap,i-1,j)==false && Int[i-1][j]==-1)//i - 1 Int[i-1][j] = Int[i][j] + 1;//i - 1 if(bX(MazeMap,i+1,j)==false && Int[i+1][j]==-1)// i + 1 Int[i+1][j] = Int[i][j] + 1;//i + 1 if(bX(MazeMap,i,j-1)==false && Int[i][j-1]==-1)// j - 1 Int[i][j-1] = Int[i][j] + 1;//j - 1 if(bX(MazeMap,i,j+1)==false && Int[i][j+1]==-1)// j + 1 Int[i][j+1] = Int[i][j] + 1;//j + 1 } } //} //}//end of while loop } } for(int i = 0;i <Int.length;i++){ for(int j = 0;j < Int[i].length;j++) System.out.printf("%-3d",Int[i][j]); System.out.println(); } }//end of main public static boolean bX(String[][]MazeMap,int i, int j){ if(MazeMap[i][j].equals("X")) return true; return false; } }
Data Structures
Definition: a data structure is a object design that stores data in a specific way. examples of a structures include java.util.list(arraylists and linkedlists), java.util.set(treeset and hashset), java.util.map(treemaps and hashmaps), trees, graphs
Lists
Definition: lists are defined by java.util.list interface. a list will have any method contained in that interface. a few of these include add(object), remove(object),size(), isEmpty(). for a complete set of methods, view the java API online.
ArrayLists are the most common type of lists. they essentially serve as dynamic arrays, allowing indexing and the setting and getting of objects at a particular index.
Lined lists are less common but are faster. in a linkedList every element is linked to the one following it. linkedlists can only add/remove from the beginning or end and have no indexing
sets
Definition: any class that implements the java.util.set. sets DO NOT allow duplicated ad upon adding to a set, java checks each element using it's defined equals() method. if no equals() method has been created for a given class then object's equals method is used. to acessess the values in a set, one must use an iterator. an easy way to do so is with a for-each loop:
Treesets are sets in which all the elements are stored in a sorted tree. this means that any object type stored in the treeset must be comaparable (i.e. properly implement the java.util.comparable interface)TreeSet<String> myset = new TreeSet<string>(); //add things to set for(String s:myset){ //do something with values }
Hashsets are sets in which the elements are stored based on an instance-specific hash code. more often than not, this hash code is simply determined by object's hashcode() method.
trees
Definition: a data structure in which every element is stored in a node and each node has link to 2 child nodes.
O <--- Node
O O <--- children
O O O O <--- Leaves (LEAVES HAVE NO CHILDREN)
There are 4 main types of trees:
1) an unsorted/unorganized tree. the items in the tree have no particular sort applied and no real structure other than the basic tree structure
2) binary trees. in binary trees, each parent node's datum is greater than the datum of its left child and less than the datum of its right child. equal data can fall on either side depending on the implementation
3) min-heap every parent node datum is less than the datum of the parent node's children
4) max-heap. every parent node datum is greater than the date of the parent node's children
A Balanced Binary Tree
6
4 8
3 5 7 9
A Balanced min-heap
3
4 7
6 5 8 9
A balanced max-heap
9
8 7
6 5 4 3
[size=large]Min heap:[/size]
heeps_of_sheeps_with_peeps_that_sleeps class
import java.util.ArrayList; public class heeps_of_sheeps_with_peeps_that_sleeps { static int[]integers = {1,59,6892,6839202,-9,58}; public static void main(String[] args) { loop(); } private static void loop() { for(int i = integers.length-1; i > 0; i--){ int curIndex = i; int parentIndex = (curIndex - 1) / 2; while(integers[parentIndex]>integers[curIndex]){ swap(parentIndex,curIndex, integers); curIndex = parentIndex; parentIndex = (curIndex - 1) / 2; } } isGood good = new isGood(); good.isgood(); } private static void swap(int C, int D,int[]integers) { int temp = integers[C]; integers[C] = integers[D]; integers[D] = temp; } }
isGood class
import java.util.Arrays; public class isGood { heeps_of_sheeps_with_peeps_that_sleeps heep = new heeps_of_sheeps_with_peeps_that_sleeps(); public void isgood() { System.out.println(Arrays.toString(heep.integers)); } }
I hope you enjoyed these tutorialskeep an eye out for more tutorial and some releases by me
![]()