1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
#include<iostream>
#include<fstream>
#include<vector>
#include<iomanip>
using namespace std;
struct binaryNode{
int height;
int value;
binaryNode *left;
binaryNode *right;
};//end node
int findHeight(binaryNode* root){
if(&root == nullptr){
return 0;
}else{
int l = findHeight(root->left);
int r = findHeight(root->right);
return max(l,r) + 1;
}
};//end findHeight
void insertNode(binaryNode* head, int x){
int depth;
if(head == NULL){
head = new binaryNode;
head->height = findHeight(head);
head->value = x;
}else if(x < head->value){
insertNode(head->left, x);
}else if(x > head->value){
insertNode(head->right, x);
}else{};//end if else
};//end create tree
void rotateWithLeftChild(binaryNode* k2){
binaryNode* k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
k2->height = max(findHeight(k2->left), findHeight(k2->right)) + 1;
k1->height = max(findHeight(k1->left), k2->height) + 1;
k2 = k1;
};//end rotate with left child
void rotateWithRightChild(binaryNode* k1){
binaryNode *k2 = k1->right;
k1->right = k2->left;
k2->left = k1;
k1->height = max(findHeight(k1->left), findHeight(k1->right)) + 1;
k2->height = max(findHeight(k2->right), k1->height) + 1;
k1 = k2;
}; // end with rotate with right child
void doubleWithLeftChild(binaryNode* k3){
rotateWithRightChild(k3->left);
rotateWithLeftChild(k3);
}; //end double with left child
void doubleWithRightChild(binaryNode* k1){
rotateWithLeftChild(k1->right);
rotateWithRightChild(k1);
}
void insertAVLtree(binaryNode* head, int x){
int height;
if(head == NULL){
head = new binaryNode;
head->height = findHeight(head);
head->value = x;
}else if(x < head->value){
insertAVLtree(head->left, x);
if((findHeight(head->left) - findHeight(head->right)) == 2){
if(x < head->left->value){
rotateWithLeftChild(head);
}else{
doubleWithLeftChild(head);
}
}
}else if(head->value < x){
insertAVLtree(head->right, x);
if((findHeight(head->right) - findHeight(head->left) == 2)){
if(head->right->value < x){
rotateWithRightChild(head);
}else{
doubleWithRightChild(head);
}
}//end if height
}else{
head->height = max(findHeight(head->left), findHeight(head->right)) + 1;
}
};//end insertAVL
void printTree(binaryNode* head, int indent=0){
if(head != NULL){
if(head->left != NULL){
printTree(head->left, indent + 4);
}
if(head->right != NULL){
printTree(head->right, indent + 4);
}
if(indent){
cout << setw(indent) << " ";
}
cout << head->value << "\n";
}
};//end print tree
main(){
vector<int> fileArray;
int number;
ifstream file; //create stream
file.open("treenode.txt"); //open file
if(file.is_open()){
while(file >> number){
fileArray.push_back(number);
char comma;
file >> comma;
}
}
file.close();//close file
binaryNode* head;
binaryNode* headAVL;
for(int i = 0; i < fileArray.size(); i++){
insertNode(head, fileArray[i]);
}
for(int j = 0; j < fileArray.size(); j++){
insertAVLtree(headAVL, fileArray[j]);
}
printTree(head);
printTree(headAVL);
};//end main
|