C code de test de langue recherche arbre binaire

  1 #include <stdio.h>
   2 #include <stdlib.h>
   3 #include <stdbool.h>
   4  
  5 typedef int DONNEES;
  6 typedef struct _SNode_
   7  {
   8      données de données;
  9      struct _SNode_ * p_Right, * p_Left;
10  } Nœud - S;
11  
12 Nœud - S * CreateTree ( const DATA d)
 13  {
 14      Nœud - S * = m_root (Nœud - S *) malloc ( sizeof (Nœud - S));
15      m_root-> data = d;
16      m_root-> p_Left = m_root-> p_Right = NULL;
17      retour m_root;
18  }
 19  
20  vide InsertRight (Nœud - S * p, const DATA d)
 21  {
 22      Nœud - S * = Pnew CreateTree (d);
23      p-> p_Right = Pnew;
24  }
 25  
26  vide InsertLeft (Nœud - S * p, const DATA d)
 27  {
 28      Nœud - S * = Pnew CreateTree (d);
29      p-> p_Left = Pnew;
30  }
 31 
32  vide précommande (Nœud - S * p)
 33  {
 34      printf ( " % d " , p-> données);
35      si (p-> p_Left)
 36          précommande (p-> p_Left);
37      si (p-> p_Right)
 38          précommande (p-> p_Right);
39  }
 40  
41  vide afinde (Nœud - S * p)
 42  {
 43      si (p-> p_Left)
 44          inorder (p-> p_Left);
45      printf (" % D " , p-> données);
46      si (P-> p_Right)
 47          afinde (P-> p_Right);
48  }
 49  
50  vide postorder (Nœud - S * p)
 51  {
 52      si (p-> p_Left)
 53          postorder (p-> p_Left);
54      si (p-> p_Right)
 55          postorder (p-> p_Right);
56      printf ( " % d " , p-> données);
57  }
 58  
59 BOOL le lookup (SNODE P *, const les données D)
 60  {
 61 est      le temps (P)
 62 est      {
 63 est          SI (p-> Données == D)
 64              retour  à vrai ;
 65          le reste  IF (p-> Données> D)
 66              P = P -> P_left;
 67          l'autre 
68              P = p-> P_right;
 69      }
 70      retour  à faux ;
 71 est  }
 72  
73 est  // Deuxième façon: l' utilisation de deux pointeurs 
74  videSetat (SNODE P *, const DATA D) // créer une recherche binaire 
75  {
 76      IF (P == NULL)
 77      {
 78          P = CreateTree (D);
 79          retour ;
 80      }
 81      SNODE ** & ppRoot = P;
 82      le tout (* ppRoot)
 83      {
 84          IF ((* ppRoot) -> données < D)
 85              ppRoot = & (* ppRoot) -> P_right,
 86          l'autre  IF ((* ppRoot) -> données> D)
 87             ppRoot = & (* ppRoot) -> p_Left;
88      }
 89      infâmes * Pnew = Créer arbre (d);
90      * ppRoot = Pnew;
91  }
 92  
93  int main ()
 94  {
 95      néfaste * Proot = Créer un arbre ( 30 );
96      Sétat (Proot, 75 );
97      Sétat (Proot, 20 );
98      Sétat (Proot, 40 );
99      Sétat (Proot, 10 );
100      Sétat (Proot, 60 );
101      Sétat (PROOT, 50 );
 102  
103      IF (la LookUp (PROOT, 52 est ))
 104          printf ( " trouvé \ n-! " );
 105      le reste 
106 de          la printf ( " non trouvé \ n-! " );
 107  
108      la afinde (PROOT )
 109  
110      retour  0 ;
 111 }

 

Je suppose que tu aimes

Origine www.cnblogs.com/veis/p/12579390.html
conseillé
Classement