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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
#include "BinTree.h"
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
//Node Constructor
Node::Node(int n){
this->data=n;
this->left=NULL;
this->right=NULL;
}
//Binary Tree Constructor
BinTree::BinTree(){
this->root=NULL;
}
//insertion method
void BinTree::insert(int n){
if (this->root==NULL){
Node* newNode = new Node(n); //create new node
this->root=newNode;
} else {
if(n < this->root->data){place(n,this->root->left);}
if(n > this->root->data){place(n,this->root->right);}
}
}
//recursive helper method for insertion
void BinTree::place(int n, Node*& curr){
if (curr==NULL){
Node* newNode = new Node(n);
curr = newNode;
}else{
if(n > curr->data){place(n,curr->right);}
if(n < curr->data){place(n,curr->left);}
}
}
//remove method for deleting nodes
//checks node data from parent pointer
void BinTree::remove(int n){
Node* pFound;
Node* found;
Node* tmp;
cout<<"removing ";
cout<<n<<endl;
if(this->root->data!=n){
if(n>this->root->data){
if(root->right->data==n){
pFound = this->root;
found = this->root->right;
}else{
pFound = seek(n,this->root->right);
if(n>pFound->data){
found = pFound->right;
}else{
found = pFound->left;
}
}
}else{
if(root->left->data==n){
pFound = this->root;
found = this->root->left;
}else{
pFound = seek(n,this->root->left);
if(pFound->data<n){
found = pFound->right;
}else{
found = pFound->left;
}
}
}
if((found->left==NULL)&&(found->right==NULL)){
tmp = found;
if(n>pFound->data){pFound->right=NULL;}else{pFound->left=NULL;}
delete tmp;
}else{
if((found->left==NULL)&&(found->right!=NULL)){
tmp = found;
if(n>pFound->data){pFound->right=found->right;}else{pFound->left=found->right;}
delete tmp;
}else{
if((found->right==NULL)&&(found->left!=NULL)){
tmp = found;
if(n>pFound->data){pFound->right=found->left;}else{pFound->left=found->left;}
delete tmp;
}else{
if((found->right!=NULL&&found->left!=NULL)){
found->data=rootReplace(found);
}
}
}
}
}else{
this->root->data=rootReplace(this->root);
}
}
//recursive method for locating a value in tree
//returns pointer to parent of node to be deleted
Node* BinTree::seek(int n, Node*& curr){
if(curr==NULL){
return NULL;
}else{
if(n==curr->right->data||n==curr->left->data){
return curr;
}else{
if(n>curr->data){
return seek(n, curr->right);
}else{
return seek(n, curr->left);
}
}
}
}
//helper method for replacing nodes with 2 children
int BinTree::rootReplace(Node*& tmpRoot){
Node* tmp;
srand(time(NULL));
if(((rand()%50+1)%2)==0){
tmp= pre(tmpRoot);
}else{
tmp= post(tmpRoot);
}
int keep=tmp->data;
remove(keep);
return keep;
}
//finds next smallest value in tree
Node* BinTree::pre(Node*& tmpRoot){
tmpRoot=tmpRoot->left;
while(tmpRoot->right!=NULL){
tmpRoot=tmpRoot->right;
}
return tmpRoot;
}
//finds next largest value in tree
Node* BinTree::post(Node*& tmpRoot){
tmpRoot=tmpRoot->right;
while(tmpRoot->left!=NULL){
tmpRoot=tmpRoot->left;
}
return tmpRoot;
}
//returns number equal to or greater than argument
int BinTree::next(int n){
Node* tmp;
tmp=seek(n,this->root);
if(tmp!=NULL){
if(n>tmp->data){return tmp->right->data;}else{return tmp->left->data;}
}else{
tmp=this->root;
while(tmp->data<n&&tmp!=NULL){
if(n>tmp->data){tmp=tmp->right;}else{tmp=tmp->left;}
}
if(tmp!=NULL){
return tmp->data;
}else{
return -1;
}
}
}
//recursive method to help with print()
void BinTree::printTree(Node*& curr){
if(curr!=NULL){
cout << "(";
printTree(curr->left);
cout<<curr->data;
printTree(curr->right);
cout << ")";
}
}
//method to print contents of tree
void BinTree::print(){printTree(this->root);cout<<endl;}
int main(){
BinTree bt = BinTree();
srand(time(0));
int nums[10]={5,3,8,4,6,10,7,1,2,9};
for(int i=0; i<10; i++){
cout<<nums[i];
cout<<" ";
}
cout<<endl;
for(int i=0; i<10; i++){
bt.insert(nums[i]);
}
bt.print();
bt.remove(nums[0]);
bt.print();
}
|