⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kdtree.c

📁 数据结构与算法分析(C语言描述)的源代码 里面的代码的质量非常高
💻 C
字号:
        #include <stdio.h>        #include <stdlib.h>        #include "fatal.h"        typedef int ElementType;        typedef ElementType ItemType[ 2 ];        struct KdNode;        typedef struct KdNode *Position;        typedef struct KdNode *KdTree;        struct KdNode        {            ItemType Data;            KdTree   Left;            KdTree   Right;        };/* START: fig12_43.txt */        static KdTree        RecursiveInsert( ItemType Item, KdTree T, int Level )        {            if( T == NULL )            {                T = malloc( sizeof( struct KdNode ) );                if( T == NULL )                    FatalError( "Out of space!!!" );                T->Left = T->Right = NULL;                T->Data[ 0 ] = Item[ 0 ];                T->Data[ 1 ] = Item[ 1 ];            }            else            if( Item[ Level ] < T->Data[ Level ] )                T->Left = RecursiveInsert( Item, T->Left, !Level );            else                T->Right = RecursiveInsert( Item, T->Right, !Level );            return T;        }        KdTree        Insert( ItemType Item, KdTree T )        {            return RecursiveInsert( Item, T, 0 );        }/* END *//* START: fig12_44.txt */        /* Print items satisfying */        /* Low[ 0 ] <= Item[ 0 ] <= High[ 0 ] and */        /* Low[ 1 ] <= Item[ 1 ] <= High[ 1 ] */        static void        RecPrintRange( ItemType Low, ItemType High,                       KdTree T, int Level )        {            if( T != NULL )            {                if( Low[ 0 ] <= T->Data[ 0 ] &&                                T->Data[ 0 ] <= High[ 0 ] &&                                Low[ 1 ] <= T->Data[ 1 ] &&                                T->Data[ 1 ] <= High[ 1 ] )                    printf( "(%d,%d)\n",                                T->Data[ 0 ], T->Data[ 1 ] );                if( Low[ Level ] <= T->Data[ Level ] )                    RecPrintRange( Low, High, T->Left, !Level );                if( High[ Level ] >= T->Data[ Level ] )                    RecPrintRange( Low, High, T->Right, !Level );            }        }        void        PrintRange( ItemType Low, ItemType High, KdTree T )        {            RecPrintRange( Low, High, T, 0 );        }/* END */main( ){    KdTree T;    ItemType It, L, H;    int i;    printf( "Starting program\n" );    T = NULL;    for( i = 300; i < 370; i++ )    {        It[ 0 ] = i; It[ 1 ] = 2500 - i;        T = Insert( It, T );    }    printf( "Insertions complete\n" );    i = 1;    L[ 0 ] = 70; L[ 1 ] = 2186; H[ 0 ] = 1200; H[ 1 ] = 2200;    PrintRange( L, H, T );    printf( "Done...\n" ) ;    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -