Binary search tree (Binary Search Tree)

Date:2019-06-25 14:40:32

Basic Operations

 1 //查找
 2 void Search(node *root, int x)
 3 {
 4     if(root == NULL)
 5     {
 6         printf("search failed\n");
 7         return;
 8     }
 9     if(x == root->data)
10         printf("%d\n", root->data);
11     else if(x < root->data)
12         Search(root->lchild, x);
13     else if(x > root->data)
14         Search(root->data)
15         Search(root->rchild, x);
16 }
17 
18 //插入
19 void Insert(node *root, int x)
20 {
21     if(root == NULL)
22     {
23         root = newNode(x);  //新建结点
24         return;
25     }
26     if(x == root->data)
27         return;
28     else if(x > root->data)
29         Insert(root->rchild, x);
30     else if(x < root->data)
31         Insert(root->lchild, x);
32 }
33 
34 //建立
35 node* Create(int data[], int n)
36 {
37     node *root = NULL;
38     for(int i=0; i<n; i++)
39         Insert(root, data[i]);
40 
41     return the root;
 42 is  }
 43 is  
44 is  // delete
 45  
46 is  // Find precursor 
47 Node * FindMax (Node * the root)
 48  {
 49      the while (directory root-> rchild =! NULL)
 50          the root directory root- => rchild;
 51 is      return the root;
 52  }
 53 is  
54 is  // find subsequent 
55 Node * FindMin (Node * the root)
 56 is  {
 57 is      the while (! directory root-> lchild = NULL)
 58          the root directory root- => lchild;
 59     return the root;
 60  }
 61 is  
62 is  // delete node the root 
63 is  void the Delete (the root Node * &, int X)
 64  {
 65      IF (the root == NULL)
 66          return ;
 67      IF (directory root-> Data == X)
 68      {
 69          IF (directory root-> == NULL & lchild directory root-> rchild == NULL) // leaf node to delete 
70              the root = NULL;
 71 is          the else  IF (! directory root-> lchild = NULL)
 72          {
 73 is             * = FindMax pre Node (directory root-> lchild);       // Find the maximum value of the left subtree (rightmost node) 
74              directory root-> pre- Data => Data;                  // with the deleted node replace 
75              the Delete (the root -> lchild, pre-> Data);         // junction after deletion replacing 
76          }
 77          the else  IF ! (directory root-> rchild = NULL)
 78          {
 79              node * = pre FindMin (directory root-> rchild);       // find right subtree minimum value (leftmost node) 
80              directory root-> pre- Data => Data;
 81              the Delete (directory root-> rchild, pre-> Data);
 82          }
 83     }
84     else if(x < root->data)
85         Delete(root->lchild, x);
86     else if(x > root->data)
87         Delete(root->rchild, x);
88 }

Delete optimization

  • Delete operation, after finding replacement node, the node is the next node to be deleted, can be deleted directly
  • Currently PAT exam has not been investigated delete nodes related algorithms
  1 #include<stdio.h>
  2 const int M = 10;
  3 const int data[M] = {6,12,18,4,5,7,16,19,17,9};
  4 
  5 struct node
  6 {
  7     int data;
  8     node *lchild, *rchild;
  9 };
 10 
 11 node* FindMax(node *root)
 12 {
 13     while(root->rchild->rchild)
 14         root = root->rchild;
 15     return root;
 16 }
 17 
 18 node *FindMin(node *root)
 19 {
 20     while(root->lchild->lchild)
 21         root = root->lchild;
 22     return root;
 23 }
 24 
 25 void BST(node *&root, int x)
 26 {
 27     if(!root)
 28     {
 29         root = new node;
 30         root->data = x;
 31         root->lchild = NULL;
 32         root->rchild = NULL;
 33         return;
 34     }
 35     if(x == root->data)
 36     {
 37         if(!root->lchild && !root->rchild)
 38             root = NULL;
 39         else if(root->lchild)
 40         {
 41             node *pre;
 42             if(root->lchild->rchild == NULL)
 43             {
 44                 pre = root->lchild;
 45                 root->lchild = pre->lchild;
 46             }
 47             else
 48             {
 49                 node *fa = FindMax(root->lchild);
 50                 pre = fa->rchild;
 51                 fa->rchild = pre->lchild;
 52             }
 53             root->data = pre->data;
 54             delete(pre);
 55         }
 56         else if(root->rchild)
 57         {
 58             node *pre;
 59             if(root->rchild->lchild == NULL)
 60             {
 61                 pre = root->rchild;
 62                 root->rchild = pre->rchild;
 63             }
 64             else
 65             {
 66                 node *fa = FindMin(root->rchild);
 67                 pre = fa->rchild;
 68                 fa->rchild = pre->lchild;
 69             }
 70             root->data = pre->data;
 71             delete(pre);
 72         }
 73     }
 74     else if(x < root->data)
 75         BST(root->lchild, x);
 76     else if(x > root->data)
 77         BST(root->rchild, x);
 78 }
 79 
 80 void Traverse(node *root)
 81 {
 82     if(root == NULL)
 83         return;
 84     Traverse(root->lchild);
 85     printf("%d ", root->data);
 86     Traverse(root->rchild);
 87 }
 88 
 89 node* Create()
 90 {
 91     node *root = NULL;
 92     for(int i=0; i<M; i++)
 93         BST(root, data[i]);
 94 
 95     return root;
 96 }
 97 
 98 int main()
 99 {
100 #ifdef ONLINE_JUDGE
101 #else
102     freopen("Test.txt", "r", stdin);
103 #endif // ONLINE_JUDGE
104 
105     node *root = Create();
106     Traverse(root);
107     printf("\n");
108     BST(root, 6);
109     Traverse(root);
110 
111     return 0;
112 }

 

Guess you like

Origin www.cnblogs.com/blue-lin/p/11082782.html