#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define TREE_TYPE int
typedef struct tree_node {
TREE_TYPE value;
struct tree_node *left;
struct tree_node *right;
}TreeNode;
/*
*指向根节点的指针,static 防止其他文件修改
*/
//static TreeNode *tree;
void insert(TreeNode* tree,TREE_TYPE value)
{//good taste
TreeNode *current;
TreeNode **link;
link = &tree; //根节点
/*持续查找,进入合适的子树*/
while((current=*link)!=NULL)
{
if(value < current->value)
link = ¤t->left;
else{
assert(value != current->value);
link = ¤t->right;
}
}
current = (TreeNode*)malloc(sizeof(struct tree_node));
assert(current != NULL);
current->value = value;
current->left = NULL;
current->right =NULL;
*link = current;
}
void insert2(TreeNode* tree,TREE_TYPE value)
{ //bad taste
TreeNode *current=tree;
TreeNode *newp=NULL;
TreeNode *last=tree;
while(current!=NULL && current->value!=value)
{
last=current;
if(value<current->value)
{
current=current->left;
}
else
{
assert(value != current->value);
current=current->right;
}
}
newp = (TreeNode*)malloc(sizeof(struct tree_node));
assert(newp!=NULL);
newp->value=value;
newp->left=NULL;
newp->right=NULL;
if(value<last->value)
last->left=newp;
else
last->right=newp;
}
TREE_TYPE* find(TreeNode* tree,TREE_TYPE value)
{
TreeNode *current;
current = tree;
while(current!=NULL && current->value != value)
{
if(value < current->value)
current = current->left;
else
current = current->right;
}
if(current!=NULL)
return ¤t->value;
else
return NULL;
}
int count_nodes(TreeNode *tree);
void destroy_tree(TreeNode **tree);
void delete_node(TreeNode *tree,TREE_TYPE value);
void print_tree(TreeNode *tree);
int main(int argc,char **argv)
{
TreeNode* current=NULL;
TreeNode* tree=(TreeNode*)malloc(sizeof(TreeNode));;
tree->value=54;
tree->left=NULL;
tree->right=NULL;
int i=0;
//for(i=0;i<9;i++)
insert2(tree,36);
insert2(tree,72);
insert2(tree,22);
insert2(tree,41);
insert2(tree,61);
insert2(tree,80);
insert2(tree,16);
insert2(tree,25);
insert2(tree,40);
insert2(tree,51);
insert2(tree,73);
printf("node num:%d\n",count_nodes(tree));
//destroy_tree(&tree);
//delete_node(tree,36);
print_tree(tree);
// current=tree;
// for(;current!=NULL;current=current->right)
// printf("a %d\n",current->value);
printf("node num:%d\n",count_nodes(tree));
return 0;
}
int count_nodes(TreeNode *tree)
{
if(tree==NULL)
return 0;
return 1+count_nodes(tree->left)+count_nodes(tree->right);
}
void destroy_tree(TreeNode **tree)
{
if(*tree!=NULL)
{
destroy_tree(&(*tree)->left);
destroy_tree(&(*tree)->right);
free(*tree);
*tree=NULL;
}
}
/*
*删除一个节点
*/
void delete_node(TreeNode *tree,TREE_TYPE value)
{
TreeNode **link=&tree;
TreeNode *current=NULL;
while((current=*link)!=NULL && current->value!=value)
{
if(value<current->value)
link=¤t->left;
else
link=¤t->right;
}
if(current==NULL)
printf("not find value:%d\n",value);
assert(current!=NULL);//判断是否找到了value
// find value in the tree
if(current->left==NULL && current->right==NULL)
{
*link = NULL;
free(current);
}
else if(current->left==NULL || current->right==NULL)
{
if(current->left != NULL)
*link = current->left;
else
*link = current->right;
free(current);
}
else //current->left!=NULL && current->right!=NULL
{
TreeNode *this_child;
TreeNode *next_child;
this_child=current->left;
link = ¤t->left;
next_child=this_child->right;
while(next_child!=NULL)
{
link = &this_child->right;
this_child=next_child;
next_child=this_child->right;
}
value=this_child->value;
current->value=value;
*link=this_child->left;
free(this_child);
}
}
void print_tree(TreeNode *tree)
{
if(tree!=NULL)
{
printf("value:%d\n",tree->value);
print_tree(tree->left);
print_tree(tree->right);
}
}
//求叶子节点数
int sum=0;
void getLeafNum(TreeNode *tree)
{
if(tree==NULL)
return;
if(tree->left==NULL && tree->right==NULL)//叶子为左右孩子都为NULL
sum++;
getLeafNum(tree->left);
getLeafNum(tree->right);
}
//求树的高度
int Depth(TreeNode *tree)
{
int ldepth=0,rdepth=0;
if(tree==NULL)
return 0;
ldepth=Depth(tree->left);
rdepth=Depth(tree->right);
return 1+(ldepth>rdepth ? ldepth : rdepth);
}
//复制二叉树
TreeNode * copy_tree(TreeNode *tree)
{
TreeNode *newT=(TreeNode*)malloc(sizeof(TreeNode));
TreeNode *newl=NULL;
TreeNode *newr=NULL;
if(newT==NULL || tree==NULL)
return NULL;
newl = copy_tree(tree->left);
newr = copy_tree(tree->right);
newT->value = tree->value;
newT->left = newl;
newT->right = newr;
return newT;
}
二叉树 相关操作
最新推荐文章于 2022-09-12 17:58:40 发布