Operation of binary search tree (implemented in c language)

Operation of binary search tree (implemented in c language)

Binary Search Tree, if its left subtree is not empty, the value of all nodes on the left subtree is less than the value of its root node; if its right subtree is not empty, then the right The values ​​of all nodes on the subtree are greater than the value of its root node; its left and right subtrees are also binary search trees. As a classic data structure, the binary search tree has the characteristics of fast insertion and deletion operations of linked lists, and the advantage of fast search of arrays; therefore, it is widely used, such as file systems and database systems. The data structure performs efficient sorting and retrieval operations.

 typedef struct treenode *Bitree;
struct treenode {
    
    
	int data;
	Bitree left;
	Bitree right;
};

The core of the conventional operation function of the binary search tree is the pre-order, middle-order, and post-order traversal of the binary tree

Recursively implement binary tree traversal

void InorderTraversal( Bintree BT )
{
    
    
    if( BT ) {
    
    
        InorderTraversal( BT->Left );
        /* 此处假设对BT结点的访问就是打印数据 */
        printf("%d ", BT->Data); /* 假设数据为整型 */
        InorderTraversal( BT->Right );
    }
}

The same is true for first order and then order. Sequence traversal uses queues instead of recursion, so I won’t discuss them here.

Insertion of binary search tree

Bitree Insert(Bitree BST, int X)
{
    
    
	if (!BST) {
    
     /* 若原树为空,生成并返回一个结点的二叉搜索树 */
		BST = (Bitree)malloc(sizeof(struct treenode));
		BST->data = X;
		BST->left = BST->right = NULL;
	}
	else {
    
     /* 开始找要插入元素的位置 */
		if (X < BST->data)
			BST->left = Insert(BST->left, X);   /*递归插入左子树*/
		else  if (X > BST->data)
			BST->right = Insert(BST->right, X); /*递归插入右子树*/
												/* else X已经存在,什么都不做 */
	}
	return BST;
}

Search operation

int findmin(Bitree t) {
    
    
	while(t->left) t = t->left;
	return t->data;    /*左子树的最后一个节点*/
}
Bitree find(Bitree t, int x) {
    
    
	Bitree a=NULL;
	if (t) {
    
    
		if (x > t->data) a = find(t->right);
		else if (x < t->data) a = find(t->left);
		else if (x == t->data) a = t;
	}
	return a;
}

Delete operation

Find the deleted node first.
There is no child node and delete it directly;
there is only one subtree. The deletion operation of
a singly linked list has two subtrees. Find the smallest node from the right subtree or the largest node from the left subtree to replace this node. Recursively delete the smallest node of the right subtree

BinTree Delete( BinTree BST, ElementType X ) 
{
    
     
    Position Tmp; 
 
    if( !BST ) 
        printf("要删除的元素未找到"); 
    else {
    
    
        if( X < BST->Data ) 
            BST->Left = Delete( BST->Left, X );   /* 从左子树递归删除 */
        else if( X > BST->Data ) 
            BST->Right = Delete( BST->Right, X ); /* 从右子树递归删除 */
        else {
    
     /* BST就是要删除的结点 */
            /* 如果被删除结点有左右两个子结点 */ 
            if( BST->Left && BST->Right ) {
    
    
                /* 从右子树中找最小的元素填充删除结点 */
                Tmp = Findmin( BST->Right );
                BST->Data = Tmp->Data;
                /* 从右子树中删除最小元素 */
                BST->Right = Delete( BST->Right, BST->Data );
            }
            else {
    
     /* 被删除结点有一个或无子结点 */
                Tmp = BST; 
                if( !BST->Left )       /* 只有右孩子或无子结点 */
                    BST = BST->Right; 
                else                   /* 只有左孩子 */
                    BST = BST->Left;
                free( Tmp );
            }
        }
    }
    return BST;
}



Guess you like

Origin blog.csdn.net/qq_40602655/article/details/106594601
Recommended