Algorithm and data Structure Foundation 11:c++ implementation--two split search tree node Delete

Source: Internet
Author: User

Based on my other article, "algorithm and data Structure Foundation 4:c++ two fork tree implementation and traversal method Daquan", the structure of the binary tree is used in this article.


The deletion of the two search forks can be broken down into three different situations:
1 is deleted is the leaf node, directly deleted;
2 is removed with only one child node, the pointer moves down;
3 has two child nodes, in order not to destroy the structure of the tree, you need to find a node to replace the current node.
According to the characteristics of the binary tree, the current node is greater than all the left subtrees, less than all the right sub-trees,
You can replace the current node with the largest node in the left Dial hand tree, or the smallest node in the right subtree, and then delete the replacement node.


BSTree.h

#include <cstdio> #include <iostream> #include <stack> #include <queue>using namespace std;// Binary search tree, Chinese translation is two forks, binary lookup tree, or binary sort tree. Abbreviation for bstclass bstree{struct node{node (int x = 0):d ata (x), lchild (null), rchild (null) {}struct node* lchild;struct node* Rchi Ld;int data;}; PUBLIC://the four functions of the **************************************************************************//class: constructors, copy constructors, Overloaded assignment operator, destructor//**************************************************************************bstree (); ~BSTree ();//* Additions/deletions//****************************** void Insert (int x); void Remove (int x);//Returns the number of binary trees unsigned short Size (); unsigned short deep (), unsigned short Leaf (), bool IsEmpty ();//traversal void preordertraversal ();//first sequence traversal void Inordertraversal () ;//sequence traversal void postordertraversal ();//post-traversal void depthfirstsearch ();//depth-first traversal of void Breadthfirstsearch ();//breadth-first traversal of private: void Remove (int x, node** Pnode);//recursive calculation of the number of binary trees unsigned short countsize (node* N), unsigned short countdeep (node* N), unsigned short countleaf (node* N) ;//recursive Traversal of void Preordertraversal (node* n); void Inordertraversal (node* n); void Postordertraversal (node* n); void Depthfirstsearch (node* N), void Breadthfirstsearch (node* n), void free (node* Node);p rivate:node* m_root;};/ /**************************************************************************//Private method//**************************** /*//1 What's wrong with the code below? void Bstree::remove (int x, node* Node) {if (!node) {return;} if (x < Node->data) {Remove (x, node->lchild);} else if (x > Node->data) {Remove (x, node->rchild);} else if (node->lchild && node->rchild) {node* min = node->rchild;while (min->lchild) {min = min-> Lchild;} Node->data = min->data; Remove (Node->data, node->rchild);} else{node* tmp = node;if (node->lchild) {Node = Node->lchild;} else if (node->rchild) {node = Node->rchild;}Else{node = NULL;} Delete tmp;tmp = NULL;}} node is a local variable, and the operation of node is not valid except for delete//2 Ask again, what is the problem with the following code? void Bstree::remove (int x, node** pnode) {if (!pnode | |!) ( *pnode)) {return;} node* node = *pnode;if (x < Node->data) {Remove (x, node->lchild);} else if (x > Node->data) {Remove (x, node->rchild);} else if (node->lchild && node->rchild) {node* min = node->rchild;while (min->lchild) {min = min-> Lchild;} Node->data = min->data; Remove (Node->data, node->rchild);} else{node* tmp = node;if (node->lchild) {Node = Node->lchild;} else if (node->rchild) {node = Node->rchild;} Else{node = NULL;} Delete tmp;tmp = NULL;}} The same problem, although the parameter passed here is a pointer, but node* node = *pnode; */void bstree::remove (int x, node** pnode) {if (!pnode | | | *pnode)) {return;} if (x < (*pnode)->data) {Remove (x, & ((*pnode)->lchild));} else if (x > (*pnode)->data) {Remove (x, & ((*pnode)->rchild));} else if ((*pnode)->lchild && (*pnode)->rchild) {node* min = (*pnode)->rchild;while (min->lchild) {min = Min->lchild;} (*pnode)->data = min->data; Remove ((*pnode)->data, & ((*pnode)->rchild));} else{node* tmp = *pnode;if ((*pnode)->lchild) {(*pnode) = (*pnode)->lchild;} else if ((*pnode)->rchild) {*pnode = (*pnode)->rchild;} Else{*pnode = NULL;} Delete tmp;tmp = NULL;}} unsigned short bstree::countsize (node* N) {if (!n) {return 0;} Return Countsize (N->lchild) + countsize (n->rchild) + 1;} unsigned short bstree::countdeep (node* N) {if (!n) {return 0;} int ldeep = Countdeep (n->lchild); int rdeep = Countdeep (N->rchild); return (Ldeep > Rdeep)? (Ldeep + 1): (Rdeep + 1);} unsigned short bstree::countleaf (node* N) {if (!n) {return 0;} if (!n->lchild&&!n->rchild) {return 1;} Return Countleaf (N->lchild) + countleaf (n->rchild);} void Bstree::P reordertraversal (node* N) {if (n) {cout << n->data << ","; Preordertraversal (N->lchild); Preordertraversal (N->rchild);}} void Bstree::inordertraversal(node* N) {if (n) {inordertraversal (n->lchild); cout << n->data << ","; Inordertraversal (N->rchild);}} void Bstree::P ostordertraversal (node* N) {if (n) {postordertraversal (n->lchild); Postordertraversal (n->rchild); cout << n->data << ",";}} void Bstree::D epthfirstsearch (node* root) {Stack<node *> nodestack;nodestack.push (root); node* node = null;while (!nodestack.empty ()) {node = Nodestack.top (); cout << node->data << ","; Nodestack.pop (); if (node->rchild) {Nodestack.push (node->rchild);} if (node->lchild) {Nodestack.push (node->lchild);}}} void Bstree::breadthfirstsearch (node* root) {Queue<node *> nodequeue;nodequeue.push (root); node* node = null;while (!nodequeue.empty ()) {node = Nodequeue.front (); Nodequeue.pop () cout << Node->data < < ","; if (node->lchild) {Nodequeue.push (node->lchild);} if (node->rchild) {Nodequeue.push (node->rchild);}}} void Bstree::free (node* n) {if (n) {free (n->lchild); Free (n-&Gt;rchild);d elete n;n = NULL;}} Four functions of a class: constructor, copy constructor, overloaded assignment operator, destructor/ /**************************************************************************bstree::bstree () {m_root = NULL;} Bstree::~bstree () {free (m_root);} Additions/deletions//*************************** void Bstree::insert (int x) {node* tmp = new Node (x); if (!m_root) {M_root = tmp;} else{node* pre = M_root; node* cur = m_root;while (cur) {pre = Cur;cur = (x < cur->data)? (cur->lchild): (Cur->rchild);} (x < Pre->data)? (Pre->lchild = tmp): (pre->rchild = tmp);}} void Bstree::remove (int x) {if (!m_root) {return;} Remove (x, &m_root);} unsigned short bstree::size () {return countsize (m_root);} unsigned short bstree::D eep () {return countdeep (m_root);} unsigned short bstree::leaf () {return countleaf (m_root);} BOOL Bstree::isempty () {return m_root = = NULL;} void Bstree::P reordertraversal () {preordertraversal (m_root); cout << Endl;} void Bstree::inordertraversal () {inordertraversal (m_root); cout << Endl;} void Bstree::P ostordertraversal () {postordertraversal (m_root); cout << Endl;} void Bstree::D epthfirstsearch () {depthfirstsearch (m_root); cout << Endl;} void Bstree::breadthfirstsearch () {breadthfirstsearch (m_root); cout << Endl;}



Main.cpp
Test for Bstree#include "BSTree.h" #include <cstdlib> #include <iostream>using namespace Std;int main () { Bstree Tree;int Arr[6] = {5, 4, 8, 1, 7, 10};for (int i = 0; i < 6; ++i) {tree. Insert (Arr[i]);} Tree. Preordertraversal (); tree. Inordertraversal (); tree. Postordertraversal (); tree. Depthfirstsearch (); tree. Breadthfirstsearch (); tree. Remove (4); tree. Preordertraversal (); tree. Remove (1); tree. Preordertraversal (); tree. Remove (); tree. Preordertraversal (); cout << "Size:" << tree. Size () << endl;cout << "deep:" << tree. Deep () << endl;cout << "leaf:" << tree. Leaf () << endl;system ("pause"); return 0;}



Structure of the tree



Run results


Algorithm and data Structure Foundation 11:c++ implementation--two split search tree node Delete

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.