#include <stdio.h> #include <stdlib.h> typedef struct TreeNode_t { int data; struct TreeNode_t *left; struct TreeNode_t *right; }TreeNode; /* 前序创建二叉树 */ TreeNode *PreCreatTree() { int num; TreeNode *T; scanf("%d", &num); if (0 == num) { T = NULL; } else { T = (TreeNode *)malloc(sizeof(TreeNode)); T->data = num; T->left = PreCreatTree(); T->right = PreCreatTree(); } return T; } /*删除二叉树*/ TreeNode *FreeTree(TreeNode *T) { if(NULL != T) { FreeTree(T->left); FreeTree(T->right); free(T); T = NULL; } return T; } /*前序遍历*/ void PreTraverse(TreeNode *root) { if (NULL == root) { return; } else { printf("%d--", root->data); PreTraverse(root->left); PreTraverse(root->right); } } /*中序遍历*/ void MidTraverse(TreeNode *root) { if (NULL == root) { return; } else { MidTraverse(root->left); printf("%d--", root->data); MidTraverse(root->right); } } /*后序遍历*/ void LastTraverse(TreeNode *root) { if (NULL == root) { return; } else { LastTraverse(root->left); LastTraverse(root->right); printf("%d--", root->data); } } /*计算节点个数*/ int NodeNum(TreeNode *root) { if (NULL == root) { return 0; } else { return 1 + NodeNum(root->left) + NodeNum(root->right); } } /*计算深度*/ int DeepNum(TreeNode *root) { int left, right; if (NULL == root) { return 0; } else { left = (DeepNum(root->left) + 1); right = (DeepNum(root->right) + 1); if (left >= right) { return left; } else { return right; } } } /*计算叶子节点数*/ int LeafNum(TreeNode *root) { if (NULL == root) { return 0; } else if (root->left == NULL && root->right == NULL) { return 1; } else { return LeafNum(root->left) + LeafNum(root->right); } } /*二叉查找树查找节点*/ TreeNode *SearchTree_Search(TreeNode *root, int data) { if (NULL == root) { return NULL; } TreeNode *p = root; while(NULL != p) { if (data > p->data) { p = p->right; } else if (data < p->data) { p = p->left; } else { return p; } } } /*二叉查找树添加节点*/ TreeNode *SearchTree_Insert(TreeNode *root, int data) { if (NULL == root) { root = (TreeNode *)malloc(sizeof(TreeNode)); root->data = data; return root; } TreeNode *p = root; while (NULL != p) { if (data > p->data) { if (NULL == p->right) { p->right = (TreeNode *)malloc(sizeof(TreeNode)); p->right->data = data; return root; } p = p->right; } else if (data < p->data) { if (NULL == p->left) { p->left = (TreeNode *)malloc(sizeof(TreeNode)); p->left->data = data; return root; } p = p->left; } else { return root; } } } /*二叉查找树删除节点*/ TreeNode *SearchTree_Delete(TreeNode *root, int data) { if (NULL == root) { return NULL; } TreeNode *p = root; TreeNode *p_parent = NULL; /*找到data对应的节点,以及他的父节点*/ while((NULL != p) && (data != p->data)) { p_parent = p; if (data > p->data) { p = p->right; } else if (data < p->data) { p = p->left; } } //printf("%d ", p->data); /*如果他的两个子节点都存在,则找他的右子树的最深左节点(因为是他右子树里最小的)*/ if ((NULL != p->left) && (NULL != p->right)) { TreeNode *free_node = p->right; TreeNode *pp = p; while (NULL != free_node->left) { pp = free_node; free_node = free_node->left; } p->data = free_node->data; p = free_node; p_parent = pp; } /*如果他只有一个节点存在,则让他的父节点直接指向他的那个子节点*/ TreeNode *p_child; if (NULL != p->left) { p_child = p->left; } else if (NULL != p->right) { p_child = p->right; } else { p_child = NULL; } /*如果父节点为空,则data在根节点*/ if (NULL == p_parent) { root = p_child; } else if (p_parent->left == p) { p_parent->left = p_child; } else { p_parent->right = p_child; } /*如果是叶子节点,则直接删去*/ free(p); p = NULL; return root; } int main() { TreeNode *tree; tree = PreCreatTree(); TreeNode *p = SearchTree_Delete(tree, 20); PreTraverse(p); //TreeNode *p = SearchTree_Search(tree, 28); //printf("%d ", p->data); tree = FreeTree(tree); return 0; } /*查找树 20 17 10 0 0 19 0 0 25 21 0 0 28 0 0 */
参考: