#include <stdio.h>
#include <stdlib.h>
#include "binTree.h"
Pnode createNode(int data)
{
Pnode p = (Pnode)malloc(sizeof(Node));
if (NULL == p)
{
return NULL;
}
p->data = data;
p->left = NULL;
p->right = NULL;
return p;
}
//插入--非递归实现
int treeInsertData(Pnode* proot, int data)
{
if (NULL == proot)
{
return -1;
}
Pnode root = *proot;
Pnode pNew = createNode(data);
if (NULL == pNew)
{
return -1;
}
Pnode p = root;
Pnode temp = NULL;
if (NULL == root)
{
*proot = pNew;
return 0;
}
else
{
while (p != NULL)
{
if (data < p->data)//小于
{
temp = p;
p = p->left;
}
else if (data>p->data)//大于
{
temp = p;
p = p->right;
}
else//相等
{
return 1;
}
}
if (data < temp->data)
{
temp->left = pNew;
}
else
{
temp->right = pNew;
}
}
return 0;
}
//插入--递归实现
int treeInsertData1(Pnode* proot, int data)
{
if (NULL == proot)
{
return -1;
}
Pnode root = *proot;
Pnode pNew = createNode(data);
if (NULL == pNew)
{
return -1;
}
Pnode p = root;
Pnode temp = NULL;
if (NULL == root)
{
*proot = pNew;
return 0;
}
else
{
if (data < p->data)//小于
{
treeInsertData(&p->left, data);
}
else if (data>p->data)//大于
{
treeInsertData(&p->right, data);
}
else//相等
{
return 1;
}
}
return 0;
}
//删除
int treeDeleteData(Pnode *proot, int data)
{
if (NULL == proot)
{
return -1;
}
Pnode root = *proot;
Pnode pre = NULL;
//Pnode pre = root;
Pnode p = root;
if (NULL == p)
{
return -2;
}
while (NULL != p&&data != p->data)
{
if (data < p->data)
{
pre = p;
p = p->left;
}
else
{
pre = p;
p=p->right;
}
}
if (NULL == p)//没找到该数据
{
return 0;
}
Pnode temp = p;
if (NULL == pre)//删除根节点
{
if (p->left != NULL)
{
*proot = p->left;
}
else
{
*proot = p->right;
}
if (*proot == p->left)
{
if (p != NULL)
{
while (p != NULL)//捋至末尾续上
{
pre = p;
p = p->right;
}
if (NULL != pre->right)
{
pre->right = (*proot)->right;
}
}
//while (p != NULL)//捋至末尾续上
//{
// pre = p;
// p = p->right;
//}
//if (NULL != pre)
//{
// pre->right = (*proot)->right;
//}
//
}
free(temp);
return 0;
}
Pnode left = p->left;
Pnode right = p->right;
if (left != NULL&&NULL == right)//左非空右空
{
if (pre->left==p)
{
pre->left = left;
}
else
{
pre->right = left;
}
}
else if (right != NULL&&NULL == left)//左空右非空
{
if (pre->left == p)
{
pre->left = right;
}
else
{
pre->right = right;
}
}
else//左右皆非空||左右皆空---默认左孩子上浮
{
if (pre->left == p)
{
pre->left = left;
}
else
{
pre->right = left;
}
p = left;
if (p != NULL)
{
while (p != NULL)//捋至末尾续上
{
pre = p;
p = p->right;
}
pre->right = right;
}
}
free(temp);
return 0;
}
int treeDestroy(Pnode *proot)
{
if (NULL == proot)
{
return -1;
}
Pnode root = *proot;
if (NULL == root)
{
return 0;
}
if (NULL==root->left&&NULL==root->right)
{
free(root);
}
else if (NULL == root->left)
{
//treeDestroy(&root->right);//取root的地址是不对的。。应该取*poot
treeDestroy(&(*proot)->right);
}
else
{
treeDestroy(&(*proot)->left);
}
*proot = NULL;
}
//中序遍历
int treeInOrderTraverse(Pnode root)
{
if (NULL == root)
{
return 0;
}
else
{
treeInOrderTraverse(root->left);
printf("%d ", root->data);
treeInOrderTraverse(root->right);
}
return 0;
}
//先序遍历
int treePreOrderTraverse(Pnode root)
{
if (NULL == root)
{
return 0;
}
else
{
printf("%d ", root->data);
treePreOrderTraverse(root->left);
treePreOrderTraverse(root->right);
}
return 0;
}
//后序遍历
int treePostOrderTraverse(Pnode root)
{
if (NULL == root)
{
return 0;
}
else
{
treePostOrderTraverse(root->left);
treePostOrderTraverse(root->right);
printf("%d ", root->data);
}
return 0;
}
// 查询
Pnode treeFindData(Pnode root, int data)
{
if (NULL == root)
{
return NULL;
}
Pnode p = root;
while (p)
{
if (data > p->data)
{
p = p->right;
}
if (data<p->data)
{
p = p->left;
}
if (data==p->data)
{
return p;
}
}
return NULL;
}
//求树的高度
int treeHeght(Pnode root)
{
if (NULL == root)
{
return 0;
}
int height = 0;
/*if (NULL == root->left&&NULL == root->right)
{
return 1;
}*/
if (treeHeght(root->left) > treeHeght(root->right))
{
height = treeHeght(root->left) + 1;
}
else
{
height = treeHeght(root->right) + 1;
}
return height;
}
void test1()//treeInsertData
{
Pnode root = NULL;
int a[] = { 1,6, 4, 3, 8, 7, 2, 9, 5 };
int len = sizeof(a) / sizeof(int);
for (int i = 0; i < len; i++)
{
treeInsertData1(&root, a[i]);
}
treeInOrderTraverse(root);
treeDeleteData(&root, 1);
printf("\n----------------------------\n");
treeInOrderTraverse(root);
treeDeleteData(&root, 2);
printf("\n----------------------------\n");
treeInOrderTraverse(root);
treeDeleteData(&root, 3);
printf("\n---------------------------\n");
treeInOrderTraverse(root);
treeDeleteData(&root, 4);
printf("\n----------------------------\n");
treeInOrderTraverse(root);
treeDeleteData(&root, 5);
printf("\n----------------------------\n");
treeInOrderTraverse(root);
treeDeleteData(&root, 6);
printf("\n----------------------------\n");
treeInOrderTraverse(root);
treeDeleteData(&root, 7);
printf("\n----------------------------\n");
treeInOrderTraverse(root);
treeDeleteData(&root, 8);
printf("\n----------------------------\n");
treeInOrderTraverse(root);
treeDeleteData(&root, 9);
printf("\n----------------------------\n");
treeInOrderTraverse(root);
}
void test2()//height
{
Pnode root = NULL;
int a[] = { 1, 6, 4, 3, 8, 7, 2, 9, 5 };
int len = sizeof(a) / sizeof(int);
for (int i = 0; i < len; i++)
{
treeInsertData1(&root, a[i]);
}
treeInOrderTraverse(root);
printf("\n树的高度:%d\n", treeHeght(root));
treeDestroy(&root);
treeInOrderTraverse(root);
}
void main()
{
//test1();
test2();
getchar();
}
二叉树,建立,查询,删除,插入的简单实现
最新推荐文章于 2023-01-14 16:12:45 发布