数据结构与算法之二叉搜索树
与链表不同,树是一种非线性的数据结构。树中最常用的是二叉树,二叉树限制了子树的数量,也就是每个结点的子树至多2个,并且这两个子树是有顺序的。而二叉搜索树(二叉查找树,二叉排序树)是指根节点的关键字大于左子树的,而小于右子树,并且,左右子树也是一颗二叉搜索树。也就是说中序遍历一颗二叉搜索树,它的输出是从小到大排序好的。
除了普通的二叉搜索树之外,还有很多关于它的变形。
二叉平衡搜索树,即即是一颗二叉平衡树,也是一颗搜索树,平衡树即任意一个结点的左子树的高度与右子树的高度之差的绝对值不大于1。
红黑树,也是一种特殊的二叉查找树,除了具备查找树的特点,它的每个结点上还有一个记录红黑颜色的元素。
这里我们主要讨论一般的二叉搜索树。
二叉搜索树,其实就是一颗特殊的树,任何适用于树的算法都适用于二叉搜索树。当然它也是图,任何适用于图的算法也适用于二叉搜索树。图中的深度优先搜索(DFS)其实就是树中的前序遍历,广度优先搜索(BFS)就是树中的层序遍历。当然图中还有很多其他算法也都适用于树。
1、二叉树的数据结构
typedefstructbinSrcTreeNode{intelement;structbinSrcTreeNode*pLeft;structbinSrcTreeNode*pRight;}BSTNode;2、动态分配一个二叉树的结点
BSTNode*allocNode(intelement){BATNode*pNode=(BSTNode*)malloc(sizeof(BSTNode));if(NULL!=pNode){pNode-element=element;pNode-pLeft=NULL;pNode-pRight=NULL;}returnpNode;}3、二叉搜索树的查找
由于二叉搜索树是已经排序了的二叉树,所以可以通过二分查找来搜索。先判断是否等于根节点,如果大于根节点的关键字,搜索右子树,小于根节点的关键字,搜索左子树。
boolsearchInBSTree(constBSTNode*pRoot,intelement,BSTNode**pNode){if(NULL==pRoot)returnfalse;*pNode=pRoot;if(pRoot-element==element){returntrue;}elseif(pRoot-elementelement)returnsearchInBSTree(pRoot-pLeft,element,pNode);elsereturnsearchInBSTree(pRoot-pRight,element,pNode);}4、二叉搜索树的插入
首先查找二叉树是是否已经存在该元素,如果已经存在返回false;如果不存在,插入该结点,插入元素大于根节点,则插入右子树,否则,插入左子树中。
boolinsertIntoBSTree(BSTNode**pRoot,intelement){if(NULL==pRoot)returnfalse;if(NULL==*pRoot){*pRoot=allocNode(element);returnNULL==*pRoot?false:true;}BSTNode**pSrc=NULL;if(searchInBSTree(pRoot,element,pSrc))returnfalse;BSTNode*pNode=allocNode(element);if(element*pSrc-element)*pSrc-pLeft=pNode;else*pSrc-pRight=pNode;returntrue;}5、二叉搜索树的删除
二叉搜索树的删除分为三种情况:
(1)删除结点为叶子结点,直接删除该结点,再修改其父结点的指针(注意分是根结点和不是根节点)。
(2)删除结点为单支结点(即只有左子树或右子树)。
(3)删除结点的左子树和右子树均不空。
booldeleteFromBSTree(BSTNode**pRoot,intelement){if(NULL==pRootNULL==*pRoot)returnfalse;BSTNode**pSrc=NULL;BSTNode**pParent=NULL;if(searchInBSTree(*pRoot,element,pParent,pSrc)){if(NULL==*pSrc-pLeftNULL==*pSrc-pRight){free(*pSrc);*pSrc=NULL;returntrue;}elseif(NULL==*pSrc-pLeft){if(*pSrc==*pParent-pLeft)*pParent-pLeft=*pSrc-pRight;elseif(*pSrc==*pParent-pRight)*pParent-pRight=*pSrc-pRight;free(*pSrc);returntrue;}elseif(NULL==*pSrc-pRight){if(*pSrc==*pParent-pLeft)*pParent-pLeft=*pSrc-pLeft;elseif(*pSrc==*pParent-pRight)*pParent-pRight=*pSrc-pLeft;free(*pSrc);returntrue;}else{BSTNode*pNode=*pSrc;BSTNode*pChild=*pSrc-pLeft;while(pChild-pRight){pNode=pChild;pChild=pChild-pRight;}if(pNode==*pSrc)pNode-pLeft=pChild-pLeft;elsepNode-pRight=pChild-pLeft;if(*pSrc==*pParent-pLeft)*pParent-pLeft=pChild;else*pParent-pRight=pChild;pChild-pLeft=*pSrc-pLeft;pChild-pRight=*pSrc-pRight;returntrue;}}returnfalse;}6、二叉搜索树的前序中序后序遍历
/*preOrdertotraversethebinarySearchTree.*/voidpreOrderBinarySearchTree(constBSTNode*pRoot){if(NULL==pRoot)return;printf(%d,pRoot-element);preOrderBinarySearchTree(pRoot-pLeft);preOrderBinarySearchTree(pRoot-pRight);}/*inOrdertotraversethebinarySearchTree.*/voidinOrderBinarySearchTree(constBSTNode*pRoot){if(NULL==pRoot)return;inOrderBinarySearchTree(pRoot-pLeft);printf(%d,pRoot-element);inOrderBinarySearchTree(pRoot-pRight);}/*posOrdertotraversethebinarySearchTree.*/voidposOrderBinarySearchTree(constBSTNode*pRoot){if(NULL==pRoot)return;posOrderBinarySearchTree(pRoot-pLeft);posOrderBinarySearchTree(pRoot-pRight);printf(%d,pRoot-element);}7、释放结点
voidfreeBinarySearchTree(BSTNode*pRoot){if(NULL==pRoot)return;freeBinarySearchTree(pRoot-pLeft);freeBinarySearchTree(pRoot-pRight);free(pRoot);}
海南最好的白癜风医院治疗白癜风效果好的医院