二叉树的复制,线索二叉树,用树处理表达式
使用递归方式创建二叉树,然后备份原来的二叉树,最后将原来的二叉树和备份的二叉树都输出来
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 copybtree(btree root) 32 { 33 btree newnode; 34 35 if(root == NULL) 36 return NULL; 37 else 38 { 39 newnode = (btree)malloc(sizeof(treenode)); 40 newnode->data = root->data; 41 newnode->left = copybtree(root->left); 42 newnode->right = copybtree(root->right); 43 return newnode; 44 } 45 } 46 47 48 void printbtree(btree ptr) 49 { 50 if(ptr != NULL) 51 { 52 printbtree(ptr->left); 53 printf("[%2d]",ptr->data); 54 printbtree(ptr->right); 55 } 56 } 57 58 int main() 59 { 60 btree root = NULL; 61 btree backup = NULL; 62 63 int data[16] = {0,5,4,6,2,0,0,8,1,3,0,0,0,0,7,9}; 64 root = createbtree(data,1); 65 backup = copybtree(root); 66 printf("原二叉树的结点内容\n"); 67 printbtree(root); 68 printf("\n备份二叉树的结点内容 \n"); 69 printbtree(backup); 70 printf("\n"); 71 return 0; 72 }
使用数组值创建线索二叉树,然后使用中序遍历方式将二叉树的内容输出来
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 struct t_tree 5 { 6 int data; 7 int leftthread; 8 int rightthread; 9 struct t_tree* left; 10 struct t_tree* right; 11 }; 12 13 typedef struct t_tree treenode; 14 typedef treenode* t_btree; 15 16 t_btree insertnode(t_btree root,int value) 17 { 18 t_btree newnode; 19 t_btree current; 20 t_btree parent; 21 t_btree previous; 22 int pos; 23 24 newnode = (t_btree)malloc(sizeof(treenode)); 25 newnode->data = value; 26 newnode->left = NULL; 27 newnode->right = NULL; 28 newnode->leftthread = 1; 29 newnode->rightthread = 1; 30 current = root->right; 31 if(current == NULL) 32 { 33 root->right = newnode; 34 newnode->left = root; 35 newnode->right = root; 36 return root; 37 } 38 parent = root; 39 pos = 0; 40 while(current != NULL) 41 { 42 if(current->data > value) 43 { 44 if(pos != -1) 45 { 46 pos = -1; 47 previous = parent; 48 } 49 parent = current; 50 if(current->leftthread == 0) 51 current = current->left; 52 else 53 current = NULL; 54 } 55 else 56 { 57 if(pos != 1) 58 { 59 pos = 1; 60 previous = parent; 61 } 62 parent = current; 63 if(current->rightthread == 0) 64 current = current->right; 65 else 66 current = NULL; 67 } 68 } 69 if(parent->data > value) 70 { 71 parent->leftthread = 0; 72 parent->left = newnode; 73 newnode->left = previous; 74 newnode->right = parent; 75 } 76 else 77 { 78 parent->rightthread = 0; 79 parent->right = newnode; 80 newnode->left = parent; 81 newnode->right = previous; 82 } 83 return root; 84 } 85 86 t_btree createtbtree(int* data,int len) 87 { 88 t_btree root = NULL; 89 int i; 90 91 root = (t_btree)malloc(sizeof(treenode)); 92 root->left = root; 93 root->right = NULL; 94 root->leftthread = 1; 95 root->rightthread = 0; 96 97 for(i = 0;i < len;i++) 98 root = insertnode(root,data[i]); 99 return root; 100 101 } 102 103 void printtbtree(t_btree root) 104 { 105 t_btree ptr; 106 107 ptr = root; 108 do 109 { 110 if(ptr->rightthread == 1) 111 ptr = ptr->right; 112 else 113 { 114 ptr = ptr->right; 115 while(ptr->leftthread != 1) 116 ptr = ptr->left; 117 } 118 if(ptr != root) 119 printf("[%d]\n",ptr->data); 120 }while(ptr != root); 121 } 122 123 124 int main() 125 { 126 t_btree root = NULL; 127 128 int data[9] = {5,6,4,8,2,3,7,1,9}; 129 root = createtbtree(data,9); 130 printf("线索二叉树的节点内容\n"); 131 printtbtree(root); 132 return 0; 133 }
将前序的表达式二叉树存入数组,然后使用递归创建表达式二叉树,接着输出三种遍历的结果
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 struct tree 5 { 6 char 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 > 7) 19 return NULL; 20 else 21 { 22 newnode = (btree)malloc(sizeof(treenode)); 23 newnode->data = data[pos]; 24 newnode->left = createbtree(data,2*pos); 25 newnode->right = createbtree(data,2*pos + 1); 26 return newnode; 27 } 28 } 29 30 void inorder(btree ptr) 31 { 32 if(ptr != NULL) 33 { 34 inorder(ptr->left); 35 printf("%c",ptr->data); 36 inorder(ptr->right); 37 } 38 } 39 40 void preorder(btree ptr) 41 { 42 if(ptr != NULL) 43 { 44 printf("%c",ptr->data); 45 preorder(ptr->left); 46 preorder(ptr->right); 47 } 48 } 49 50 void postorder(btree ptr) 51 { 52 if(ptr != NULL) 53 { 54 postorder(ptr->left); 55 postorder(ptr->right); 56 printf("%c",ptr->data); 57 } 58 } 59 60 int getvalue(int op,int operand1,int operand2) 61 { 62 switch((char) op) 63 { 64 case '*': return (operand2 * operand1); 65 case '/': return (operand2 / operand2); 66 case '+': return (operand2 + operand1); 67 case '-': return (operand2 - operand2); 68 } 69 } 70 71 int cal(btree ptr) 72 { 73 int operand1 = 0; 74 int operand2 = 0; 75 76 if(ptr->left == NULL && ptr->right == NULL) 77 return ptr->data - 48; 78 { 79 operand1 = cal(ptr->left); 80 operand2 = cal(ptr->right); 81 return getvalue(ptr->data,operand1,operand2); 82 } 83 } 84 85 int main() 86 { 87 btree root = NULL; 88 int result; 89 90 int data[8] = {' ','+','*','*','5','6','4','3'}; 91 root = createbtree(data,1); 92 printf("中序表达式:"); 93 inorder(root); 94 printf("\n前序表达式:"); 95 preorder(root); 96 printf("\n后序表达式:"); 97 postorder(root); 98 result = cal(root); 99 printf("\n表达式结果是:%d\n",result); 100 101 return 0; 102 }