Insert and delete binary tree and binary tree

Source: Internet
Author: User

Insert and delete binary tree and binary tree

Insert a binary sorting tree

First, check whether the data to be inserted already exists. If yes, do not insert the data. If no, insert the element to the left and right children of the node where the binary tree fails to be searched. In special cases, the binary tree is empty before the first element is inserted.

  

 1 bool insert(BiTreeNode *&root,DataType *item) { 2     BiTreeNode *current = NULL,*parent = NULL,*p = NULL; 3     current = root; 4     while(current != NULL) { 5         if(current->data.key == item->key)return false; 6         parent = current; 7         if(current->data.key < item->key) 8             current = current->rightChild; 9         else10             current = current->leftChild;11     }12     p = new BiTreeNode;13     p->data = *item;14 15     //the new node is root node16     if(parent == NULL)17         root = p;18     //as leftChild19     else if(item->key < parent->data.key)20         parent->leftChild = p;21     //as rightChild22     else23         parent->rightChild = p;24     return true;25 }

Ii. Deletion of the binary sorting tree

The delete operation requires that: first, check whether the element to be deleted exists. If the element does not exist, return the result directly. Theoretically, two or four conditions must be considered.

A. the node to be deleted is the root node.

B. the node to be deleted is not the root node.

1. This node has no child nodes.

Sln1: Delete the node directly.

2. This node only has the left child node.

Sln2: the parent node of the deleted node points to the left child node of the node. If the deleted node is the Left or Right node of its parent node, or the root node, You need to determine whether it is the root node.

3. This node only has the right child node.

Sln3: similar to sln2.

4. This node has left and right child nodes.

Sln4: first, the keyword of the data element is greater than the minimum value of the keyword of the node to be deleted. That is, the leftmost subtree of the right subtree of the node to be deleted, for example, Kdata.

Then, assign Kdata data to the elements to be deleted.

Finally, use the right subtree of the element to be deleted as the root node and Kdata as the data to be deleted, and call the deletion algorithm recursively.

1 bool deleteNode (BiTreeNode * & root, DataType item) {2 BiTreeNode * current = root, * parent = NULL, * p = NULL, * kmin = NULL; 3 bool flag = false; 4 // search for the node to be deleted and its parent node 5 while (current! = NULL) {6 if (current-> data. key = item. key) {7 flag = true; 8 break; 9} 10 parent = current; 11 if (current-> data. key <item. key) 12 current = current-> rightChild; 13 else current = current-> leftChild; 14} 15 // The element to be deleted is not found 16 if (flag = false) {17 cout <"delete error: item not found" <endl; 18 return false; 19} 20 if (current-> leftChild = NULL) {21 p = current; 22 if (parent! = NULL) {23 if (parent-> leftChild! = NULL & parent-> leftChild-> data. key = current-> data. key) 24 parent-> leftChild = current-> rightChild; 25 else parent-> rightChild = current-> rightChild; 26} else {27 root = current-> rightChild; 28} 29 delete p; 30} else if (current-> rightChild = NULL) {31 p = current; 32 if (parent! = NULL) {33 if (parent-> leftChild! = NULL & parent-> leftChild-> data. key = current-> data. key) 34 parent-> leftChild = current-> leftChild; 35 else parent-> rightChild = current-> leftChild; 36} else {37 root = current-> leftChild; 38} 39 delete p; 40} else {41 kmin = current-> rightChild; 42 while (kmin-> leftChild! = NULL) {43 kmin = kmin-> leftChild; 44} 45 if (parent! = NULL) 46 current-> data = kmin-> data; 47 else48 root-> data = kmin-> data; 49 deleteNode (current-> rightChild, kmin-> data ); 50} 51 return true; 52}

Complete Test code:

1 # include <iostream> 2 # include <stdlib. h> 3 # include <stdio. h> 4 5 using namespace std; 6 7 struct DataType {8 int key; 9}; 10 11 typedef struct node {12 DataType data; 13 struct node * leftChild; 14 struct node * rightChild; 15 node () {16 leftChild = NULL; 17 rightChild = NULL; 18} 19} BiTreeNode; 20 21 BiTreeNode * search (BiTreeNode * root, DataType item) {22 BiTreeNode * p = NULL; 23 if (root! = NULL) {24 p = root; 25 while (p! = NULL) {26 if (p-> data. key = item. key) 27 return p; 28 if (item. key> p-> data. key) 29 p = p-> rightChild; 30 else 31 p = p-> leftChild; 32} 33} 34 return NULL; 35} 36 37 bool insert (BiTreeNode * & root, dataType * item) {38 BiTreeNode * current = NULL, * parent = NULL, * p = NULL; 39 current = root; 40 while (current! = NULL) {41 if (current-> data. key = item-> key) return false; 42 parent = current; 43 if (current-> data. key <item-> key) 44 current = current-> rightChild; 45 else 46 current = current-> leftChild; 47} 48 p = new BiTreeNode; 49 p-> data = * item; 50 51 // the new node is root node 52 if (parent = NULL) 53 root = p; 54 // as leftChild 55 else if (item-> key <parent-> data. key) 56 parent-> leftChild = p; 57 // As rightChild 58 else 59 parent-> rightChild = p; 60 return true; 61} 62 63 void inTraverse (BiTreeNode * root) {64 if (root = NULL) {65 cout <"inTraverse error: root = NULL" <endl; 66 return; 67} 68 if (root-> leftChild! = NULL) 69 inTraverse (root-> leftChild); 70 cout <root-> data. key <""; 71 if (root-> rightChild! = NULL) 72 inTraverse (root-> rightChild); 73} 74 75 bool deleteNode (BiTreeNode * & root, DataType item) {76 BiTreeNode * current = root, * parent = NULL, * p = NULL, * kmin = NULL; 77 bool flag = false; 78 // find the node to be deleted and its parent node 79 while (current! = NULL) {80 if (current-> data. key = item. key) {81 flag = true; 82 break; 83} 84 parent = current; 85 if (current-> data. key <item. key) 86 current = current-> rightChild; 87 else current = current-> leftChild; 88} 89 // The element 90 to be deleted is not found if (flag = false) {91 cout <"delete error: item not found" <endl; 92 return false; 93} 94 if (current-> leftChild = NULL) {95 p = current; 96 if (parent! = NULL) {97 if (parent-> leftChild! = NULL & parent-> leftChild-> data. key = current-> data. key) 98 parent-> leftChild = current-> rightChild; 99 else parent-> rightChild = current-> rightChild; 100} else {101 root = current-> rightChild; 102} 103 delete p; 104} else if (current-> rightChild = NULL) {105 p = current; 106 if (parent! = NULL) {107 if (parent-> leftChild! = NULL & parent-> leftChild-> data. key = current-> data. key) 108 parent-> leftChild = current-> leftChild; 109 else parent-> rightChild = current-> leftChild; 110} else {111 root = current-> leftChild; 112} 113 delete p; 114} else {115 kmin = current-> rightChild; 116 while (kmin-> leftChild! = NULL) {117 kmin = kmin-> leftChild; 118} 119 if (parent! = NULL) 120 current-> data = kmin-> data; 121 else122 root-> data = kmin-> data; 123 deleteNode (current-> rightChild, kmin-> data ); 124} 125 return true; 126} 127 128 void createFile (int n) {129 FILE * fp = fopen ("file.txt", "w"); 130 while (n --) {131 fprintf (fp, "% d", rand () % 10000); 132} 133 fclose (fp); 134} 135 136 int * readFile (int n) {137 int * num = new int [n], I = 0; 138 FILE * fp = fopen ("file.txt", "r"); 139 while (! Feof (fp) {140 fscanf (fp, "% d", & num [I ++]); 141} 142 return num; 143} 144 145 int main () {146 BiTreeNode * root = NULL; 147 int n = 100, * num = NULL, I; 148 createFile (n); 149 num = readFile (n ); 150 for (I = 0; I <n; I ++) {151 DataType x; 152 x. key = num [I]; 153 insert (root, & x); 154} 155 for (I = 0; I <n; I ++) {156 cout <endl <"del:" <num [I] <endl; 157 DataType m; 158 m. key = num [I]; 159 deleteNode (root, m); 160 inTraverse (root); 161} 162 // BiTreeNode * root = NULL; 163 return 0; 164}View Code

 

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.