关于二叉树的查找及四叉树
#include<stdio.h>#include<stdlib.h>
struct tree
{
int data;
struct tree *left;
struct tree *right;
};
typedef struct tree treenode;
typedef treenode *btree;
btree insertnode(btree root,int value)
{
btree newnode;
btree now;
btree parent;
newnode=(btree)malloc(sizeof(treenode));
newnode->data=value;
newnode->left=NULL;
newnode->right=NULL;
if(root==NULL)
{
return newnode;
}
else
{
now=root;
while(now!=NULL)
{
parent=now;
if(value%2==1/*now->data>value*/)
now=now->left;
else
now=now->right;
}
}
if(value%2==1/*parent->data>value*/)
parent->left=newnode;
else
parent->right=newnode;
return root;
}
btree createbtree(int *data,int len)
{
btree root=NULL;
int i;
for(i=0;i<len;i++)
{
root=insertnode(root,data[i]);
}
return root;
}
void preorder(btree p)
{
if(p!=NULL)
{
printf("%d\n",p->data);
preorder(p->left);
preorder(p->right);
}
}
void main()
{
btree root=NULL;
int data[5]={1,2,3,4,5};
root=createbtree(data,5);
printf("the idea are\n");
preorder(root);
}
#include<stdio.h>
#include<stdlib.h>
struct tree
{
int data;
struct tree *first;
struct tree *second;
struct tree *third;
struct tree *fourth;
};
typedef struct tree treenode;
typedef treenode *btree;
btree insertnode(btree root,int value)
{
btree newnode;
btree now;
btree parent;
newnode=(btree)malloc(sizeof(treenode));
newnode->data=value;
newnode->first=NULL;
newnode->second=NULL;
newnode->third=NULL;
newnode->fourth=NULL;
if(root==NULL)
{
return newnode;
}
else
{
now=root;
while(now!=NULL)
{
parent=now;
if(value%3==1)
now=now->first;
else if(value%5==1)
now=now->second;
else if(value%5==1)
now=now->third;
else
now=now->fourth;
}
}
if(value%3==1)
parent->first=newnode;
else if(value%5==1)
parent->second=newnode;
else if(value%7==1)
parent->third=newnode;
else
parent->fourth=newnode;
return root;
}
btree createbtree(int *data,int len)
{
btree root=NULL;
int i;
for(i=0;i<len;i++)
{
root=insertnode(root,data[i]);
}
return root;
}
void preorder(btree p)
{
if(p!=NULL)
{
printf("%d\n",p->data);
preorder(p->first);
// printf("%d\n",p->data);
preorder(p->second);
// printf("%d\n",p->data);
preorder(p->third);
preorder(p->fourth);
// printf("%d\n",p->data);
}
}
void main()
{
btree root=NULL;
int data[20]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
root=createbtree(data,20);
printf("the idea are\n");
preorder(root);
}
其中,注释符代表各种查找的顺序