查找二叉树-链表实现
来源:互联网 发布:辉煌中国 知乎 编辑:程序博客网 时间:2024/06/07 12:52
对于查找二叉树,一般用来进行查找操作,其基本操作的平均时间为 O(log N)。
下面给出查找二叉树的链表实现,该查找二叉树中所有的元素互异,在插入时若树种已经有了具有相同关键字的元素,不做任何操作。
代码如下:
struct TreeNode;typedef struct TreeNode *Position;typedef struct TreeNode *SearchTree;struct TreeNode{ ElementType Element;SearchTree Left;SearchTree Right;};SearchTree MakeEmpty( SearchTree );SearchTree Insert( ElementType X, SearchTree T );SearchTree Delete( ElementType X, SearchTree T );Position Find( ElementType X, SearchTree T );Position FindMin( SearchTree T );Position FindMax( SearchTree T );SearchTree MakeEmpty( SearchTree T ){ if ( T != NULL ){ MakeEmpty( T->Left );MakeEmpty( T->Right );free( T );}return NULL;}SearchTree Insert( ElementType X, SearchTree T ){ if ( T == NULL ){ T = ( Position )malloc( sizeof(struct TreeNode) );if ( T == NULL ){ printf( "Out of space" );return NULL;}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 );// 若 X = X->Element, 不处理return T;}SearchTree Delete( ElementType X, SearchTree T ){ Position TmpCell; if ( T == NULL ) { printf( "Element not found" ); return NULL; }elseif ( X < T->Element ) T->Left = Delete( X, T->Left );elseif ( X > T->Element ) T->Right = Delete( X, T->Right );elseif ( T->Left && T->Right ){ TmpCell = FindMin( T->Right );T->Element = TmpCell->Element;T->Right = Delete( TmpCell->Element, T->Right );}else{ TmpCell = T; if ( T->Left == NULL )T = T->Right;elseif ( T->Right == NULL ) T = T->Left;free( TmpCell );}return T;}Position Find ( 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;}Position FindMax( SearchTree T ){ if ( T == NULL ) return NULL;elseif( T->Right == NULL ) return T;else return FindMax( T->Right );}Position FindMin( SearchTree T ){ while ( T != NULL ) if ( T->Left != NULL ) T = T->Left;return T;}
0 0
- 查找二叉树-链表实现
- 二叉查找树<链表实现>
- 二叉查找树实现类——二叉链表
- 用二叉链表实现二叉查找树(二)
- 二叉查找树实现
- 二叉查找树实现
- 二叉查找树实现
- 二叉查找树实现
- 二叉查找树转双向链表JAVA实现
- 查找--二叉查找树分析与实现
- JAVA二叉查找树实现
- 二叉查找树代码实现
- 二叉查找树 ADT实现
- java实现二叉查找树
- 二叉查找树 java实现
- 二叉查找树实现排序
- 【二叉查找树】c实现
- 二叉查找树 BinarySearchTree 实现
- hdu2222 ac自动机模板题
- c#初学_02
- ffmpeg解码视频及倒序播放
- 使用keytool工具生成自签名证书
- Uva 11732题解
- 查找二叉树-链表实现
- 2015创发科技校园招聘笔试题—— union考察
- 人脸识别必读的N篇文章
- hdu 4412 Sky Soldiers (区间dp 单调性)
- iptables基本用法
- Introduction to C++ for Financial Engineers-Study Notes 2(Ch8-Ch)
- UIScrollView 是否滑到底部
- discuz 后台页面开发
- SAP SD 如何把几张delivery合并成一张发票invoice