Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include<iostream>
#include "Task4.h"
#include<string>
#include<vector>
// Reference
// https://ansohxxn.github.io/algorithm/bst/ for BST delete function
// https://embeddedartistry.com/blog/2017/06/23/c-range-based-for-loops/ for for(auto e : v)
// https://www.geeksforgeeks.org/binary-search-tree-set-1-search-and-insertion/ for making BST
// https://www.geeksforgeeks.org/passing-vector-function-cpp/ using vector function
// https://www.geeksforgeeks.org/binary-tree-set-1-introduction/ For making Binary Tree
// https://zeddios.tistory.com/492 BST deleting // I am sorry for article which wrote down in Korean.
// https://www.tutorialspoint.com/data_structures_algorithms/selection_sort_algorithm.htm // For sequential sort
int main()
{
mytree::BinaryTree tree;
mytree::Student mStudent1{ 8, "Leo", 19990402, "CV12AH", 8940799, 20210402, "UnGradue" };
mytree::Student mStudent2{ 3, "Jack", 20000402, "CV12AH", 1111111, 20210402, "Gradueted" };
mytree::Student mStudent3{ 10, "CuVee", 20010402, "CV12AH", 8940799, 20210402, "Gradueted" };
mytree::Student mStudent4{ 1, "Baker", 20020402, "CV12AH", 1111111, 20210402, "UnGradue" };
mytree::Student mStudent5{ 6, "Aeddy", 20030402, "CV12AH", 8940799, 20210402, "UnGradue" };
auto pRoot = tree.InsertBST(nullptr, mStudent1 );
tree.InsertBST(pRoot, mStudent2);
tree.InsertBST(pRoot, mStudent3);
tree.InsertBST(pRoot, mStudent4);
tree.InsertBST(pRoot, mStudent5);
auto mStudent = tree.KEY_SEARCH(pRoot, 8);
if(mStudent != nullptr)
{
mStudent->student_name = "Karsa";
mStudent->birth_date = 20100402;
}
// Find a student by his/hers unique code, and support updating of the student info if foun
//auto's type is struct mytree::student so if users find node which users want to access for node's data, users can change the student data
// because KEY_SEARCH fucntion return &node->mStudent
//Listbyclass
std::vector<std::string> v1;
tree.ListbyClass(pRoot, v1, 8940799);
std::string temp1;
for (int i = 0; i < v1.size() - 1; ++i)
{
for (int j = i + 1; j < v1.size(); ++j)
{
if (v1[i] > v1[j])
{
temp1 = v1[i];
v1[i] = v1[j];
v1[j] = temp1;
}
}
}
for (int i = 0; i < v1.size(); ++i)
{
std::cout << v1[i] << " , ";
}
std::cout << std::endl;
//Compare the string's first word, for example "Apple" and "Cat", based on ASCII code A has smallest value in Decial
// For lexicographic order, used sequential sort. sequential sort is that makes the smallest value be put the first place.
// Compare the current value with the value next values and if the next value is smaller than the current value, swap these two.
// based on this function, can make lexicographic order because, as I mentioned, A is smallest, next is B and C, D ,,,,, in ASCII
// List all students in lexicographic order of their names.
std::vector<std::string> v;
tree.AllNames(pRoot, v);
std::string temp;
for (int i = 0; i < v.size() - 1; ++i)
for (int j = i + 1; j < v.size(); ++j)
{
if (v[i] > v[j])
{
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
}
for (int i = 0; i < v.size(); ++i)
{
std::cout << v[i] << " , ";
}
std::cout << std::endl;
// List all graduated students
std::vector<std::string> v2; // put all graduated studnets in vector and print that vector.
tree.Graduated_List(pRoot, v2, "Gradueted");
for (int i = 0; i < v2.size(); ++i)
{
std::cout << v2[i] << " , ";
}
std::cout << std::endl;
// UnGradue List by Class
std::vector<std::string> v3;
tree.UnGradue_Class(pRoot, v3, "UnGradue", 1111111); //Need to find out ungraduated by Class. It means need 2 parameter and to put student in vector, vector is also need as parameter
std::string temp3;
for (int i = 0; i < v3.size() - 1; ++i)
{
for (int j = i + 1; j < v3.size(); ++j)
{
if (v3[i] > v3[j])
{
temp3 = v3[i];
v3[i] = v3[j];
v3[j] = temp3;
}
}
}
for (int i = 0; i < v3.size(); ++i)
{
std::cout << v3[i] << " , ";
}
std::cout << std::endl;
//Delete a student given by its code
tree.Remove_Key(pRoot, 1);
tree.Remove_Key(pRoot, 3);
tree.Preorder(pRoot);
//Delete all graduated students
std::vector<int> v4;
tree.Graduated_List_2(pRoot, v4, "Gradueted");
for(auto e : v4) // for auto e type is int type because vector v4 has int type of values.
{ // if use for(auto e:v), put out all e(int keyV) which is in vector v4.
tree.Remove_Key(pRoot, e);
}
std::cout << std::endl;
tree.Preorder(pRoot);
//Delete gradue
}