1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
| #include <ctype.h> #include <limits.h> #include <math.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define MaxSize 100 typedef struct TreeNode { int value; bool isEmpty; } Tree; void InitTreeNode(Tree t) { int i; for (i = 0; i < MaxSize; i++) { t[i].isEmpty = true; } } typedef struct BiTNode { int data; struct BiTNode *lchild, *rchild; } BiTNode, *BiTree; void PreOrder(BiTree T); void visit(BiTNode* q); void LevelOrder(BiTree T);
BiTNode* p; BiTNode* pre = NULL; BiTNode* final = NULL; void PreOrder(BiTree T) { if (T != NULL) { visit(T); PreOrder(T->lchild); PreOrder(T->rchild); } } void visit(BiTNode* q) { if (q == p) { final = pre; } else { pre = q; } }
void LevelOrder(BiTree T) { LinkQueue Q; InitQueue(Q); BiTree p; EnQueue(Q, T); while (!IsEmpty(Q)) { DeQueue(Q, p); visit(p); if (p->lchild != NULL) { EnQueue(Q, p->lchild); } if (p->rchild != NULL) { EnQueue(Q, p->rchild); } } }
typedef struct ThreadNode { int data; struct ThreadNode *lchild, *rchild; int ltag, rtag; } ThreadNode, *ThreadTree;
void InThread(ThreadTree T); void visit(ThreadNode* q); void CreateInThread(ThreadTree T);
ThreadNode* pre = NULL; void InThread(ThreadTree T) { if (T != NULL) { InThread(T->lchild); visit(T); InThread(T->rchild); } } void visit(ThreadNode* q) { if (q->lchild == NULL) { q->lchild = pre; q->ltag = 1; } if (pre != NULL && pre->rchild == NULL) { pre->rchild = q; pre->rtag = 1; } pre = q; }
void CreateInThread(ThreadTree T) { pre = NULL; if (T != NULL) { INThread(T); if (pre->rchild == NULL) { pre->rtag = 1; } } }
#define MAX_TREE_SIZE 100 typedef struct { int data; int parent; } PTNode; typedef struct { PTNode nodes[MAX_TREE_SIZE]; int n; } PTree;
struct CTNode { int child; struct CTNode* next; }; typedef struct { int data; struct CTNode* firstChild; } CTBox; typedef struct { CTBox nodes[MAX_TREE_SIZE]; int n, r; } CTree;
typedef struct CSNode { int data; struct CSNode *firstchild, *nextsibing; } CSNode, *CSTree;
void PreOrder(TreeNode* R);
void PreOrder(TreeNode* R) { if (R != NULL) { visit(R); while (R->value != NULL) { PreOrder(T); } } }
typedef struct BSTNode { int key; struct BSTNode *lchild, *rchild; } BSTNode, *BSTree; BSTNode* BST_Search(BSTree T, int key) { while (T != NULL && key != T->key) { if (key < T->key) { T = T->lchild; } else { T = T->rchild; } } return T; } BSTNode* BSTSearch(BSTree T, int key) { if (T == NULL) { return NULL; } if (key == T->key) { return T; } else if (key < T->key) { return BSTSearch(T->lchild, key); } else { return BSTSearch(T->rchild, key); } }
int BST_Insert(BSTree& T, int k) { if (T == NULL) { T = (BSTree)malloc(sizeof(BSTNode)); T->key = k; T->lchild = T->rchild = NULL; return 1; } else if (k == T->key) { return 0; } else if (k < T->key) { return BST_Insert(T->lchild, k); } else { return BST_Insert(T->rchild, k); } }
void Creat_BST(BSTree& T, int str[], int n) { T = NULL; int i = 0; while (i < n) { BST_Insert(T, str[i]); i++; } }
typedef struct AVLNode { int key; int balance; struct AVLNode *lchild, *rchild; } AVLNode, *AVLTree;
|