-
Notifications
You must be signed in to change notification settings - Fork 0
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
c0cf195
commit ff7f6c3
Showing
6 changed files
with
360 additions
and
0 deletions.
There are no files selected for viewing
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,184 @@ | ||
#include "BinarySearchTree.h" | ||
|
||
using namespace std; | ||
|
||
BinarySearchTree::BinarySearchTree() | ||
{ | ||
// * set tree | ||
root = NULL; | ||
} | ||
|
||
BinarySearchTree::~BinarySearchTree() | ||
{ | ||
// * delete tree | ||
if(root != NULL) removeTree(root); | ||
} | ||
|
||
void BinarySearchTree::insert(int e) | ||
{ | ||
// * BST constrain: L < R | ||
if(root == NULL){ | ||
root = new BinaryTreeNode(e); | ||
} else { | ||
insert(root, e); | ||
} | ||
|
||
} | ||
|
||
void BinarySearchTree::remove() | ||
{ | ||
if(root != NULL){ | ||
removeTree(root); | ||
} | ||
} | ||
|
||
int BinarySearchTree::findMin() | ||
{ | ||
return findMin(root); | ||
} | ||
|
||
int BinarySearchTree::findMax() | ||
{ | ||
return findMax(root); | ||
} | ||
|
||
int BinarySearchTree::findDepth() | ||
{ | ||
return findDepth(root, 0); | ||
} | ||
|
||
int BinarySearchTree::findMin(BinaryTreeNode *p) | ||
{ | ||
return p->element; | ||
} | ||
|
||
int BinarySearchTree::findMax(BinaryTreeNode *p) | ||
{ | ||
if(p->Right != NULL) // * if Tree not NULL go to root(left/right) | ||
return findMax(p->Right); | ||
else | ||
return p->element; | ||
} | ||
|
||
int BinarySearchTree::findDepth(BinaryTreeNode *p, int d) | ||
{ | ||
int lDepth = d; | ||
int rDepth = d; | ||
|
||
// * find left depth (recursive) | ||
if(p->Left != NULL) lDepth = findDepth(p->Left, d + 1); | ||
|
||
// * find right depth (recursive) | ||
if(p->Right != NULL) rDepth = findDepth(p->Right, d + 1); | ||
|
||
// * return max(ld, rd); | ||
return (lDepth > rDepth ? lDepth : rDepth); | ||
} | ||
|
||
void BinarySearchTree::insert(BinaryTreeNode *p, int e) | ||
{ | ||
// * p not NULL | ||
// * L < n <= R | ||
if(e < p->element){ | ||
if(p->Left == NULL) p->Left = new BinaryTreeNode(e); | ||
else insert(p->Left, e); | ||
} else { | ||
if(p->Right == NULL) p->Right = new BinaryTreeNode(e); | ||
else insert(p->Right, e); | ||
} | ||
} | ||
|
||
void BinarySearchTree::removeTree(BinaryTreeNode *&p) | ||
{ | ||
// * started p->root left/right check == NULL? if not p->root left/right | ||
// * if == NULL get delete tree(p) | ||
|
||
if(p != NULL){ | ||
// * remove left subtree | ||
if(p->Left != NULL) removeTree(p->Left); | ||
// * remove right subtree | ||
if(p->Right != NULL) removeTree(p->Right); | ||
|
||
// * remove node | ||
delete p; | ||
|
||
// * set p to nothing | ||
p = NULL; // *&p (allow to self-editable) | ||
// * no error but effect to | ||
} | ||
} | ||
|
||
void BinarySearchTree::displayPreOrder(BinaryTreeNode *p) | ||
{ | ||
// * (n, L, R) | ||
// * ... | ||
|
||
cout << "("; | ||
|
||
cout << p->element; | ||
|
||
cout << ","; | ||
|
||
if(p->Left != NULL) displayPreOrder(p->Left); else cout << "-"; | ||
|
||
cout << ","; | ||
|
||
if(p->Right != NULL) displayPreOrder(p->Right); else cout << "-"; | ||
|
||
cout << ")"; | ||
} | ||
|
||
void BinarySearchTree::displayInOrder(BinaryTreeNode *p) | ||
{ | ||
// * (L, n, R) | ||
cout << "("; | ||
if(p->Left != NULL) displayInOrder(p->Left); else cout << "-"; | ||
|
||
cout << ","; | ||
|
||
cout << p->element; | ||
|
||
cout << ","; | ||
|
||
if(p->Right != NULL) displayInOrder(p->Right); else cout << "-"; | ||
|
||
cout << ")"; | ||
} | ||
|
||
void BinarySearchTree::displayPostOrder(BinaryTreeNode *p) | ||
{ | ||
// * (L, R, n) | ||
// * ... | ||
|
||
cout << "("; | ||
|
||
if(p->Left != NULL) displayPostOrder(p->Left); else cout << "-"; | ||
|
||
cout << ","; | ||
|
||
if(p->Right != NULL) displayPostOrder(p->Right); else cout << "-"; | ||
|
||
cout << ","; | ||
|
||
cout << p->element; | ||
|
||
cout << ")"; | ||
} | ||
|
||
|
||
void BinarySearchTree::display(ORDER o) | ||
{ | ||
if(root == NULL){ | ||
cout << "Tree: -"; | ||
} else { | ||
cout << "Tree: "; | ||
if(o == PRE_ORDER) | ||
displayPreOrder(root); | ||
else if(o == POST_ORDER) | ||
displayPostOrder(root); | ||
else // * defualt case | ||
displayInOrder(root); | ||
|
||
cout << endl; | ||
} | ||
} |
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,42 @@ | ||
#if !defined(_BINARYSEARCHTREE_H) | ||
#define _BINARYSEARCHTREE_H | ||
|
||
#include "../BinaryTreeNode/BinaryTreeNode.h" | ||
|
||
enum ORDER{PRE_ORDER, IN_ORDER, POST_ORDER}; | ||
|
||
class BinarySearchTree | ||
{ | ||
private: | ||
BinaryTreeNode *root; | ||
|
||
public: | ||
BinarySearchTree(); | ||
~BinarySearchTree(); | ||
|
||
void insert(int e); | ||
void remove(); // * [option] | ||
|
||
int findMin(); // * done | ||
int findMax(); // * done | ||
|
||
int findDepth(); // * done | ||
|
||
void display(ORDER o = IN_ORDER); // * done | ||
|
||
private: | ||
void insert(BinaryTreeNode *p, int e); // * done | ||
|
||
int findMin(BinaryTreeNode *p); // * done | ||
int findMax(BinaryTreeNode *p); // * done | ||
|
||
int findDepth(BinaryTreeNode *p, int d); // * done | ||
|
||
void removeTree(BinaryTreeNode *&p); // * done | ||
|
||
void displayPreOrder(BinaryTreeNode *p); // * done | ||
void displayInOrder(BinaryTreeNode *p); // * done | ||
void displayPostOrder(BinaryTreeNode *p); // * done | ||
}; | ||
|
||
#endif // _BINARYSEARCHTREE_H |
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,76 @@ | ||
/*************************************************************************************** | ||
* ID: 65037743 | ||
* NAME: Mr.Kidsadakorn Nuallaoong | ||
* DESCRIPTION: BinarySearchTree | ||
***************************************************************************************/ | ||
#include <iostream> | ||
#include "BinarySearchTree.h" | ||
|
||
using namespace std; | ||
|
||
#define NodeType IN_ORDER | ||
|
||
int main(void) | ||
{ | ||
BinarySearchTree BST; | ||
int choice,Element; | ||
|
||
do | ||
{ | ||
cout << "----------------- MENU -----------------" << endl << endl; | ||
|
||
cout << "================ DISPLAY ===============" << endl << endl; | ||
|
||
BST.display(NodeType); | ||
|
||
cout << endl << "========================================" << endl << endl; | ||
|
||
cout << "1: Insert element" << endl; | ||
cout << "2: remove tree" << endl; | ||
cout << "3: Find max" << endl; | ||
cout << "4: Find min" << endl; | ||
cout << "5: Find Depth" << endl; | ||
|
||
cout << "0: Exit!!!!" << endl; | ||
|
||
cout << endl; | ||
|
||
cout << "Enter your choice : " ; cin >> choice; | ||
|
||
switch (choice) | ||
{ | ||
case 1: // * add | ||
cout << "Enter Element : "; cin >> Element; | ||
BST.insert(Element); | ||
break; | ||
|
||
case 2: | ||
BST.remove(); | ||
break; | ||
|
||
case 3: // * find max | ||
cout << "Max : "; | ||
cout << BST.findMax() << endl; | ||
break; | ||
|
||
case 4: // * find min | ||
cout << "min : "; | ||
cout << BST.findMin() << endl; | ||
break; | ||
|
||
case 5: // * find Depth | ||
cout << "Depth : "; | ||
cout << BST.findDepth() << endl; | ||
break; | ||
|
||
case 0: | ||
cout << "Bye...." << endl << endl; | ||
cout << "-------------- Call method --------------" << endl; | ||
break; | ||
|
||
default: // * Exit | ||
cout << "[Massage] Wrong choice,try again..." << endl; | ||
} | ||
|
||
}while(choice != 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,32 @@ | ||
run: | ||
@clear | ||
@g++ -c main.cpp -o main.o | ||
@echo "\e[1;32m File 1 Success \e[0m" | ||
@g++ -c BinarySearchTree.cpp -o BinarySearchTree.o | ||
@echo "\e[1;32m File 2 Success \e[0m" | ||
@g++ -c ../BinaryTreeNode/BinaryTreeNode.cpp -o ../BinaryTreeNode/BinaryTreeNode.o | ||
@echo "\e[1;32m File 3 Success \e[0m" | ||
@g++ main.o BinarySearchTree.o ../BinaryTreeNode/BinaryTreeNode.o -o BinarySearchTree | ||
@clear | ||
@echo "\e[1;32m Build Success \e[0m" | ||
|
||
@./BinarySearchTree | ||
|
||
@rm -f *.o ../BinaryTreeNode/BinaryTreeNode.o BinarySearchTree | ||
@clear | ||
@echo "\e[1;31m Remove done \e[0m" | ||
|
||
all: main.o BinarySearchTree.o ../BinaryTreeNode/BinaryTreeNode.o | ||
g++ main.o BinarySearchTree.o ../BinaryTreeNode/BinaryTreeNode.o -o BinarySearchTree | ||
|
||
main.o: main.cpp | ||
g++ -c main.cpp -o main.o | ||
|
||
BinarySearchTree.o: BinarySearchTree.cpp | ||
g++ -c BinarySearchTree.cpp -o BinarySearchTree.o | ||
|
||
../BinaryTreeNode/BinaryTreeNode.o: ../BinaryTreeNode/BinaryTreeNode.cpp | ||
g++ -c ../BinaryTreeNode/BinaryTreeNode.cpp -o ../BinaryTreeNode/BinaryTreeNode.o | ||
|
||
clean: | ||
rm -f *.o ../BinaryTreeNode/BinaryTreeNode.o BinarySearchTree |
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,8 @@ | ||
#include "BinaryTreeNode.h" | ||
|
||
BinaryTreeNode::BinaryTreeNode(int element, BinaryTreeNode *Left, BinaryTreeNode *Right) | ||
{ | ||
this->element = element; | ||
this->Left = Left; | ||
this->Right = Right; | ||
} |
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,18 @@ | ||
#if !defined(_BINARYTREENODE_H) | ||
#define _BINARYTREENODE_H | ||
|
||
#include <iostream> | ||
|
||
class BinaryTreeNode | ||
{ | ||
public: | ||
int element; | ||
BinaryTreeNode *Left; | ||
BinaryTreeNode *Right; | ||
public: | ||
BinaryTreeNode(int element = 0, | ||
BinaryTreeNode *Left = NULL, | ||
BinaryTreeNode *Right = NULL); | ||
}; | ||
|
||
#endif // _BINARYTREENODE_H |