数据结构—二叉搜索树
原理:参考趣学数据结构
代码:
队列代码:
# pragma once
# define N 100
# define elemType bstTree*
# include <stdlib.h>
typedef struct bstTree { int data; struct bstTree * lchild, * rchild;
} bstTree;
typedef struct dQueue { elemType data; struct dQueue * next;
} dQueue;
typedef struct queue { dQueue * front, * rear;
} queue;
bool initQueue ( queue & Queue) { Queue. front = Queue. rear = ( dQueue* ) malloc ( sizeof ( dQueue) ) ; if ( ! Queue. front) { return false; } Queue. front-> next = NULL ; return true;
}
elemType getQueueTopElem ( queue & Queue) { elemType u= NULL ; if ( Queue. front != Queue. rear) { dQueue* p = Queue. front-> next; u = p-> data; } return u;
}
bool enQueue ( queue & Queue, elemType e) { dQueue* p = Queue. rear; dQueue* s = ( dQueue* ) malloc ( sizeof ( dQueue) ) ; s-> data = e; s-> next = NULL ; p-> next = s; Queue. rear = s; return true;
}
bool deQueue ( queue & Queue, elemType & e) { if ( Queue. front == Queue. rear) { return false; } dQueue* p = Queue. front-> next; e = p-> data; Queue. front-> next = p-> next; if ( p == Queue. rear) { Queue. rear = Queue. front; } delete p; return true;
}
bool emptyQueue ( queue Queue) { if ( Queue. front == Queue. rear) { return true; } return false;
}
删除结点和四种(前序、中序、后序、层次)树的结点遍历:
# include <stdio.h>
# include <stdlib.h>
# include "queue.h"
void createBSTTree ( bstTree* & T, int data) { bstTree * p = NULL ; if ( ! T) { p = ( bstTree* ) malloc ( sizeof ( bstTree) ) ; p-> data = data; p-> lchild = p-> rchild = NULL ; T = p; return ; } if ( data < T-> data) { createBSTTree ( T-> lchild, data) ; } else { createBSTTree ( T-> rchild, data) ; }
}
void prePrint ( bstTree* BSTTree) { if ( BSTTree) { printf ( "%d " , BSTTree-> data) ; prePrint ( BSTTree-> lchild) ; prePrint ( BSTTree-> rchild) ; }
}
void midPrint ( bstTree* BSTTree) { if ( BSTTree) { midPrint ( BSTTree-> lchild) ; printf ( "%d " , BSTTree-> data) ; midPrint ( BSTTree-> rchild) ; }
}
void lastPrint ( bstTree* BSTTree) { if ( BSTTree) { lastPrint ( BSTTree-> lchild) ; lastPrint ( BSTTree-> rchild) ; printf ( "%d " , BSTTree-> data) ; }
}
void layerPrint ( bstTree* BSTTree) { printf ( "\n层次遍历二叉排序树:" ) ; queue Queue; initQueue ( Queue) ; if ( ! BSTTree) { return ; } enQueue ( Queue, BSTTree) ; while ( ! emptyQueue ( Queue) ) { bstTree* e1 = getQueueTopElem ( Queue) , * e; deQueue ( Queue, e) ; printf ( "%d " , e1-> data) ; if ( e1-> lchild) { enQueue ( Queue, e1-> lchild) ; } if ( e1-> rchild) { enQueue ( Queue, e1-> rchild) ; } } printf ( "\n" ) ;
}
void deleteElemBSTTree ( bstTree* & T, int e) { bstTree * f= NULL , * p= T, * q, * s; if ( ! T) { return ; } while ( p) { if ( p-> data == e) { break ; } f = p; if ( p-> data < e) { p = p-> lchild; } else { p = p-> rchild; } } if ( ! p) { printf ( "\n没有要删除的元素%d\n" , e) ; return ; } if ( ( p-> lchild) && ( p-> rchild) ) { q = p-> lchild; s = q; while ( q-> rchild) { s = q; q = q-> rchild; } p-> data = q-> data; if ( s != q) { s-> rchild = q-> lchild; } else { p-> lchild= q-> lchild; p = q; } delete q; } else { q = p; if ( ! p-> lchild) { p = p-> rchild; } else if ( ! p-> rchild) { p = p-> lchild; } if ( ! f) { T= p; } else if ( f-> lchild == q) { f-> lchild = p; } else { f-> rchild = p; } delete q; }
}
int main ( ) { bstTree* T= NULL ; int count, data; printf ( "开始构造二叉排序树:\n输入二叉排序树结点的数目:" ) ; scanf_s ( "%d" , & count) ; while ( count-- ) { printf ( "输入二叉排序树的第%d个结点:" , count+ 1 ) ; scanf_s ( "%d" , & data) ; createBSTTree ( T, data) ; } printf ( "前序遍历二叉排序树\n" ) ; prePrint ( T) ; printf ( "\n" ) ; printf ( "中序遍历二叉排序树\n" ) ; midPrint ( T) ; printf ( "\n" ) ; printf ( "后序遍历二叉排序树\n" ) ; lastPrint ( T) ; printf ( "\n" ) ; layerPrint ( T) ; printf ( "\n" ) ; int delElem; int counts = 2 ; while ( counts-- ) { printf ( "删除元素:" ) ; scanf_s ( "%d" , & delElem) ; deleteElemBSTTree ( T, delElem) ; } printf ( "删除元素后的遍历如下:\n" ) ; printf ( "前序遍历二叉排序树\n" ) ; prePrint ( T) ; printf ( "\n" ) ; printf ( "中序遍历二叉排序树\n" ) ; midPrint ( T) ; printf ( "\n" ) ; printf ( "后序遍历二叉排序树\n" ) ; lastPrint ( T) ; printf ( "\n" ) ; layerPrint ( T) ; printf ( "\n" ) ; system ( "pause" ) ; return 0 ;
}
测试截图:
删除元素的时间复杂度O(logn),空间复杂度为O(1)
如果存在什么问题,欢迎批评指正!谢谢!