// pos 走的路径 值类似 110(左右右) 011 (右右左)
// count 代表走的步数
// flag 代表被替换的结点应该插入在新节点的位置,
//如果是BLEFT 表示插在左边,BRIGHT表示插在右边
int insert(BTree *tree,a data,int pos,int count,int flag)//二叉树插入
{
if (tree == NULL)
return FALSE;
//初始化
BTreeNode *node = (BTreeNode*)malloc
(sizeof(BTreeNode)/
sizeof(char));
node->lchild = NULL;
node->rchild = NULL;
node->data = data;
BTreeNode* parent = NULL;
BTreeNode* current = tree->root;
//移位操作
int way;
while (count >0 && current!= NULL)//count代表级数
{
way = pos & 1;//前进
pos = pos >> 1;//进位
parent = current;
if (way ==BLEFT)
current = current->lchild;
else
current = current->rchild;
count--;
}
//
if (flag == BLEFT)
node->lchild = current;
else
node->rchild = current;
if (parent != NULL)
{
if (way == BLEFT)
parent->lchild=node;
else
parent->rchild=node;
}//上两步均为插入
else
tree->root = node;
}
void r_display(BTreeNode* tree,int pfun,int gap)
{
if (tree == NULL)
return;
int i;
for(i=0;i<gap,i++)
{
printf("%c","-");
}
pfun(tree);//printf("%c\n",tree->data);
if (node->lchild != NULL || node->rchild != NULL)
{
r_display(tree->lchild,pfun,gap+4);
r_display(tree->rchild,pfun,gap+4);
}
}
void display(BTree *tree)//二叉树打印数据
{
if (tree == NULL)
return FALSE;
r_display(tree->root,pfun, 0)
}
int r_delete(BTreeNode* node,int gap,int count)
{
if (node == NULL);
return FALSE;
BTreeNode* current = tree->root;
BTreeNode* parent = NULL;
int way;
while (count >0 && current!= NULL)]
{
way = pos & 1;
pos = pos >> 1;//移位操作
parent = current;
if (way ==BLEFT)
current = current->lchild;
else
current = current->rchild;
count--;
}//同上插入操作
if (parent != 0)
{
if(way == BLEFT)
parent->lchild = NULL;
else
parent->rchild = NULL;
}
else
{
tree->root = NULL;
}//删除
delete(tree,current);
// count 代表走的步数
// flag 代表被替换的结点应该插入在新节点的位置,
//如果是BLEFT 表示插在左边,BRIGHT表示插在右边
int insert(BTree *tree,a data,int pos,int count,int flag)//二叉树插入
{
if (tree == NULL)
return FALSE;
//初始化
BTreeNode *node = (BTreeNode*)malloc
(sizeof(BTreeNode)/
sizeof(char));
node->lchild = NULL;
node->rchild = NULL;
node->data = data;
BTreeNode* parent = NULL;
BTreeNode* current = tree->root;
//移位操作
int way;
while (count >0 && current!= NULL)//count代表级数
{
way = pos & 1;//前进
pos = pos >> 1;//进位
parent = current;
if (way ==BLEFT)
current = current->lchild;
else
current = current->rchild;
count--;
}
//
if (flag == BLEFT)
node->lchild = current;
else
node->rchild = current;
if (parent != NULL)
{
if (way == BLEFT)
parent->lchild=node;
else
parent->rchild=node;
}//上两步均为插入
else
tree->root = node;
}
void r_display(BTreeNode* tree,int pfun,int gap)
{
if (tree == NULL)
return;
int i;
for(i=0;i<gap,i++)
{
printf("%c","-");
}
pfun(tree);//printf("%c\n",tree->data);
if (node->lchild != NULL || node->rchild != NULL)
{
r_display(tree->lchild,pfun,gap+4);
r_display(tree->rchild,pfun,gap+4);
}
}
void display(BTree *tree)//二叉树打印数据
{
if (tree == NULL)
return FALSE;
r_display(tree->root,pfun, 0)
}
int r_delete(BTreeNode* node,int gap,int count)
{
if (node == NULL);
return FALSE;
BTreeNode* current = tree->root;
BTreeNode* parent = NULL;
int way;
while (count >0 && current!= NULL)]
{
way = pos & 1;
pos = pos >> 1;//移位操作
parent = current;
if (way ==BLEFT)
current = current->lchild;
else
current = current->rchild;
count--;
}//同上插入操作
if (parent != 0)
{
if(way == BLEFT)
parent->lchild = NULL;
else
parent->rchild = NULL;
}
else
{
tree->root = NULL;
}//删除
delete(tree,current);
}
int delete(BTree *tree,BTreeNode* node)
{
if (node == NULL || tree == NULL)
return;
delete(tree,node->lchild);
delete(tree,node->rchild);
free(node);
}
int delete2(BTree *tree)//二叉树删除节点
{
if (tree == NULL)
return FALSE;
r_delete(tree->root,0);
}