二叉尋找樹 – C語言實現(摘自資料結構與演算法分析 C語言描述)

來源:互聯網
上載者:User
一、概述        二叉樹的一個重要的應用是它們在尋找中的使用。使二叉樹成為尋找樹的性質是,對於樹中的每個結點 X,它的左子樹中所有關鍵字值小於X的關索引值,而它的右子樹中所有關鍵字大於X的關索引值。在圖1中,左邊的樹是二叉尋找樹,但右邊的樹則不是(想一想為什麼)。圖1 兩棵二叉樹(只有左邊的樹是尋找樹)二、實現
        因為二叉樹最多有兩個兒子,所以我們可以用指標直接指向它們。樹節點的聲明在結構上類似於雙鏈表的聲明,在聲明中,一個節點就是由Key(關鍵字)資訊加上兩個指向其他節點的指標(Left和Right)組成的結構。檔案名稱:tree.h
#ifndef _Tree_Htypedef int ElementType;struct TreeNode;typedef struct TreeNode *Position;typedef struct TreeNode *SearchTree;SearchTree MakeEmpty( SearchTree T );Position Find( ElementType X, SearchTree T );Position FindMin( SearchTree T );Position FindMax( SearchTree T );SearchTree Insert( ElementType X, SearchTree T );SearchTree Delete( ElementType X, SearchTree T );ElementType Retrieve( Position P );void PrintElement( SearchTree T );void PreOrder( SearchTree T );void InOrder( SearchTree T );void PostOrder( SearchTree T );#endif /* Tree_H */

檔案名稱:tree.c

#include "fatal.h"#include "tree.h"struct TreeNode{ElementType Element;SearchTree Left;SearchTree Right;};SearchTree MakeEmpty( SearchTree T ){if ( T != NULL ){MakeEmpty( T->Left );MakeEmpty( T->Right );free( T );}return NULL;}PositionFind( ElementType X, SearchTree T ){if ( T == NULL )return NULL;if ( X < T->Element )return Find( X, T->Left );elseif ( X > T->Element )return Find( X, T->Right );else return T;}/* 對二叉尋找樹的FindMin的遞迴實現 */PositionFindMin( SearchTree T ){if ( T == NULL )return NULL;elseif ( T->Left  == NULL )return T;elsereturn FindMin( T->Left );}/* 對二叉尋找樹的FindMax的非遞迴實現 */PositionFindMax( SearchTree T ){if ( T != NULL )while( T->Right != NULL )T = T->Right;return T;}SearchTreeInsert( ElementType X, SearchTree T ){if ( T == NULL ){/* Create and return a one-node tree */T = malloc( sizeof( struct TreeNode ) );if ( T== NULL )FatalError( "Out of space!!!" );else{T->Element = X;T->Left = T->Right = NULL;}}elseif ( X < T->Element )T->Left = Insert( X, T->Left );elseif ( X > T->Element )T->Right = Insert( X, T->Right );/* Else X is in the tree already;we'll do nothing */return T; /* Do not forget this line!!! */}SearchTreeDelete( ElementType X, SearchTree T ){Position TmpCell;if ( T == NULL)Error( "Element not found" );elseif ( X < T->Element ) /* Go left */T->Left = Delete( X, T->Left );elseif ( X > T->Element ) /* Go Right */T->Right = Delete( X, T->Left );else /* Found element to be deleted */if ( T->Left && T->Right ) /*Two children */{/* Replace with smallest in right subtree */TmpCell = FindMin( T->Right );T->Element = TmpCell->Element;T->Right = Delete( T->Element, T->Right );}else /* One or zero children */{TmpCell = T;if ( T->Left == NULL) /* Also handles 0 children */T = T->Right;else if ( T->Right == NULL )T = T->Left;free( TmpCell );}return T;}ElementType Retrieve( Position P ){return P->Element;}void PrintElement( SearchTree T ){printf( "%3d ", Retrieve( T ) );}void PreOrder( SearchTree T ){if (T != NULL ){PrintElement( T );PreOrder( T->Left );PreOrder( T->Right );}}void InOrder( SearchTree T ){if (T != NULL ){InOrder( T->Left );PrintElement( T );InOrder( T->Right );}}void PostOrder( SearchTree T ){if ( T != NULL ){PostOrder( T->Left );PostOrder( T->Right );PrintElement( T );}}

檔案名稱:main.c

#include "tree.h"#include <stdio.h>int main(){SearchTree T = NULL;int i, j, m, n;ElementType tmp;printf( "Number of Elements:" );scanf( "%d", &n );for ( i = 0; i < n; i++){scanf( "%d", &tmp );T = Insert( tmp, T );}        printf( "\nPreOrder :" );        PreOrder( T );printf( "\nInOrder  :" );InOrder( T );printf( "\nPostOrder:" );PostOrder( T );        printf( "\n" );return 0;}

附錄:上述代碼中用到了Error、FatalError等函數,其實現如下(即fatal.h檔案):

#include <stdio.h>#include <stdlib.h>#define Error( Str )        FatalError( Str )#define FatalError( Str )   fprintf( stderr, "%s\n", Str ), exit( 1 )

備忘:本文摘自《資料結構與演算法分析 C語言描述 Mark Allen Weiss著》,代碼經gcc編譯測試通過。

附件下載:http://download.csdn.net/detail/shuxiao9058/4212427#tree_20120401.tar.gz

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.