二叉树

#include<stdlib.h>
struct tree //澹版槑鏍戠殑缁撴瀯
{
struct tree *left;
int data;
struct tree *right;
};


typedef struct tree treenode;
type treenode *b_tree; //澹版槑浜屽弶鏍戦摼琛?
//鎻掑叆浜屽弶鏍戠殑鑺傜偣
b_tree insert_node(b_tree root,int node)
{
b_tree newnode;
b_tree currentnode;
b_tree parentnode;


newnode=(b_tree)malloc(sizeof(treenode)); //寤虹珛鏂拌妭鐐圭殑鍐呭瓨绌洪棿
newnode->data=node;
newnode->right=NULL;
newnode->left=NULL;

if(root=NULL)
{
return newnode;
}
else

currentnode=root;
while(currentnode!=NULL)

parentnode=currentnode;

if( currentnode->data>node)
{
  currentnode=currentnode->left;
}  
else

   currentnode=currentnode->right;

}
if(parentnode->data>node)
{
parentnode->left=newnode;
}
  else 
  {
    parentnode->right=newnode;
  }
   }
return root;
}
// 寤虹珛浜屽弶鏍?b_tree create_btree(int *data,int len)
{
b_tree root=NULL;
int i;
for(i=0;i<len;i++)
root=insert_node(root,data[i]);
return root;
}
//浜屽弶鏍戜腑搴忛亶鍘?void inorder(b_tree point)
{
if(point!=NULL)
{
inorder(point->left);
printf("%d",point->data);
inorder(point->right);
}
}


//涓荤▼搴?void main( )
{
b_tree root=NULL;
int i,index;
int value;
int nodelist[20];
printf("\n pleaase input the elements of binary tree(exit for 0 ):\n");
index=0;


//璇诲彇鏁板€煎瓨鍒版暟缁勪腑
scanf("%d",&value);


while(value!=0)
{
nodelist[index]=value];
index=index+1;
scanf("%d",&value);
}
//寤虹珛浜屽弶鏍?root=create_btree(nodelist,index);


//涓簭閬嶅巻浜屽弶鏍?printf("\nThe inorder traversal result is :");
inorder(root);
printf("\n");

}





#include <stdio.h>
#include <stdlib.h>


struct tree
{
struct tree *left;
struct tree *right;
int data;


};
typedef struct tree treenode;
typedef treenode *btree;
void printnode(btree root);
btree insertnode(btree root,int num)
{
btree currentnode;
btree parentnode;
btree newnode;

newnode = (btree)malloc(sizeof(treenode));
newnode->data = num;
newnode->left = NULL;
newnode->right = NULL;

if(root == NULL)
{

return newnode;
}
else
{
currentnode = root;

while(currentnode != NULL)
{
parentnode = currentnode;

if(currentnode->data < num)
{
currentnode = currentnode->right;
}
else
{
currentnode = currentnode->left;
}
}
if(parentnode->data < num)
{
parentnode->right = newnode;

}
else
{
parentnode->left = newnode;
}


}



return root;
}
btree createnode(btree root)
{
int i;
int a[9] = {5,6,4,8,2,3,7,1,9};

for(i=0;i<9;i++)
{
root = insertnode(root,a[i]);
}

return root;
}
btree findnode(btree ptr,int value,int *pos)
{
btree previous;
previous = ptr;
*pos = 0;


while(ptr != NULL)
{
if(ptr->data == value)
{
return previous;
}
else
{
previous = ptr;
}
if(ptr->data > value)
{
ptr = ptr->left;
*pos = -1;
}
else
{
ptr = ptr->right;
*pos = 1;
}
}

return NULL;
}
void printnode(btree ptr)
{
if(ptr!= NULL)
{
printnode(ptr->left);
printf("[%2d]\n",ptr->data);
printnode(ptr->right);
}


}


btree deletenode(btree root,int value)
{
btree previous;
btree ptr;
btree next;
int pos;

previous = findnode(root,value,&pos);

if(previous == NULL)
{


return root;
}
previous = findnode(root,value,&pos);

if(previous == NULL)
{
return root;
}

switch(pos)
{
case -1: ptr = previous->left;break;
case 1:  ptr = previous->right;break;
case 0: ptr = previous;break;


if(ptr->left == NULL)
{
if(previous != ptr)
{
previous->right = ptr->right;
}
else
{
root = root->right;
}
free(ptr);
return root;
}
if(ptr->right == NULL)
{
if(previous != ptr)
{
previous->left = ptr->left;
}
else
{
root = root->left;
}
free(ptr);
return root;
}
previous = ptr;
next = ptr->left;
while(next->right != NULL)
{
previous = next;
next = next->right;
}
ptr->data = next->data;
if(previous->left == next)
{
previous->left = next->left;
}
else
{
previous->right = next->right;
}
free(next);
return root;
}


int main(void)
{
btree root = NULL;

root = createnode(root);

printnode(root);
printf("************\n");
deletenode(root,5);

printnode(root);

return 0;
}


线索二叉树:


#include <stdio.h>
#include <stdlib.h>


struct t_tree
{
int data;
int leftthread;
int rightthread;
struct t_tree *left;
struct t_tree *right;
};
typedef struct t_tree treenode;
typedef treenode* t_btree;


t_btree insertnode(t_btree root,int value)
{
t_btree newnode;
t_btree previous;
t_btree parent;
t_btree current;
int pos;

newnode = (t_btree)malloc(sizeof(treenode));

newnode->data = value;
newnode->leftthread = 1;
newnode->rightthread = 1;
newnode->left = NULL;
newnode->right = NULL;

current = root->right;
pos = 0;

if(current == NULL)
{
root->right =  newnode;
newnode->right = root;
newnode->left = root;
return root;
}
parent = root;

while(current != NULL)
{
if(current->data > value)
{
if(pos != -1)
{
pos = -1;
previous = parent;
}
parent = current;

if(current->leftthread == 0)
{
current = current->left;
}
else
{
current = NULL;
}
}
else
{
if(pos != 1)
{
pos = 1;
previous = parent;

}
parent = current;

if(current->rightthread == 0)
{
current = current->right;
}
else
{
current = NULL;
}
}
}

if(parent->data >value)
{
parent->left = newnode;
parent->leftthread = 0;
newnode->left = previous;
newnode->right = parent;

}
else
{
parent->right = newnode;
parent->rightthread = 0;
newnode->left = parent;
newnode->right = previous;
}

return root;
}
t_btree createbtree(int *data,int len)
{
t_btree root = NULL;
int i;

root = (t_btree)malloc(sizeof(treenode));
root->left = NULL;
root->right = NULL;
root->leftthread = 1;
root->rightthread = 0;

for(i=0;i<len;i++)
{
root = insertnode(root,data[i]);
}

return root;
}


void printbtree(t_btree root)
{
t_btree ptr;
ptr = root;


do
{
if(ptr->rightthread == 1)
{
ptr = ptr->right;
}
else
{
ptr = ptr->right;
while(ptr->leftthread != 1)
{
ptr = ptr->left;
}


}
if(ptr != root)
{
printf("[%d]\n",ptr->data);
}

}while(ptr != root);



}
int main(void)
{
t_btree root = NULL;
int data[9] = {5,6,4,8,2,3,7,1,9};
root = createbtree(data,9);
printbtree(root);


return 0;
}









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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值