Use a binary linked list to implement a binary search tree (2)

Source: Internet
Author: User

 
/* Implement the linked list of the Binary Search Tree: delete a node through three traversal methods; find a node; author: no double Date in the world: 2014-5-28Version: 3.0 */# include
 
  
# Include
  
   
Typedef int T; // the data Type of the node in the tree: using namespace std; class BiTree {private: struct BiNode {T data; BiNode * lchild, * rchild; BiNode (T d) {data = d; lchild = nullptr; rchild = nullptr ;}}; BiNode * root; public: BiTree () {// root = root-> rchild = nullptr; root = nullptr ;}~ BiTree () {}// use recursion to create a binary tree // create a binary tree rule // reference the pointer (recommended) bool addBiNode (BiNode * & nodeRoot, T d) {if (nodeRoot = nullptr) {BiNode * p = new BiNode (d); nodeRoot = p; cout <
   
    
Data <"insert success! "<
    
     
Data) {// recursive addBiNode (nodeRoot-> lchild, d);} else if (nodeRoot! = Nullptr & d> (nodeRoot)-> data) {// recursive addBiNode (nodeRoot-> rchild, d) to the right subtree );} else {cout <"the data already exists in the tree" <
     
      
Data = d) {return true;} else if (nodeRoot-> data
      
        Rchild, d);} elsereturn Search (nodeRoot-> lchild, d);} // recursive Search determines the location of the node bool DeleteBST (BiNode * & nodeRoot, T d) {if (nullptr = nodeRoot) // when the node is empty, return false; else {if (nodeRoot-> data = d) {// return Delete (nodeRoot ); // Delete (nodeRoot);} else if (nodeRoot-> data
       
         Rchild, d); // DeleteBST (nodeRoot-> rchild, d);} elsereturn DeleteBST (nodeRoot-> lchild, d); // DeleteBST (nodeRoot-> lchild, d) ;}} protected: // delete operation // Delete the corresponding node // If the node is a leaf node, the left and right children of the node are empty, you only need to point the parent node to this node. // leave the pointer blank. // if there are only left children or only right children, move the corresponding node up // nodeRoot as the node to be deleted // if there are both left and right children, see the following comments bool Delete (BiNode * & nodeRoot) {// if the right subtree of the node to be deleted is empty, move the left subtree if (nullptr = nodeRoot-> rchild) {BiNode * temp = nodeRoot; nodeRoot = nodeRoot-> lchild; delete temp; return True;} else if (nullptr = nodeRoot-> lchild) {// if the left subtree is empty, move the right subtree BiNode * temp = nodeRoot; nodeRoot = nodeRoot-> rchild; delete temp; return true ;} else {// left and right subtree are not empty // swap the data on the rightmost right node of the Left subtree of the node with that node // or swap the leftmost node of the right subtree of the node data exchange between the left node and the node // here is the rightmost node of the Left subtree, BiNode * temp = nodeRoot-> lchild; // temp is the root node BiNode * preTemp = nodeRoot-> lchild; while (nullptr! = Temp-> rchild) {preTemp = temp; // set preTemp to the rightmost node's precursor temp = temp-> rchild; // keep searching for the rightmost right node // temp points to the node to be deleted} // At This Time temp points to the rightmost node nodeRoot-> data = temp-> data; // exchange data. Due to the feature of the Binary Search Tree, this feature remains unchanged after the switch. /* 503070 20 if 50 is deleted, preTemp = temp = & 30 */if (temp! = PreTemp) preTemp-> rchild = temp-> lchild; // change the right child of the front node to elsenodeRoot of the left child of the deleted node-> lchild = temp-> lchild; delete temp; // delete the right node return true;} return false;} // T if it is a structure or class type, it must be reloaded. <
        <运算符void visit(const binode *r)const{cout<
          Data <";}/// recursive traversal is used. The three traversal principles are the same // forward traversal. The first root traversal is void PreOrderTraverse (const BiNode * nodeRoot) const {if (nodeRoot! = Nullptr) Visit (nodeRoot); if (nodeRoot-> lchild! = Nullptr) PreOrderTraverse (nodeRoot-> lchild); if (nodeRoot-> rchild! = Nullptr) PreOrderTraverse (nodeRoot-> rchild);} // The root traversal void InOrderTraverse (const BiNode * nodeRoot) const {if (nodeRoot-> lchild! = Nullptr) InOrderTraverse (nodeRoot-> lchild); if (nodeRoot! = Nullptr) // Visit (nodeRoot) when the left subtree of the vertex is empty; if (nodeRoot-> rchild! = Nullptr) InOrderTraverse (nodeRoot-> rchild);} // The void PostOrderTraverse (const BiNode * nodeRoot) const {if (nodeRoot-> lchild! = Nullptr) PostOrderTraverse (nodeRoot-> lchild); if (nodeRoot-> rchild! = Nullptr) PostOrderTraverse (nodeRoot-> rchild); if (nodeRoot! = Nullptr) Visit (nodeRoot );}};
        
       
      
     
    
   
  
 
It seems that the return in recursion is still a little unclear.
It is not clear when it should be used or when it should not be used.
Test code
# Include "bit4.cpp" int main () {BiTree B; // B. addBiNode (& B. root, 50); // set the root node value // write the second-level pointer B. addBiNode (B. getRoot (), 50); // pointer reference method int I; int arr [9] = {30, 40, 35, 27, 999 }; for (int j = 0; j <9; j ++) {I = arr [j]; if (I =-999) break; B. addBiNode (B. getRoot (), I);} B. traverse (B. getPtrToRoot (), "PreOrderTraverse"); B. traverse (B. getPtrToRoot (), "InOrderTraverse"); B. traverse (B. getPtrToRoot (), "PostOrderTraverse"); while (true) {int k; cout <"\ n enter the value to be searched:" <
  
   
> K; if (B. Search (B. getRoot (), k) cout <"OK" <
   
    


    





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.