-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3572578
commit f60e0cd
Showing
17 changed files
with
1,787 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
data_structures/Tree/minSpanningTree/binary_search_tree.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <iostream> | ||
#include "create_tree.h" | ||
|
||
void searchbst(Node* root, int val){ | ||
if(root==nullptr){ | ||
std::cout << "Value not found" << '\n'; | ||
return; | ||
} | ||
if (root->data==val){ | ||
std::cout << "Value found at address -->" <<root<< '\n'; | ||
return; | ||
} | ||
|
||
else if(val<root->data){ | ||
searchbst(root->left_child,val); | ||
} | ||
else if(val>root->data){ | ||
searchbst(root->right_child,val); | ||
} | ||
} | ||
|
||
void display(Node* root){ | ||
if (root==nullptr){ | ||
return; | ||
} | ||
else{ | ||
if(root->right_child==nullptr && root->left_child==nullptr){ | ||
return; | ||
} | ||
if(root->right_child!=nullptr && root->left_child!=nullptr){ | ||
std::cout << root->left_child->data<<"-->" ; | ||
std::cout << root->right_child->data<<"-->" ; | ||
display(root->left_child); | ||
display(root->right_child); | ||
} | ||
else if(root->right_child==nullptr){ | ||
std::cout << root->left_child->data << '\n'; | ||
|
||
} | ||
else if(root->left_child==nullptr){ | ||
std::cout << root->right_child->data << '\n'; | ||
|
||
} | ||
|
||
std::cout<< '\n'; | ||
} | ||
} | ||
|
||
|
||
int main(){ //to run use g++ -o main binary_search_tree.cpp create_tree.cpp | ||
Node* root=new Node;//this will create the executable with name main. the default name is a.out | ||
create_tree tree; | ||
tree.add_root(&root,89); | ||
tree.add_node(root,56); | ||
tree.add_node(root,34); | ||
tree.add_node(root,78); | ||
tree.add_node(root,22); | ||
tree.add_node(root,96); | ||
tree.add_node(root,95); | ||
tree.add_node(root,104); | ||
searchbst(root,78); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include<iostream> | ||
#include "create_tree.h" | ||
|
||
|
||
void create_tree:: add_root(Node** root,int data){ | ||
Node* new_node=new Node; | ||
Node* root_temp=*root; | ||
new_node->data=data; | ||
new_node->left_child=root_temp->left_child; | ||
new_node->right_child=root_temp->right_child; | ||
*root=new_node; | ||
delete(root_temp); | ||
std::cout << "root added of value-->" <<new_node->data<<"--> on address "<<new_node<< '\n'; | ||
} | ||
|
||
void create_tree:: add_node(Node* root, int data){ | ||
Node* new_node=new Node; | ||
new_node->data=data; | ||
while(1){ | ||
if (data>root->data){ | ||
if (root->right_child==nullptr){ | ||
root->right_child=new_node; | ||
break; | ||
} | ||
else{ | ||
root=root->right_child; | ||
} | ||
} | ||
else{ | ||
if (root->left_child==nullptr){ | ||
root->left_child=new_node; | ||
break; | ||
} | ||
else{ | ||
root=root->left_child; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
class character { | ||
|
||
color c; | ||
int lives; | ||
int xpos; | ||
int ypos; | ||
int xperm; | ||
int yperm; | ||
int arrx; | ||
int arry; | ||
|
||
character() { | ||
lives = 3; | ||
xpos = 40; | ||
ypos = 40; | ||
xperm = 40; | ||
yperm = 40; | ||
arrx = 2; | ||
arry = 2; | ||
c = color(256, 0, 256); // Uses RGB Input for a new color | ||
} | ||
|
||
void printChar() { | ||
noStroke(); | ||
fill(c); | ||
ellipse(xpos, ypos, 10, 10); | ||
} | ||
|
||
void up() { | ||
ypos -= 16; | ||
arry -= 1; | ||
} | ||
|
||
void down() { | ||
ypos += 16; | ||
arry += 1; | ||
} | ||
|
||
void left() { | ||
xpos -= 16; | ||
arrx -= 1; | ||
} | ||
|
||
void right() { | ||
xpos += 16; | ||
arrx += 1; | ||
} | ||
|
||
void validDirection(int i, cell[][] maze) { | ||
if (i == 0 && maze[arry-1][arrx].getColor() != color(0,0,0) ) { | ||
//System.out.println("up"); | ||
up(); | ||
} | ||
|
||
else if (i == 1 && maze[arry+1][arrx].getColor() != color(0,0,0) ) { | ||
//System.out.println("down"); | ||
down(); | ||
} | ||
|
||
if (i == 2 && maze[arry][arrx-1].getColor() != color(0,0,0) ) { | ||
//System.out.println("left"); | ||
left(); | ||
} | ||
|
||
if (i == 3 && maze[arry][arrx+1].getColor() != color(0,0,0) ) { | ||
//System.out.println("right"); | ||
right(); | ||
} | ||
//System.out.println(arry + "," + arrx); | ||
} | ||
|
||
int getArrX() { | ||
return arrx; | ||
} | ||
|
||
int getArrY() { | ||
return arry; | ||
} | ||
|
||
int getX() { | ||
return xpos; | ||
} | ||
|
||
int getY() { | ||
return ypos; | ||
} | ||
|
||
int getLives() { | ||
return lives; | ||
} | ||
|
||
void reset() { | ||
xpos = xperm; | ||
ypos = yperm; | ||
arrx = 2; | ||
arry = 2; | ||
} | ||
|
||
void die() { | ||
lives -= 1; | ||
reset(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
interface GenMaze { | ||
|
||
void generate(); | ||
void displayMaze(); | ||
boolean generated(); | ||
void makeExit(); | ||
cell[][] getMaze(); | ||
cell getExit(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
import java.util.Stack; | ||
|
||
class MazeDepth implements GenMaze { | ||
|
||
cell[][] maze; //holds our maze | ||
Stack<cell> path; //holds the current path we have taken | ||
cell current; //current cell we are on in maze | ||
character dude; | ||
int x; //current xcor within array [y][x] | ||
int y; //current ycor within array [y][x] | ||
int newX; //used for transitions with midX | ||
int newY; //used for transitions with midY | ||
int midX; //used for walls in between cells | ||
int midY; //used for walls in between cells | ||
int numRow; | ||
int numCol; | ||
cell exit; | ||
|
||
|
||
MazeDepth(int w, int h) { | ||
//start at upperleft hand corner | ||
//init vars | ||
numRow = h / 16; | ||
numCol = w / 16; | ||
maze = new cell[numRow][numCol]; | ||
for ( int i = 0; i < numRow; i++ ) { | ||
for ( int j = 0; j < numCol; j++ ) { | ||
maze[i][j] = new wall(j*16, i*16); //generates matrix of walls | ||
} | ||
} | ||
for ( int i = 2; i < numRow-1; i += 2 ) { | ||
for ( int j = 2; j < numCol-1; j += 2 ) { | ||
maze[i][j] = new cell(j*16, i*16); | ||
//creates cells 2 spaces apart with a 2 cell | ||
//buffer zone around the edges, helps with out of bounds | ||
//exceptions | ||
} | ||
} | ||
//start at upperleft hand corner | ||
//init vars | ||
x = 2; | ||
y = 2; | ||
current = maze[2][2]; | ||
path = new Stack(); | ||
current.visit(); | ||
dude = new character(); | ||
} | ||
|
||
void generate() { | ||
//delay(100); //stops .1 second each frame for better visualization | ||
|
||
//if current cell has neighbors | ||
if ( hasNeighbors() ) { | ||
//choose a path | ||
getNext(); | ||
} | ||
|
||
//otherwise if we can backtrack | ||
else if ( !path.isEmpty() ) { | ||
//get last placement and go through draw again | ||
current.backTrack(); //shows backtracking process | ||
|
||
current = path.pop(); | ||
|
||
newX = current.getX() / 16; | ||
newY = current.getY() / 16; | ||
|
||
midX = (newX + x)/2; | ||
midY = (newY + y)/2; | ||
|
||
maze[midY][midX].backTrack(); | ||
|
||
x = newX; | ||
y = newY; | ||
|
||
//System.out.println("current popped"); | ||
//System.out.println(x); | ||
//System.out.println(y); | ||
|
||
current.backTrack(); | ||
|
||
} | ||
} | ||
|
||
|
||
|
||
|
||
//returns if a cell in the array has unvisited neighbors | ||
//to travel to | ||
boolean hasNeighbors() { | ||
return maze[y+2][x].unvisited() || maze[y-2][x].unvisited() || maze[y][x-2].unvisited() || maze[y][x+2].unvisited(); | ||
} | ||
|
||
//updates current and moves to next cell | ||
//as well as removes wall by converting it to | ||
//a visited cell | ||
void getNext() { | ||
double rand; | ||
cell ret; | ||
path.push(current);//add our last location to the stack | ||
//System.out.println("current pushed"); | ||
ret = current;//give ret an init value that passes first case | ||
|
||
while ( !ret.unvisited() ) { | ||
//while our new current has been visited | ||
//look for another | ||
rand = Math.random(); | ||
|
||
if ( rand < .25 ) { | ||
ret = maze[y+2][x]; | ||
} | ||
else if ( rand < 0.5 ) { | ||
ret = maze[y-2][x]; | ||
} | ||
else if ( rand < 0.75 ) { | ||
ret = maze[y][x-2]; | ||
} | ||
else { | ||
ret = maze[y][x+2]; | ||
} | ||
} | ||
|
||
newX = ret.getX() / 16;//new array xcor | ||
newY = ret.getY() / 16;//new array ycor | ||
|
||
//System.out.println(newX); | ||
//System.out.println(newY); | ||
|
||
midX = (newX+x)/2;//wall array xcor | ||
midY = (newY+y)/2;//wall array ycor | ||
|
||
//System.out.println(midX); | ||
//System.out.println(midY); | ||
|
||
maze[midY][midX] = (cell) maze[midY][midX];//typecast wall to cell | ||
maze[midY][midX].visit();//turn wall to visited cell | ||
|
||
//update vars | ||
x = newX; | ||
y = newY; | ||
current = ret; | ||
current.visit(); | ||
} | ||
|
||
cell[][] getMaze() { | ||
return maze; | ||
} | ||
|
||
void displayMaze() { | ||
for ( int i = 0; i < maze.length; i++ ) { | ||
for ( int j = 0; j < maze[0].length; j++ ) { | ||
maze[i][j].displayCell(); | ||
} | ||
} | ||
} | ||
|
||
boolean generated() { | ||
return maze[2][2].getColor() == color(0,256,0); | ||
} | ||
|
||
void makeExit() { | ||
boolean bool = true; | ||
while (bool) { | ||
int rand1 = (int) (random(maze.length/4) + 3*maze.length/4); | ||
int rand2 = (int) (random(maze[0].length/4) + 3*maze[0].length/4); | ||
if (!(maze[rand1][rand2] instanceof wall)) { | ||
maze[rand1][rand2].setColor(color(0,256,256)); | ||
bool = false; | ||
exit = maze[rand1][rand2]; | ||
} | ||
} | ||
} | ||
cell getExit() { | ||
return exit; | ||
} | ||
|
||
} |
Oops, something went wrong.