二叉树的基本应用及其他

// 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);

}

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);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值