| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 788 人关注过本帖
标题:关于二叉树的查找及四叉树
只看楼主 加入收藏
撒旦
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-6-1
收藏
 问题点数:0 回复次数:0 
关于二叉树的查找及四叉树
#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);
}
其中,注释符代表各种查找的顺序
搜索更多相关主题的帖子: 二叉树 btree newnode struct NULL 
2007-12-26 22:33
快速回复:关于二叉树的查找及四叉树
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016659 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved