Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.28 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. //Header File Binary Search Tree
  6. #ifndef H_binaryTree
  7. #define H_binaryTree
  8.  
  9. //Definition of the Node
  10. template <class elemType>
  11. struct nodeType
  12. {
  13. elemType info;
  14. nodeType<elemType> *lLink;
  15. nodeType<elemType> *rLink;
  16. };
  17.  
  18. //Definition of the class
  19. template <class elemType>
  20. class binaryTreeType
  21. {
  22. public:
  23. const binaryTreeType<elemType>& operator=
  24. (const binaryTreeType<elemType>&);
  25. //Overload the assignment operator.
  26.  
  27. bool isEmpty() const;
  28. //Function to determine whether the binary tree is empty.
  29. //Postcondition: Returns true if the binary tree is empty;
  30. // otherwise, returns false.
  31.  
  32. void inorderTraversal() const;
  33. //Function to do an inorder traversal of the binary tree.
  34. //Postcondition: Nodes are printed in inorder sequence.
  35.  
  36. void preorderTraversal() const;
  37. //Function to do a preorder traversal of the binary tree.
  38. //Postcondition: Nodes are printed in preorder sequence.
  39.  
  40. void postorderTraversal() const;
  41. //Function to do a postorder traversal of the binary tree.
  42. //Postcondition: Nodes are printed in postorder sequence.
  43.  
  44. int treeHeight() const;
  45. //Function to determine the height of a binary tree.
  46. //Postcondition: Returns the height of the binary tree.
  47.  
  48. int treeNodeCount() const;
  49. //Function to determine the number of nodes in a
  50. //binary tree.
  51. //Postcondition: Returns the number of nodes in the
  52. // binary tree.
  53.  
  54. int treeLeavesCount() const;
  55. //Function to determine the number of leaves in a
  56. //binary tree.
  57. //Postcondition: Returns the number of leaves in the
  58. // binary tree.
  59.  
  60. int treeSingleParent();
  61.  
  62. void treeSwapSubtreesOfNode();
  63.  
  64. void destroyTree();
  65. //Function to destroy the binary tree.
  66. //Postcondition: Memory space occupied by each node
  67. // is deallocated.
  68. // root = nullptr;
  69.  
  70. virtual bool search(const elemType& searchItem) const = 0;
  71. //Function to determine if searchItem is in the binary
  72. //tree.
  73. //Postcondition: Returns true if searchItem is found in
  74. // the binary tree; otherwise, returns
  75. // false.
  76.  
  77. virtual void insert(const elemType& insertItem) = 0;
  78. //Function to insert insertItem in the binary tree.
  79. //Postcondition: If there is no node in the binary tree
  80. // that has the same info as insertItem, a
  81. // node with the info insertItem is created
  82. // and inserted in the binary search tree.
  83.  
  84. virtual void deleteNode(const elemType& deleteItem) = 0;
  85. //Function to delete deleteItem from the binary tree
  86. //Postcondition: If a node with the same info as
  87. // deleteItem is found, it is deleted from
  88. // the binary tree.
  89. // If the binary tree is empty or
  90. // deleteItem is not in the binary tree,
  91. // an appropriate message is printed.
  92.  
  93. binaryTreeType(const binaryTreeType<elemType>& otherTree);
  94. //Copy constructor
  95.  
  96. binaryTreeType();
  97. //Default constructor
  98.  
  99. ~binaryTreeType();
  100. //Destructor
  101.  
  102. nodeType<elemType> *root;
  103.  
  104. private:
  105. void copyTree(nodeType<elemType>* &copiedTreeRoot,
  106. nodeType<elemType>* otherTreeRoot);
  107. //Makes a copy of the binary tree to which
  108. //otherTreeRoot points.
  109. //Postcondition: The pointer copiedTreeRoot points to
  110. // the root of the copied binary tree.
  111.  
  112. void destroy(nodeType<elemType>* &p);
  113. //Function to destroy the binary tree to which p points.
  114. //Postcondition: Memory space occupied by each node, in
  115. // the binary tree to which p points, is
  116. // deallocated.
  117. // p = nullptr;
  118.  
  119. void inorder(nodeType<elemType> *p) const;
  120. //Function to do an inorder traversal of the binary
  121. //tree to which p points.
  122. //Postcondition: Nodes of the binary tree, to which p
  123. // points, are printed in inorder sequence.
  124.  
  125. void preorder(nodeType<elemType> *p) const;
  126. //Function to do a preorder traversal of the binary
  127. //tree to which p points.
  128. //Postcondition: Nodes of the binary tree, to which p
  129. // points, are printed in preorder
  130. // sequence.
  131.  
  132. void postorder(nodeType<elemType> *p) const;
  133. //Function to do a postorder traversal of the binary
  134. //tree to which p points.
  135. //Postcondition: Nodes of the binary tree, to which p
  136. // points, are printed in postorder
  137. // sequence.
  138.  
  139. int height(nodeType<elemType> *p) const;
  140. //Function to determine the height of the binary tree
  141. //to which p points.
  142. //Postcondition: Height of the binary tree to which
  143. // p points is returned.
  144.  
  145. int max(int x, int y) const;
  146. //Function to determine the larger of x and y.
  147. //Postcondition: Returns the larger of x and y.
  148.  
  149. int nodeCount(nodeType<elemType> *p) const;
  150. //Function to determine the number of nodes in
  151. //the binary tree to which p points.
  152. //Postcondition: The number of nodes in the binary
  153. // tree to which p points is returned.
  154.  
  155. int leavesCount(nodeType<elemType> *p) const;
  156. //Function to determine the number of leaves in
  157. //the binary tree to which p points
  158. //Postcondition: The number of leaves in the binary
  159. // tree to which p points is returned.
  160.  
  161. int singleParent(nodeType<elemType> *p);
  162.  
  163. void swapSubtreesOfNode(nodeType<elemType> *p);
  164. };
  165.  
  166. //Definition of member functions
  167.  
  168. template <class elemType>
  169. binaryTreeType<elemType>::binaryTreeType()
  170. {
  171. root = nullptr;
  172. }
  173.  
  174. template <class elemType>
  175. bool binaryTreeType<elemType>::isEmpty() const
  176. {
  177. return (root == nullptr);
  178. }
  179.  
  180. template <class elemType>
  181. void binaryTreeType<elemType>::inorderTraversal() const
  182. {
  183. inorder(root);
  184. }
  185.  
  186. template <class elemType>
  187. void binaryTreeType<elemType>::preorderTraversal() const
  188. {
  189. preorder(root);
  190. }
  191.  
  192. template <class elemType>
  193. void binaryTreeType<elemType>::postorderTraversal() const
  194. {
  195. postorder(root);
  196. }
  197.  
  198. template <class elemType>
  199. int binaryTreeType<elemType>::treeHeight() const
  200. {
  201. return height(root);
  202. }
  203.  
  204. template <class elemType>
  205. int binaryTreeType<elemType>::treeNodeCount() const
  206. {
  207. return nodeCount(root);
  208. }
  209.  
  210. template <class elemType>
  211. int binaryTreeType<elemType>::treeLeavesCount() const
  212. {
  213. return leavesCount(root);
  214. }
  215. template <class elemType>
  216. int binaryTreeType<elemType>::treeSingleParent()
  217. {
  218. return singleParent(root);
  219. }
  220.  
  221. template <class elemType>
  222. void binaryTreeType<elemType>::treeSwapSubtreesOfNode()
  223. {
  224. return swapSubtreesOfNode(root);
  225. }
  226.  
  227. template <class elemType>
  228. void binaryTreeType<elemType>::copyTree
  229. (nodeType<elemType>* &copiedTreeRoot,
  230. nodeType<elemType>* otherTreeRoot)
  231. {
  232. if (otherTreeRoot == nullptr)
  233. copiedTreeRoot = nullptr;
  234. else
  235. {
  236. copiedTreeRoot = new nodeType<elemType>;
  237. copiedTreeRoot->info = otherTreeRoot->info;
  238. copyTree(copiedTreeRoot->lLink, otherTreeRoot->lLink);
  239. copyTree(copiedTreeRoot->rLink, otherTreeRoot->rLink);
  240. }
  241. } //end copyTree
  242.  
  243. template <class elemType>
  244. void binaryTreeType<elemType>::inorder
  245. (nodeType<elemType> *p) const
  246. {
  247. if (p != nullptr)
  248. {
  249. inorder(p->lLink);
  250. cout << p->info << " ";
  251. inorder(p->rLink);
  252. }
  253. }
  254.  
  255. template <class elemType>
  256. void binaryTreeType<elemType>::preorder
  257. (nodeType<elemType> *p) const
  258. {
  259. if (p != nullptr)
  260. {
  261. cout << p->info << " ";
  262. preorder(p->lLink);
  263. preorder(p->rLink);
  264. }
  265. }
  266.  
  267. template <class elemType>
  268. void binaryTreeType<elemType>::postorder
  269. (nodeType<elemType> *p) const
  270. {
  271. if (p != nullptr)
  272. {
  273. postorder(p->lLink);
  274. postorder(p->rLink);
  275. cout << p->info << " ";
  276. }
  277. }
  278.  
  279. //Overload the assignment operator
  280. template <class elemType>
  281. const binaryTreeType<elemType>& binaryTreeType<elemType>::
  282. operator=(const binaryTreeType<elemType>& otherTree)
  283. {
  284. if (this != &otherTree) //avoid self-copy
  285. {
  286. if (root != nullptr) //if the binary tree is not empty,
  287. //destroy the binary tree
  288. destroy(root);
  289.  
  290. if (otherTree.root == nullptr) //otherTree is empty
  291. root = nullptr;
  292. else
  293. copyTree(root, otherTree.root);
  294. }//end else
  295.  
  296. return *this;
  297. }
  298.  
  299. template <class elemType>
  300. void binaryTreeType<elemType>::destroy(nodeType<elemType>* &p)
  301. {
  302. if (p != nullptr)
  303. {
  304. destroy(p->lLink);
  305. destroy(p->rLink);
  306. delete p;
  307. p = nullptr;
  308. }
  309. }
  310.  
  311. template <class elemType>
  312. void binaryTreeType<elemType>::destroyTree()
  313. {
  314. destroy(root);
  315. }
  316.  
  317. //copy constructor
  318. template <class elemType>
  319. binaryTreeType<elemType>::binaryTreeType
  320. (const binaryTreeType<elemType>& otherTree)
  321. {
  322. if (otherTree.root == nullptr) //otherTree is empty
  323. root = nullptr;
  324. else
  325. copyTree(root, otherTree.root);
  326. }
  327.  
  328. //Destructor
  329. template <class elemType>
  330. binaryTreeType<elemType>::~binaryTreeType()
  331. {
  332. destroy(root);
  333. }
  334.  
  335. template<class elemType>
  336. int binaryTreeType<elemType>::height
  337. (nodeType<elemType> *p) const
  338. {
  339. if (p == nullptr)
  340. return 0;
  341. else
  342. return 1 + max(height(p->lLink), height(p->rLink));
  343. }
  344.  
  345. template <class elemType>
  346. int binaryTreeType<elemType>::max(int x, int y) const
  347. {
  348. if (x >= y)
  349. return x;
  350. else
  351. return y;
  352. }
  353.  
  354.  
  355. template <class elemType>
  356. int binaryTreeType<elemType>::nodeCount(nodeType<elemType> *p) const
  357. {
  358. if (p = NULL)
  359. return 0;
  360. else
  361. return 1 + nodeCount(p->lLink) + nodeCount(p->rLink);
  362. }
  363.  
  364. template <class elemType>
  365. int binaryTreeType<elemType>::leavesCount(nodeType<elemType> *p) const
  366. {
  367.  
  368. if (p == NULL)
  369. {
  370. return 1 + leavesCount(p->lLink ) + leavesCount(p->rLink);
  371. }
  372. else
  373. {
  374. leavesCount(p->lLink);
  375. leavesCount(p->rLink);
  376. }
  377. }
  378.  
  379. template <class elemType>
  380. int binaryTreeType<elemType>::singleParent(nodeType<elemType> *p)
  381. {
  382. if (p->lLink == NULL && p->rLink != NULL)
  383. return 1 + singleParent(p->rLink) + singleParent(p->lLink);
  384.  
  385. else if (p->rLink == NULL && p->lLink != NULL)
  386. return 1 + singleParent(p->lLink) + singleParent(p->rLink);
  387.  
  388. else
  389. {
  390. singleParent(p->rLink);
  391. singleParent(p->lLink);
  392. }
  393.  
  394. }
  395.  
  396. template <class elemType>
  397. void binaryTreeType<elemType>::swapSubtreesOfNode(nodeType<elemType> *p)
  398. {
  399. nodeType<elemType> *tempt ;
  400.  
  401. while (p != NULL)
  402. {
  403. if (p->rLink != NULL && p->lLink != NULL)
  404. {
  405. tempt = p->rLink;
  406. p->rLink = p->lLink;
  407. p->lLink = tempt;
  408. }
  409. swapSubtreesOfNode(p->rLink);
  410. swapSubtreesOfNode(p->lLink);
  411. }
  412.  
  413. return ;
  414. }
  415. #endif
  416.  
  417.  
  418. #ifndef H_binarySearchTree
  419. #define H_binarySearchTree
  420.  
  421.  
  422.  
  423. template <class elemType>
  424. class bSearchTreeType: public binaryTreeType<elemType>
  425. {
  426. public:
  427.  
  428. bool search(const elemType& searchItem) const;
  429. //Function to determine if searchItem is in the binary
  430. //search tree.
  431. //Postcondition: Returns true if searchItem is found in
  432. // the binary search tree; otherwise,
  433. // returns false.
  434.  
  435. void insert(const elemType& insertItem);
  436. //Function to insert insertItem in the binary search tree.
  437. //Postcondition: If there is no node in the binary search
  438. // tree that has the same info as
  439. // insertItem, a node with the info
  440. // insertItem is created and inserted in the
  441. // binary search tree.
  442.  
  443. void deleteNode(const elemType& deleteItem);
  444. //Function to delete deleteItem from the binary search tree
  445. //Postcondition: If a node with the same info as deleteItem
  446. // is found, it is deleted from the binary
  447. // search tree.
  448. // If the binary tree is empty or deleteItem
  449. // is not in the binary tree, an appropriate
  450. // message is printed.
  451.  
  452. private:
  453. void deleteFromTree(nodeType<elemType>* &p);
  454. //Function to delete the node to which p points is
  455. //deleted from the binary search tree.
  456. //Postcondition: The node to which p points is deleted
  457. // from the binary search tree.
  458.  
  459.  
  460. };
  461.  
  462.  
  463. template <class elemType>
  464. bool bSearchTreeType<elemType>::search
  465. (const elemType& searchItem) const
  466. {
  467. nodeType<elemType> *current;
  468. bool found = false;
  469.  
  470. if (root == nullptr)
  471. cout << "Cannot search an empty tree." << endl;
  472. else
  473. {
  474. current = root;
  475.  
  476. while (current != nullptr && !found)
  477. {
  478. if (current->info == searchItem)
  479. found = true;
  480. else if (current->info > searchItem)
  481. current = current->lLink;
  482. else
  483. current = current->rLink;
  484. }//end while
  485. }//end else
  486.  
  487. return found;
  488. }//end search
  489.  
  490. template <class elemType>
  491. void bSearchTreeType<elemType>::insert
  492. (const elemType& insertItem)
  493. {
  494. nodeType<elemType> *current; //pointer to traverse the tree
  495. nodeType<elemType> *trailCurrent = nullptr; //pointer
  496. //behind current
  497. nodeType<elemType> *newNode; //pointer to create the node
  498.  
  499. newNode = new nodeType<elemType>;
  500. newNode->info = insertItem;
  501. newNode->lLink = nullptr;
  502. newNode->rLink = nullptr;
  503.  
  504. if (root == nullptr)
  505. root = newNode;
  506. else
  507. {
  508. current = root;
  509.  
  510. while (current != nullptr)
  511. {
  512. trailCurrent = current;
  513.  
  514. if (current->info == insertItem)
  515. {
  516. cout << "The item to be inserted is already ";
  517. cout << "in the tree -- duplicates are not "
  518. << "allowed." << endl;
  519. return;
  520. }
  521. else if (current->info > insertItem)
  522. current = current->lLink;
  523. else
  524. current = current->rLink;
  525. }//end while
  526.  
  527. if (trailCurrent->info > insertItem)
  528. trailCurrent->lLink = newNode;
  529. else
  530. trailCurrent->rLink = newNode;
  531. }
  532. }//end insert
  533.  
  534. template <class elemType>
  535. void bSearchTreeType<elemType>::deleteNode
  536. (const elemType& deleteItem)
  537. {
  538. nodeType<elemType> *current; //pointer to traverse the tree
  539. nodeType<elemType> *trailCurrent; //pointer behind current
  540. bool found = false;
  541.  
  542. if (root == nullptr)
  543. cout << "Cannot delete from an empty tree."
  544. << endl;
  545. else
  546. {
  547. current = root;
  548. trailCurrent = root;
  549.  
  550. while (current != nullptr && !found)
  551. {
  552. if (current->info == deleteItem)
  553. found = true;
  554. else
  555. {
  556. trailCurrent = current;
  557.  
  558. if (current->info > deleteItem)
  559. current = current->lLink;
  560. else
  561. current = current->rLink;
  562. }
  563. }//end while
  564.  
  565. if (current == nullptr)
  566. cout << "The item to be deleted is not in the tree."
  567. << endl;
  568. else if (found)
  569. {
  570. if (current == root)
  571. deleteFromTree(root);
  572. else if (trailCurrent->info > deleteItem)
  573. deleteFromTree(trailCurrent->lLink);
  574. else
  575. deleteFromTree(trailCurrent->rLink);
  576. }
  577. else
  578. cout << "The item to be deleted is not in the tree."
  579. << endl;
  580. }
  581. } //end deleteNode
  582.  
  583. template <class elemType>
  584. void bSearchTreeType<elemType>::deleteFromTree
  585. (nodeType<elemType>* &p)
  586. {
  587. nodeType<elemType> *current; //pointer to traverse the tree
  588. nodeType<elemType> *trailCurrent; //pointer behind current
  589. nodeType<elemType> *temp; //pointer to delete the node
  590.  
  591. if (p == nullptr)
  592. cout << "Error: The node to be deleted does not exist."
  593. << endl;
  594. else if (p->lLink == nullptr && p->rLink == nullptr)
  595. {
  596. temp = p;
  597. p = nullptr;
  598. delete temp;
  599. }
  600. else if (p->lLink == nullptr)
  601. {
  602. temp = p;
  603. p = temp->rLink;
  604. delete temp;
  605. }
  606. else if (p->rLink == nullptr)
  607. {
  608. temp = p;
  609. p = temp->lLink;
  610. delete temp;
  611. }
  612. else
  613. {
  614. current = p->lLink;
  615. trailCurrent = nullptr;
  616.  
  617. while (current->rLink != nullptr)
  618. {
  619. trailCurrent = current;
  620. current = current->rLink;
  621. }//end while
  622.  
  623. p->info = current->info;
  624.  
  625. if (trailCurrent == nullptr) //current did not move;
  626. //current == p->lLink; adjust p
  627. p->lLink = current->lLink;
  628. else
  629. trailCurrent->rLink = current->lLink;
  630.  
  631. delete current;
  632. }//end else
  633. } //end deleteFromTree
  634.  
  635. #endif
  636.  
  637.  
  638. int main()
  639. {
  640. bSearchTreeType<int> numbers;
  641. int num;
  642. int index;
  643.  
  644.  
  645. cout<<"Enter positive numbers "<<endl;
  646.  
  647.  
  648. //Enter a negative number to quit the loop
  649. while (index>=0)
  650. {
  651. cin>> num;
  652. numbers.insert(num);
  653. index++;
  654. }
  655.  
  656. numbers.preorderTraversal();
  657.  
  658. numbers.treeNodeCount();
  659. numbers.treeLeavesCount();
  660. numbers.treeSingleParent();
  661. numbers.treeSwapSubtreesOfNode();
  662. numbers.preorderTraversal();
  663. return 0;
  664. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement