二叉树数据结构代码
二叉树的数组表示
1 #include <stdio.h> 2 3 void createbtree(int * btree,int *data,int len) 4 { 5 int level; 6 int i; 7 8 btree[1] = data[1]; 9 for(i = 2; i <= len;i++) 10 { 11 level = 1; 12 while(btree[level] != 0) 13 { 14 if(data[i] > btree[level]) 15 level = level * 2 + 1; 16 else 17 level = level * 2; 18 } 19 btree[level] = data[i]; 20 } 21 } 22 23 int main() 24 { 25 int btree[16]; 26 27 int data[10] = {0,5,6,4,8,2,3,7,1,9}; 28 int i; 29 30 for(i = 1;i < 16;i++) 31 btree[i] = 0; 32 createbtree(btree,data,9); 33 for(i = 1;i < 16;i++) 34 printf("%2d: [%d] \n",i,btree[i]); 35 36 return 0; 37 }
二叉树的结构数组表示
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 struct tree 5 { 6 int data; 7 int left; 8 int right; 9 }; 10 typedef struct tree treenode; 11 treenode btree[15]; 12 13 void createbtree(int *data,int len) 14 { 15 int level; 16 int pos; 17 int i; 18 19 btree[0].data = data[0]; 20 for(i = 1;i < len;i++) 21 { 22 btree[i].data = data[i]; 23 level = 0; 24 pos = 0; 25 while(pos == 0) 26 { 27 if(data[i] > btree[level].data) 28 if(btree[level].right != -1) 29 level = btree[level].right; 30 else 31 pos = -1; 32 else 33 if(btree[level].left != -1) 34 level = btree[level].left; 35 else 36 pos = 1; 37 } 38 if(pos == 1) 39 btree[level].left = i; 40 else 41 btree[level].right = i; 42 } 43 } 44 45 int main() 46 { 47 int data[10] = {5,6,4,8,2,3,7,1,9}; 48 int i; 49 50 for(i = 0;i < 15;i++) 51 { 52 btree[i].data = 0; 53 btree[i].left = -1; 54 btree[i].right = -1; 55 } 56 createbtree(data,9); 57 printf(" 左 数据 右 \n"); 58 printf("----------------------\n"); 59 for(i = 0;i < 15;i++) 60 if(btree[i].data != 0) 61 printf("%2d:[%2d] [%2d] [%2d]\n",i,btree[i].left, 62 btree[i].data,btree[i].right); 63 return 0; 64 }
使用链表创建二叉树
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 struct tree 5 { 6 int data; 7 struct tree* left; 8 struct tree* right; 9 }; 10 11 typedef struct tree treenode; 12 typedef treenode* btree; 13 14 btree insertnode(btree root,int value) 15 { 16 btree newnode; 17 btree current; 18 btree back; 19 20 newnode = (btree)malloc(sizeof(treenode)); 21 newnode->data = value; 22 newnode->left = NULL; 23 newnode->right = NULL; 24 if(root == NULL) 25 { 26 return newnode; 27 } 28 else 29 { 30 current = root; 31 while(current != NULL) 32 { 33 back = current; 34 if(current->data > value) 35 current = current->left; 36 else 37 current = current->right; 38 } 39 if(back->data > value) 40 back->left = newnode; 41 else 42 back->right = newnode; 43 } 44 return root; 45 } 46 47 48 btree createbtree(int* data,int len) 49 { 50 btree root = NULL; 51 int i; 52 53 for(i = 0;i < len;i++) 54 root = insertnode(root,data[i]); 55 return root; 56 } 57 58 59 void printbtree(btree root) 60 { 61 btree ptr; 62 63 ptr = root->left; 64 printf("输出左子树:\n"); 65 while(ptr != NULL) 66 { 67 printf("[%2d]\n",ptr->data); 68 ptr = ptr->left; 69 } 70 ptr = root->right; 71 printf("输出右子树:\n"); 72 while(ptr != NULL) 73 { 74 printf("[%2d]\n",ptr->data); 75 ptr = ptr->right; 76 } 77 } 78 79 int main() 80 { 81 btree root = NULL; 82 83 int data[10] = {5,6,4,8,2,3,7,1,9}; 84 root = createbtree(data,9); 85 printf("树的节点内容 \n"); 86 printbtree(root); 87 return 0; 88 }