二叉树的递归创建,二叉树查找,二叉树结点的删除代码
二叉树的递归创建
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 createbtree(int* data,int pos) 15 { 16 btree newnode; 17 18 if(data[pos] == 0 || pos > 15) 19 return NULL; 20 else 21 { 22 newnode = (btree)malloc(sizeof(treenode)); 23 newnode->data = data[pos]; 24 25 newnode->left = createbtree(data,2*pos); 26 newnode->right = createbtree(data,2*pos + 1); 27 return newnode; 28 } 29 } 30 31 void printbtree(btree ptr) 32 { 33 if(ptr != NULL) 34 { 35 printbtree(ptr->left); 36 printf("[%2d]",ptr->data); 37 printbtree(ptr->right); 38 } 39 } 40 41 int main() 42 { 43 btree root = NULL; 44 int i; 45 46 int data[16] = {0,5,4,6,2,0,0,8,1,3,0,0,0,0,7,9}; 47 root = createbtree(data,1); 48 printf("数组的节点内容 \n"); 49 for(i = 1;i < 16;i++) 50 printf("[%2d]",data[i]); 51 printf("\n"); 52 printf("树的结点内容 \n"); 53 printbtree(root); 54 printf("\n"); 55 return 0; 56 }
使用递归创建二叉查找树,分别使用遍历和二叉查找树方式运行查找
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 createbtree(int* data,int pos) 15 { 16 btree newnode; 17 18 if(data[pos] == 0 || pos > 15) 19 return NULL; 20 else 21 { 22 newnode = (btree)malloc(sizeof(treenode)); 23 newnode->data = data[pos]; 24 25 newnode->left = createbtree(data,2*pos); 26 newnode->right = createbtree(data,2*pos + 1); 27 return newnode; 28 } 29 } 30 31 btree btreefind(btree ptr,int value) 32 { 33 while(ptr != NULL) 34 { 35 if(ptr->data == value) 36 return ptr; 37 else 38 if(ptr->data > value) 39 ptr = ptr->left; 40 else 41 ptr = ptr->right; 42 } 43 return NULL; 44 } 45 46 btree btreesearch(btree ptr,int value) 47 { 48 btree ptr1,ptr2; 49 50 if(ptr != NULL) 51 { 52 if(ptr->data == value) 53 return ptr; 54 else 55 56 ptr1 = btreesearch(ptr->left,value); 57 ptr2 = btreesearch(ptr->right,value); 58 if(ptr1 != NULL) 59 return ptr1; 60 else 61 if(ptr2 != NULL) 62 return ptr2; 63 else 64 return NULL; 65 } 66 else 67 return NULL; 68 } 69 70 int main() 71 { 72 btree root = NULL; 73 btree ptr = NULL; 74 int value; 75 76 int data[16] = {0,5,4,6,2,0,0,8,1,3,0,0,0,0,7,9}; 77 root = createbtree(data,1); 78 printf("请输入寻找结点数据(1-9) ==> "); 79 scanf("%d",&value); 80 ptr = btreefind(root,value); 81 if(ptr != NULL) 82 printf("二叉查找树:结点数据是 [%d]\n",ptr->data); 83 else 84 printf("二叉查找树:没有找到\n"); 85 ptr = btreesearch(root,value); 86 if(ptr != NULL) 87 printf("遍历查找:结点数据是 [%d]\n",ptr->data); 88 else 89 printf("遍历查找:没有找到\n"); 90 return 0; 91 }
用递归创建一颗二叉树,然后输入一个结点值,找到就将结点删除
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 createbtree(int* data,int pos) 15 { 16 btree newnode; 17 18 if(data[pos] == 0 || pos > 15) 19 return NULL; 20 else 21 { 22 newnode = (btree)malloc(sizeof(treenode)); 23 newnode->data = data[pos]; 24 25 newnode->left = createbtree(data,2*pos); 26 newnode->right = createbtree(data,2*pos + 1); 27 return newnode; 28 } 29 } 30 31 btree btreefind(btree ptr,int value,int* pos) 32 { 33 btree previous; 34 35 previous = ptr; 36 *pos = 0; 37 while(ptr != NULL) 38 { 39 if(ptr->data == value) 40 return previous; 41 else 42 previous = ptr; 43 if(ptr->data > value) 44 { 45 ptr = ptr->left; 46 *pos = -1; 47 } 48 else 49 { 50 ptr = ptr->right; 51 *pos = 1; 52 } 53 54 } 55 return NULL; 56 } 57 58 btree deletenode(btree root,int value) 59 { 60 btree previous; 61 btree ptr; 62 btree next; 63 int pos; 64 65 previous = btreefind(root,value,&pos); 66 if(previous == NULL) 67 return root; 68 switch(pos) 69 { 70 case -1: ptr = previous->left; 71 break; 72 case 1: ptr= previous->right; 73 break; 74 case 0: ptr = previous; 75 break; 76 } 77 78 if(ptr->left == NULL) 79 { 80 if(previous != ptr) 81 previous->right = ptr->right; 82 else 83 root = root->right; 84 free(ptr); 85 return root; 86 } 87 88 if(ptr->right == NULL) 89 { 90 if(previous != ptr) 91 previous->left = ptr->left; 92 else 93 root = root->left; 94 free(ptr); 95 return root; 96 } 97 98 previous = ptr; 99 next = ptr->left; 100 while(next->right != NULL) 101 { 102 previous = next; 103 next = next->right; 104 } 105 ptr->data = next->data; 106 if(previous->left == next) 107 previous->left = next->left; 108 else 109 previous->right = next->right; 110 free(next); 111 return root; 112 } 113 114 void printbtree(btree ptr) 115 { 116 if(ptr != NULL) 117 { 118 printbtree(ptr->left); 119 printf("[%2d]",ptr->data); 120 printbtree(ptr->right); 121 } 122 } 123 124 int main() 125 { 126 btree root = NULL; 127 int value; 128 129 int data[16] = {0,5,4,6,2,0,0,8,1,3,0,0,0,0,7,9}; 130 root = createbtree(data,1); 131 printbtree(root); 132 printf("\n请输入要删除的节点数据 ==> "); 133 scanf("%d",&value); 134 root = deletenode(root,value); 135 printf("删除后树的结点内容 \n"); 136 printbtree(root); 137 printf("\n"); 138 return 0; 139 }