配置ubuntu系統 sudo免密碼
阿新 • • 發佈:2021-01-01
中序遍歷
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 inorder(btree ptr) 60 { 61 if(ptr != NULL) 62 { 63 inorder(ptr->left); 64 printf("[%2d]\n",ptr->data); 65 inorder(ptr->right); 66 } 67 } 68 69 int main() 70 { 71 btree root = NULL; 72 73 int data[9] = {5,6,4,8,2,3,7,1,9}; 74 root = createbtree(data,9); 75 printf("樹的節點內容 \n"); 76 inorder(root); 77 return 0; 78 }
前序遍歷
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 preorder(btree ptr) 60 { 61 if(ptr != NULL) 62 { 63 printf("[%2d]\n",ptr->data); 64 preorder(ptr->left); 65 preorder(ptr->right); 66 } 67 } 68 69 int main() 70 { 71 btree root = NULL; 72 73 int data[9] = {5,6,4,8,2,3,7,1,9}; 74 root = createbtree(data,9); 75 printf("樹的節點內容 \n"); 76 preorder(root); 77 return 0; 78 }
後序遍歷
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 postorder(btree ptr) 60 { 61 if(ptr != NULL) 62 { 63 postorder(ptr->left); 64 postorder(ptr->right); 65 printf("[%2d]\n",ptr->data); 66 } 67 } 68 69 int main() 70 { 71 btree root = NULL; 72 73 int data[9] = {5,6,4,8,2,3,7,1,9}; 74 root = createbtree(data,9); 75 printf("樹的節點內容 \n"); 76 postorder(root); 77 return 0; 78 }