Algorithms Series 15 Days Quick - Sixth Days Five Classics Search

Algorithms Series 15 Days Quick - Sixth Days Five Classics Search


From: http://blog.csdn.net/m13666368773/article/details/7516439

Do you feel that trees are prevalent in the data structure, and that every field should be touched and touched?

Take the sort we learned a few days ago and use the heap and today's "binary sort tree," so radically say, master the tree you are a cowman.

 

Today I'm going to talk about the "Find the Last Binary Sorting Tree" of the five classics.

 

1. concept:

In fact, <1> is very simple. If the root node has a left subtree, all the nodes of the left subtree are smaller than the root node.

If the root node has a right subtree, all nodes of the right subtree are larger than the root node.

The <2> figure is a "binary sort tree", and then compare with the concept.

              

         

2. Actual operation:

As we all know, to operate a thing is nothing more than adding, deleting, checking and modifying. Next, we will talk about the basic operation.

<1> Insertion: I believe you all know the concept of "sort tree", then the principle of insertion is very simple.

Let's say we insert a 20 into the tree.

First of all, 20 to 50 ratio, found that 20 is old and young, had to be boiled down to 50 left subtrees to compare.

Then: 20 to 30, we find that 20 is still young and old.

Then: 20 to 10, find yourself the eldest, and insert it into the right subtree of 10.

Finally, the effect rendering chart is as follows:

               

               

Finding: Believe that if you understand insertion, it's easy to understand.

Take the picture above, for example, I want to find node 10.

First of all, when 10 is compared with 50, it is found that 10 is young and old, and it is found in the left subtree of 50.

Then: 10 to 30, if you find that you are old or young, look in the left subtree of 30.

Then: 10 to 10, find the same, and then return to the signal found.

                

Delete < 3 >: Delete nodes in the tree is more troublesome, there are three main cases.

Delete "leaf node 20" in "1". This situation is relatively simple. Delete 20 will not destroy the structure of the tree. As shown in the picture:

                    

                      

In "2" delete "single child node 90", which is a little more troublesome than the first one, and need to put his child on top.

                    

                       

The deletion of "50 nodes for both right and left children" in "3" has made me tangle with coding for a long time, and the problem is very straightforward.

I deleted 50. Who had the problem, the left child? Or the right child? Or something else? Here I will

Frankly, I don't know if you know the middle order traversal of the binary tree, but I'll talk about it later. Now it's OK.

Keep in mind that the formula is to find the left child of the left subtree of the right node.

For example, first find 50 right children, 70.

Then find the leftmost child of 70 and return to him if he finds none.

Finally, the original and final drawings are as follows.  

  

 

3. Having said so much, talk to the code.

  1. using System;  
  2.  using System.Collections.Generic;  
  3.  using System.Linq;  
  4.  using System.Text;  
  5.  using System.Diagnostics;  
  6.    
  7.  namespace TreeSearch  
  8.  {  
  9.      class Program  
  10.      {  
  11.          static void Main(string[] args)  
  12.          {  
  13.              List<int> list = new List<int>() { 50, 30, 70, 10, 40, 90, 80 };  
  14.    
  15.              //Create a binary traversal tree  
  16.              BSTree bsTree = CreateBST(list);  
  17.    
  18.              Console.Write("The raw data of intermediate traversal:");  
  19.    
  20.              //Sequential traversal  
  21.              LDR_BST(bsTree);  
  22.    
  23.              Console.WriteLine("\n---------------------------------------------------------------------------n");  
  24.    
  25.              //Find a node  
  26.              Console.WriteLine("\n10 Is it included in the binary tree?" + SearchBST(bsTree, 10));  
  27.    
  28.              Console.WriteLine("\n---------------------------------------------------------------------------n");  
  29.    
  30.              bool isExcute = false;  
  31.    
  32.              //Insert a node  
  33.              InsertBST(bsTree, 20, ref isExcute);  
  34.    
  35.              Console.WriteLine("\n20 After inserting into the binary tree and traversing in middle order:");  
  36.    
  37.              //Sequential traversal  
  38.              LDR_BST(bsTree);  
  39.    
  40.              Console.WriteLine("\n---------------------------------------------------------------------------n");  
  41.    
  42.              Console.Write("Delete leaf nodes 20, \n After middle order traversal:");  
  43.    
  44.              //Delete a node (leaf node)  
  45.              DeleteBST(ref bsTree, 20);  
  46.    
  47.              //Again intermediate traversal  
  48.              LDR_BST(bsTree);  
  49.    
  50.              Console.WriteLine("\n****************************************************************************\n");  
  51.    
  52.              Console.WriteLine("Delete single-child nodes 90, \n After middle order traversal:");  
  53.    
  54.              //Delete single-child nodes  
  55.              DeleteBST(ref bsTree, 90);  
  56.    
  57.              //Again intermediate traversal  
  58.              LDR_BST(bsTree);  
  59.    
  60.              Console.WriteLine("\n****************************************************************************\n");  
  61.    
  62.              Console.WriteLine("Delete the root node 50, \n After middle order traversal:");  
  63.              //Delete the root node  
  64.              DeleteBST(ref bsTree, 50);  
  65.    
  66.              LDR_BST(bsTree);  
  67.    
  68.          }  
  69.    
  70.          ///<summary>  
  71.  /// Define a binary sort tree structure  
  72.  ///</summary>  
  73.          public class BSTree  
  74.          {  
  75.              public int data;  
  76.              public BSTree left;  
  77.              public BSTree right;  
  78.          }  
  79.    
  80.          ///<summary>  
  81.  /// Insertion of Binary Sorting Tree  
  82.  ///</summary>  
  83.  /// <param-name="bsTree">sort tree</param>  
  84.  /// <param-name="key">insertion number </param>  
  85.  /// Whether the if statement </param> has been executed by <param name="isExcute">  
  86.          static void InsertBST(BSTree bsTree, int key, ref bool isExcute)  
  87.          {  
  88.              if (bsTree == null)  
  89.                  return;  
  90.    
  91.              //If the parent node is larger than key, traverse the left subtree  
  92.              if (bsTree.data > key)  
  93.                  InsertBST(bsTree.left, key, ref isExcute);  
  94.              else  
  95.                  InsertBST(bsTree.right, key, ref isExcute);  
  96.    
  97.              if (!isExcute)  
  98.              {  
  99.                  //Building the current node  
  100.                  BSTree current = new BSTree()  
  101.                    {  
  102.                        data = key,  
  103.                        left = null,  
  104.                        right = null  
  105.                    };  
  106.    
  107.                  //The current element inserted into the parent node  
  108.                  if (bsTree.data > key)  
  109.                      bsTree.left = current;  
  110.                  else  
  111.                      bsTree.right = current;  
  112.    
  113.                  isExcute = true;  
  114.              }  
  115.    
  116.          }  
  117.    
  118.          ///<summary>  
  119.  /// Creating Binary Sorting Tree  
  120.  ///</summary>  
  121.  ///<param name="list"></param>  
  122.          static BSTree CreateBST(List<int> list)  
  123.          {  
  124.              //Building Root Node in BST  
  125.              BSTree bsTree = new BSTree()  
  126.              {  
  127.                  data = list[0],  
  128.                  left = null,  
  129.                  right = null  
  130.              };  
  131.    
  132.              for (int i = 1; i < list.Count; i++)  
  133.              {  
  134.                  bool isExcute = false;  
  135.                  InsertBST(bsTree, list[i], ref isExcute);  
  136.              }  
  137.              return bsTree;  
  138.          }  
  139.    
  140.          ///<summary>  
  141.  /// Search for a specified node in a sorted binary tree  
  142.  ///</summary>  
  143.  ///<param name="bsTree"></param>  
  144.  ///<param name="key"></param>  
  145.  ///<returns></returns>  
  146.          static bool SearchBST(BSTree bsTree, int key)  
  147.          {  
  148.              //If bsTree is empty, the traversal is over  
  149.              if (bsTree == null)  
  150.                  return false;  
  151.    
  152.              if (bsTree.data == key)  
  153.                  return true;  
  154.    
  155.              if (bsTree.data > key)  
  156.                  return SearchBST(bsTree.left, key);  
  157.              else  
  158.                  return SearchBST(bsTree.right, key);  
  159.          }  
  160.    
  161.          ///<summary>  
  162.  /// Mid-Rank Traversal Binary Sorting Tree  
  163.  ///</summary>  
  164.  ///<param name="bsTree"></param>  
  165.  ///<returns></returns>  
  166.          static void LDR_BST(BSTree bsTree)  
  167.          {  
  168.              if (bsTree != null)  
  169.              {  
  170.                  //Traversing Left Subtree  
  171.                  LDR_BST(bsTree.left);  
  172.    
  173.                  //Input node data  
  174.                  Console.Write(bsTree.data + "");  
  175.    
  176.                  //Traversing right subtree  
  177.                  LDR_BST(bsTree.right);  
  178.              }  
  179.          }  
  180.    
  181.          ///<summary>  
  182.  /// Delete the specified key node in the binary sorting tree  
  183.  ///</summary>  
  184.  ///<param name="bsTree"></param>  
  185.  ///<param name="key"></param>  
  186.          static void DeleteBST(ref BSTree bsTree, int key)  
  187.          {  
  188.              if (bsTree == null)  
  189.                  return;  
  190.    
  191.              if (bsTree.data == key)  
  192.              {  
  193.                  //The first case: leaf nodes  
  194.                  if (bsTree.left == null && bsTree.right == null)  
  195.                  {  
  196.                      bsTree = null;  
  197.                      return;  
  198.                  }  
  199.                  //The second case is that the left subtree is not empty  
  200.                  if (bsTree.left != null && bsTree.right == null)  
  201.                  {  
  202.                      bsTree = bsTree.left;  
  203.                      return;  
  204.                  }  
  205.                  //In the third case, the right subtree is not empty  
  206.                  if (bsTree.left == null && bsTree.right != null)  
  207.                  {  
  208.                      bsTree = bsTree.right;  
  209.                      return;  
  210.                  }  
  211.                  //In the fourth case, neither left nor right subtree is empty.  
  212.                  if (bsTree.left != null && bsTree.right != null)  
  213.                  {  
  214.                      var node = bsTree.right;  
  215.    
  216.                      //Find the leftmost node in the right subtree  
  217.                      while (node.left != null)  
  218.                      {  
  219.                          //Traversing its left subtree  
  220.                          node = node.left;  
  221.                      }  
  222.    
  223.                      //Exchange left and right children  
  224.                      node.left = bsTree.left;  
  225.    
  226.                      //Judging whether it is a true leaf node or a parent of an empty left child  
  227.                      if (node.right == null)  
  228.                      {  
  229.                          //Delete the leftmost node of the right subtree  
  230.                          DeleteBST(ref bsTree, node.data);  
  231.    
  232.                          node.right = bsTree.right;  
  233.                      }  
  234.                      //Re-assignment  
  235.                      bsTree = node;  
  236.    
  237.                  }  
  238.              }  
  239.    
  240.              if (bsTree.data > key)  
  241.              {  
  242.                  DeleteBST(ref bsTree.left, key);  
  243.              }  
  244.              else  
  245.              {  
  246.                  DeleteBST(ref bsTree.right, key);  
  247.              }  
  248.          }  
  249.      }  
  250.  }  

  

Operation results:

 

Note that the binary sorting tree also uses the "space for time" approach.

Suddenly found that the middle order traversal of binary sort tree can also sort arrays, ha ha ha, good!

 

PS: Insert operation: O(LogN).

Delete operation: O(LogN).

Find operation: O(LogN).


Posted by dan90joe on Sat, 18 May 2019 16:33:26 -0700