The definition of a binary search tree and related operations

definition

Binary search tree (BST) is a special function and rubbing, can be empty, at which satisfy properties:
1. nonempty left subtree of all keys less than the key of its root node
2. The non-empty right sub tree all keys greater than its root keys
3. the left and right words are binary search tree
4. This can be seen in the binary tree traversal sequence can be a sequence from small to large output

operating

Find the search operation binary search tree

Recursive

Position Find(BinTree BST, ElementType X)
{
	if(!BST) return NULL; //查找失败
	if(x>BST->Data)
	    return Find(BST->Right, X); //在右子树中递归查找
	else if(x<BST->Data)
	    return Find(Bst->Left, X);  //在左子树中递归查找
	else  //x==BST->Data
	return BST;   //在当前结点查找成功,返回当前结点的地址 
}

Due to the high efficiency of non-recursive functions, general non-recursive iterative implementation looks
iterative implementation

Position Find(BinTree BST, ElementType X)
{
	while(BST)
	{
		if(x>BST->Data)
		    BST=BST->Right;
		else if(x<BST->Data)
		    BST=BST->Left;
		else //X==BST->Data
			break;  //在当前结点查找成功,跳出循环 
	}
	return BST; //返回找到的结点地址,或是UNLL 
}
Find the maximum and minimum elements

Depending on the nature of the binary search tree, the smallest element must be on the end of the leftmost node of the tree branches. The so-called left-most branch end nodes, means that no child node on the left-most branch. It must be the largest element on the end nodes of the rightmost branch. As shown below. This makes it easier to find than a function, just start from the root node, when it is not empty, the pointer is determined individually for each node along the left branch or the right branch, the null pointer know so far. Back layer by layer from the left branch looking down to the smallest element, find the right branch is the largest element.
Here Insert Picture Description
Find the smallest element of recursive

Position FindMin(BinTree BST) //最小元素在最左端点 
{
	if(!BST) return NULL; //空的二叉搜索树,返回NULL
	else if(!BST->Left) return BST; //找到最左端并返回
	else
	return FindMin(BST->Left); //沿左分支递归查找 
}

Find the largest element of the iterative implementation

Position FindMin(BinTree BST)
{
	if(BST)
	while(BST->Right)
		BST=BST->Right; //沿右分支一直向下,知道最右端点
	return BST; 
}
Binary search tree insertion

The element X is inserted into a binary search tree BST key is to find the elements to be inserted. Determining position may be utilized to find a method analogous Find function, found in the tree if the BST X, described element to be inserted already exists, the insertion operation can be abandoned. If you do not find X, X find the location of the termination is to be inserted.
Insert element shown in FIG. 35, a lookup operation to do first, according to the search algorithm described above, to find the key value to terminate at a leaf node 33, the termination element 35 as this right child node to complete the new element the insertion.
Code

BinTree Insert(BinTree BST, ElementType X)
{
	if(!BST)  //若原树为空,生成并返回一个结点的二叉搜索树
	{
		BST=(BinTree)malloc(sizeof(struct TNode));
		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;
}
Delete binary search tree

Delete binary search tree is more complicated, to remove the position of the nodes in the tree determines the method of operation, there are three cases to consider:
1. To remove a leaf node
this situation can be removed directly, then modifying the pointer to its parent node, is set to be empty.
Here Insert Picture Description
2. If the eye delete nodes with only one child node (the node is not necessarily a leaf node, it can be the root of sub-tree), it is necessary to change the pointer before deleting the parent node, point to delete nodes child node, shown in FIG.
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_44256227/article/details/89976001