#include "stdlib.h" #include "stdio.h" #include <iostream> using namespace std; typedef int data_type; typedef struct bst_node{ data_type data; struct bst_node * lchild, *rchild; }bst_t,*bst_p; static bst_p search_bst_for_insert(bst_p* root,data_type key) { bst_p s = *root; bst_p p = *root; while (p) {//寻找能给这个节点的位置,用s保存 s = p; if (p->data==key) { return NULL; } p = (key < p->data) ? p->lchild : p->rchild; } return s; } //插入节点 void insert_bst_node(bst_p*root, data_type data) { bst_p s, p=NULL; s = (bst_p)malloc(sizeof(struct bst_node)); s->data = data; s->lchild = s->rchild = NULL;//需要插入的指针 if (*root == NULL)//第一次赋值给root *root = s; else { p = search_bst_for_insert(root, data);//寻找能给这个节点的位置 if (p == NULL) { free(s); return; } if (data < p->data) {//对比本节点数据,判断存储在左节点还是右节点 p->lchild = s; } else { p->rchild = s; } } } //查询节点 int pre_order_traverse(bst_p root,int(*visit)(data_type data)) { if (root) { if (visit(root->data)) if (pre_order_traverse(root->lchild,visit)) if (pre_order_traverse(root->rchild, visit))//查询右节点 return 1; return 0; } else return 1; } int post_order_traverse(bst_p root, int(*visit)(data_type data)) { if (root) { if (post_order_traverse(root->lchild, visit)) if (visit(root->data)) if (post_order_traverse(root->rchild, visit)) return 1; return 0; } else return 1; } static int print(data_type data) { printf("%d ", data); return 1; } /* 删除某个结点后依然要保持二叉查找树的特性。例子中的删除过程如下: a、若删除点是叶子结点,则设置其双亲结点的指针为空。 b、若删除点只有左子树,或只有右子树,则设置其双亲结点的指针指向左子树或右子树。 c、若删除点的左右子树均不为空,则: 1)、查询删除点的右子树的左子树是否为空,若为空,则把删除点的左子树设为删除点的右子树的左子树 2)、若不为空,则继续查询左子树,直到找到最底层的左子树为止。 */ void delete_bst_node(bst_p *root, data_type data) { bst_p p = *root; bst_p parent = NULL; bst_p s = NULL; if (p->data == data) { if (!p->lchild && !p->rchild) { *root = NULL; free(p); } else if (!p->rchild) {//右子节点为null *root = p->lchild; free(p); } else if (!p->lchild) {//左子节点为null *root = p->rchild; free(p); } else { s = p->rchild; if (!s->lchild)//左子节点为null s->lchild = p->lchild; else { while (s->lchild) { parent = s; s = s->lchild; } parent->lchild = s->rchild; s->lchild = p->lchild; s->rchild = p->rchild; } *root = s; free(p); } } else if (data>p->data) { delete_bst_node(&(p->rchild), data); } else if (data<p->data) { delete_bst_node(&(p->lchild), data); } } //查找节点 bst_p tree_find(bst_p root, int e) { while (root != NULL && root->data != e) { if (root->data > e) root = root->lchild; else root = root->rchild; } return root; } void main() { int i, num; bst_p root = NULL; data_type arr[] = {45,24,53,12,37,93}; num = 6; for (int i = 0; i < num; i++) { insert_bst_node(&root,arr[i]); } bst_p find= tree_find(root,12); if (find) { cout << "找到了该节点:" << find->data << endl; } printf("\npre order traverse: "); pre_order_traverse(root, print); printf("\npost order traverse: "); post_order_traverse(root, print); delete_bst_node(&root, 45); printf("\npre order traverse: "); pre_order_traverse(root, print); printf("\npost order traverse: "); post_order_traverse(root, print); getchar(); } 参考自:http://blog.csdn.net/npy_lp/article/details/7426431
平衡二叉树(AVL树)
最新推荐文章于 2024-03-27 12:12:39 发布