Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write in Java a method, swapSubtrees, that swaps all of the left and right subtr

ID: 3652328 • Letter: W

Question

Write in Java a method, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this method to the class BinaryTree and create a program to test this method. (Note: To test your algorithm, first create a binary search tree.)

Explanation / Answer

//Header File Binary Search Tree 002 003 #ifndef H_binarySearchTree 004 #define H_binarySearchTree 005 #include 006 #include 007 #include "binaryTree.h" 008 009 using namespace std; 010 011 template 012 class bSearchTreeType: public binaryTreeType 013 { 014 public: 015 bool search(const elemType& searchItem); 016 //Function to determine if searchItem is in the binary 017 //search tree. 018 //Postcondition: Returns true if searchItem is found in the 019 // binary search tree; otherwise, returns false. 020 021 void insert(const elemType& insertItem); 022 //Function to insert insertItem in the binary search tree. 023 //Postcondition: If no node in the binary search tree has 024 // the same info as insertItem, a node with the 025 // info insertItem is created and inserted in the 026 //binary search tree. 027 028 void deleteNode(const elemType& deleteItem); 029 //Function to delete deleteItem from the binary search tree 030 //Postcondition: If a node with the same info as deleteItem 031 // is found, it is deleted from the binary 032 // search tree. 033 034 035 036 private: 037 void deleteFromTree(nodeType* &p); 038 //Function to delete the node, to which p points, from the 039 //binary search tree. 040 //Postcondition: The node to which p points is deleted 041 // from the binary search tree. 042 043 044 045 }; 046 047 048 template 049 bool bSearchTreeType::search(const elemType& searchItem) 050 { 051 nodeType *current; 052 bool found = false; 053 054 if(root == NULL) 055 cerrllink; 067 else 068 current = current->rlink; 069 }//end while 070 }//end else 071 072 return found; 073 }//end search 074 075 template 076 void bSearchTreeType::insert(const elemType& insertItem) 077 { 078 nodeType *current; //pointer to traverse the tree 079 nodeType *trailCurrent; //pointer behind current 080 nodeType *newNode; //pointer to create the node 081 082 newNode = new nodeType; 083 assert(newNode != NULL); 084 newNode->info = insertItem; 085 newNode->llink = NULL; 086 newNode->rlink = NULL; 087 088 if(root == NULL) 089 root = newNode; 090 else 091 { 092 current = root; 093 094 while(current != NULL) 095 { 096 trailCurrent = current; 097 098 if(current->info == insertItem) 099 { 100 cerr insertItem) 112 trailCurrent->llink = newNode; 113 else 114 trailCurrent->rlink = newNode; 115 } 116 }//end insert 117 118 119 120 template 121 void bSearchTreeType::deleteNode(const elemType& deleteItem) 122 { 123 nodeType *current; //pointer to traverse the tree 124 nodeType *trailCurrent; //pointer behind current 125 bool found = false; 126 127 if(root == NULL) 128 cerrllink; 144 else 145 current = current->rlink; 146 } 147 }//end while 148 149 if(current == NULL) 150 coutrlink); 161 }//end if 162 } 163 }//end deleteNode 164 165 template 166 void bSearchTreeType::deleteFromTree 167 (nodeType* &p) 168 { 169 nodeType *current; //pointer to traverse 170 //the tree 171 nodeType *trailCurrent; //pointer behind current 172 nodeType *temp; //pointer to delete the node 173 174 if(p == NULL) 175 cerrrlink; 187 delete temp; 188 } 189 else if(p->rlink == NULL) 190 { 191 temp = p; 192 p = temp->llink; 193 delete temp; 194 } 195 else 196 { 197 current = p->llink; 198 trailCurrent = NULL; 199 200 while(current->rlink != NULL) 201 { 202 trailCurrent = current; 203 current = current->rlink; 204 }//end while 205 206 p->info = current->info; 207 208 if(trailCurrent == NULL) //current did not move; 209 //current == p->llink; adjust p 210 p->llink = current->llink; 211 else 212 trailCurrent->rlink = current->llink; 213 214 delete current; 215 }//end else 216 }//end deleteFromTree 217 218 219 220 #endif
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote