diff options
Diffstat (limited to 'c/dataStructure/树')
-rwxr-xr-x | c/dataStructure/树/btree.c | 91 | ||||
-rwxr-xr-x | c/dataStructure/树/btree.h | 27 | ||||
-rwxr-xr-x | c/dataStructure/树/main.c | 27 |
3 files changed, 145 insertions, 0 deletions
diff --git a/c/dataStructure/树/btree.c b/c/dataStructure/树/btree.c new file mode 100755 index 0000000..f41c404 --- /dev/null +++ b/c/dataStructure/树/btree.c @@ -0,0 +1,91 @@ +#include "btree.h" + +void InitTree(Tree * ptree) +{ + ptree->root = NULL; + ptree->size = 0; +} + +bool EmptyTree(const Tree * ptree) +{ + return !ptree->root?true:false; +} + +int CountItem(const Tree * ptree) +{ + return ptree->size; +} + + +static bool ToLeft(ElemType e1, ElemType e2) +{ + return e1 < e2; +} +static bool ToRight(ElemType e1, ElemType e2) +{ + return e1 > e2; +} +static bool AddNode(Node * new, Node * root) +{ + if (ToLeft(new->e, root->e)) + { + if (root->left == NULL) + root->left = new; + else + AddNode(new,root->left); + } + else if (ToRight(new->e,root->e)) + { + if (root->right == NULL) + root->right = new; + else + AddNode(new,root->right); + } + else + return false; + return true; +} + +bool AddItem(const ElemType e, Tree * ptree) +{ + Node * new = (Node *) calloc (1,sizeof(Node)); + if (!new) + return false; + new->e = e; + + ptree->size++; + + if (!ptree->root) + ptree->root = new; + else + AddNode(new,ptree->root); + + return true; +} + +bool ResTree(Node * root) +{ + Node * pnode = root; + if (pnode->right) + { + ResTree(pnode->right); + free(pnode); + } + else if (pnode->left) + { + ResTree(pnode->left); + free(pnode); + } + else + free(pnode); + return true; +} + +void PrintTree(const Node * root) +{ + printf("%c",root->e); + if (root->left) + PrintTree(root->left); + if (root->right) + PrintTree(root->right); +} diff --git a/c/dataStructure/树/btree.h b/c/dataStructure/树/btree.h new file mode 100755 index 0000000..d950745 --- /dev/null +++ b/c/dataStructure/树/btree.h @@ -0,0 +1,27 @@ +#ifndef _BTREE_H +#define _BTREE_H + +#include<stdio.h> +#include<stdbool.h> +#include<stdlib.h> + +typedef char ElemType; +typedef struct node { + ElemType e; + struct node * left; + struct node * right; +} Node; + +typedef struct tree { + Node * root; + int size; +} Tree; + +void InitTree (Tree * ptree); +bool EmptyTree (const Tree * ptree); +int CountItem (const Tree * ptree); +bool AddItem (const ElemType e, Tree * ptree); +bool ResTree (Node * pnode); +void PrintTree (const Node * root); + +#endif diff --git a/c/dataStructure/树/main.c b/c/dataStructure/树/main.c new file mode 100755 index 0000000..7def53f --- /dev/null +++ b/c/dataStructure/树/main.c @@ -0,0 +1,27 @@ +#include "btree.h" + +int main(void) +{ + Tree btree; + + ElemType e; + InitTree(&btree); + + int size = 10; + char a[] = {'A','a','B','b','C','c','D','d','E','e'}; + + if (EmptyTree(&btree)) + printf("It's empty\n"); + + for (int i = 0; i < size; i++) + AddItem(a[i],&btree); + + printf("Tree content: %d\n", CountItem(&btree)); + + PrintTree(btree.root); + putchar('\n'); + + ResTree(btree.root); + + return 0; +} |