树的基本操作有创建,插入,删除,以及各种遍历的应用,如:利用后序遍历求高度,利用前序遍历求层数的结点
基本算法思路:创建二叉树函数参数必须接受二级指针!如果使用同级指针,无法返回创建后的结果,利用递归malloc函数完成创建
插入(检索树):根据检索树特性,在插入必须判断根节点左右两边的值来完成插入
删除:如果删除的是节点是叶结点,直接free。如果有一个子树,将其父节点指向删除节点的儿子。如果两个子树,遍历右节点找到最大的data,将他的data复制给删除data,然后删除该节(重复第一二种情况)
更多应用举例请看代码(普通二叉树,检索树)
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<iostream> #include<conio.h> struct tree1 { char data;//数据域 struct tree1* light;//指向左孩子的指针 struct tree1* right;//指向右孩子的指针 }; char ch; struct tree1* root; struct tree1 *del_node1= NULL;//需要删除的节点 void create(struct tree1** p);//创造而叉树 void create_tree();//创造检索树 void front(struct tree1* p);//前序遍历 void midder(struct tree1* p);//中序遍历 void post(struct tree1* p);//后序遍历 void toot(struct tree1* p);//以括号的形式输出二叉树 //int h(struct tree1* p);//求该节点的高度 struct tree1* enter_node(char a, struct tree1** p);//插入检索树的节点 struct tree1* find_father(struct tree1* p);//返回父节点的指针,若无寻找不到则返回空指针,函数不接受根节点! struct tree* find_rmax(struct tree1* p);//寻找右节点中的最大值 int find_layer(struct tree1* p,char a,int n);//寻找树中指定内容 并返回层数 int find_node(struct tree1* p, char a);//如果有结点返回1,没有返回0 int layer = 0;//接收节点层数 void main() { root = NULL; printf("输入#代表此节点为终端结点\n"); create(&root); front(root); printf("\n"); midder(root); printf("\n"); post(root); printf("\n"); toot(root); printf("\n"); printf("%d\n", find_layer(root, 'H', 1)); } void create(struct tree1** p) { std::cin >> ch; if (ch == '#') { *p = NULL; return; } else { *p = (struct tree1*)malloc(sizeof(struct tree1)); (*p)->data = ch; create(&((*p)->light)); create(&((*p)->right)); } } void front(struct tree1* p) { if (p != NULL) { printf("%c", p->data); front(p->light); front(p->right); } } void midder(struct tree1* p) { if (p != NULL) { midder(p->light); printf("%c", p->data); midder(p->right); } } void post(struct tree1* p) { if (p != NULL) { post(p->light); post(p->right); printf("%c", p->data); } } void toot(struct tree1* p) { if (p == NULL) { printf("0"); return; } printf("%c", p->data); if (p->light == NULL && p->right == NULL) { return; } printf("("); toot(p->light); printf(","); toot(p->right); printf(")"); } void create_tree() { struct tree1* p=NULL; int hj = 0; char c; root = (struct tree1*)malloc(sizeof(struct tree1)); //自己赋值根节点的数据域 std::cin >> c; root->data = c; root->light = NULL; root->right = NULL; //以#结束创建 while (1) { std::cin >> c; if (c == '#') break; if (hj == 0) { hj++; p=enter_node(c, &(root)); } else { p= enter_node(c, &p); } } } struct tree1* enter_node(char a,struct tree1 **p) { if (*p == NULL) return NULL; //插入 if (((*p)->light) == NULL && ((*p)->right) == NULL) { struct tree1* new1 = (struct tree1*)malloc(sizeof(struct tree1)); new1->light = new1->right = NULL; new1->data = a; if (strcmp(&a, &((*p)->data)) > 0) { ((*p)->right) = new1; } else { ((*p)->light) = new1; } return new1; } if (strcmp(&a, &(*p)->data) > 0) { enter_node(a, &((*p)->right)); } else { enter_node(a, &((*p)->light)); } } void del_node(struct tree1* p) { struct tree1* father;//临时的存储的父节点 struct tree1** father1;//真正的父节点 p = del_node1; if (p->light == NULL || p->right == NULL)//删除叶子结点 free(p); if (p->light != NULL && p->right == NULL || p->right != NULL && p->light == NULL)//只有一个结点 { father = find_father(root);//接收该节点的父节点 father1 = &father; //判断是父节点的哪个方向的儿子 if (father->light == p) { if (p->light == NULL) { (*father1)->light = p->right; } else { (*father1)->light = p->light; } } else { if (p->light == NULL) { (*father1)->right = p->right; } else { (*father1)->right = p->light; } } } } struct tree1* find_father(struct tree1* p) { if (p == NULL) { return NULL; } if (p->light == del_node1 || p->right == del_node1) { return p; } find_father(p->light); find_father(p->right); } int find_layer(struct tree1* p, char a, int n) { int c,g; int b=0;//判断是否有该节点 if (p == NULL) { return 0 ; } if(p->data==a) { return n; } c=find_layer(p->light, a, n + 1); g=find_layer(p->right, a, n + 1); if (c >= g) { return c; } else { return g; } } int find_node(struct tree1* p, char a) { if (p->data == a) { return 1; } if (p = NULL) { return 0; } int c, g; c = find_node(p->light, a); g = find_node(p->right, a); if (c >= g) { return c; } else { return g; } }