1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <stdbool.h> 4 5 typedef int DATA; 6 typedef struct _SNode_ 7 { 8 DATA data; 9 struct _SNode_ *p_Right, *p_Left; 10 }SNode; 11 12 SNode *CreateTree(const DATA d) 13 { 14 SNode *m_root = (SNode *)malloc(sizeof(SNode)); 15 m_root->data = d; 16 m_root->p_Left = m_root->p_Right = NULL; 17 return m_root; 18 } 19 20 void InsertRight(SNode *p, const DATA d) 21 { 22 SNode *pNew = CreateTree(d); 23 p->p_Right = pNew; 24 } 25 26 void InsertLeft(SNode *p, const DATA d) 27 { 28 SNode *pNew = CreateTree(d); 29 p->p_Left = pNew; 30 } 31 32 void PreOrder(SNode *p) 33 { 34 printf("%d ", p->data); 35 if (p->p_Left) 36 PreOrder(p->p_Left); 37 if (p->p_Right) 38 PreOrder(p->p_Right); 39 } 40 41 void InOrder(SNode *p) 42 { 43 if (p->p_Left) 44 InOrder(p->p_Left); 45 printf("%d ", p->data); 46 if (p->p_Right) 47 InOrder(p->p_Right); 48 } 49 50 void PostOrder(SNode *p) 51 { 52 if (p->p_Left) 53 PostOrder(p->p_Left); 54 if (p->p_Right) 55 PostOrder(p->p_Right); 56 printf("%d ", p->data); 57 } 58 59 BOOL the LookUp (SNODE P *, const the DATA D) 60 { 61 is the while (P) 62 is { 63 is IF (p-> Data == D) 64 return to true ; 65 the else IF (p-> Data> D) 66 P = P -> P_left; 67 the else 68 P = p-> P_right; 69 } 70 return to false ; 71 is } 72 73 is // Second way: using two pointers 74 voidSetAt (SNODE P *, const the DATA D) // create a binary search 75 { 76 IF (P == NULL) 77 { 78 P = CreateTree (D); 79 return ; 80 } 81 SNODE ** & ppRoot = P; 82 the while (* ppRoot) 83 { 84 IF ((* ppRoot) -> Data < D) 85 ppRoot = & (* ppRoot) -> P_right; 86 the else IF ((* ppRoot) -> Data> D) 87 ppRoot = & (* ppRoot) -> p_Left; 88 } 89 nefarious * Pnew = Create Tree (d); 90 * ppRoot = Pnew; 91 } 92 93 int main () 94 { 95 nefarious * Proot = Create Tree ( 30 ); 96 SetAt (Proot, 75 ); 97 SetAt (Proot, 20 ); 98 SetAt (Proot, 40 ); 99 SetAt (Proot, 10 ); 100 SetAt (Proot, 60 ); 101 SetAt (PROOT, 50 ); 102 103 IF (the LookUp (PROOT, 52 is )) 104 the printf ( " found \ n-! " ); 105 the else 106 the printf ( " not found \ n-! " ); 107 108 the InOrder (PROOT ); 109 110 return 0 ; 111 }