Data Structure (6)-Search and Sort

Find


Personal notes, intrusion


Binary search is also called binary search.

He must askSequential storage structureAnd requiresOrderly

Insert picture description here

int Search_Bin(SSTable ST,KeyType key)
{
	low=1;
	high=ST.length;
	while(low<=high)
	{
		mid=(low+high)/2;
		if(ST.R[mid].key==key) return mid;
		else if(ST.R[mid]>key) high=mid-1;
		else low=mid+1; 
	}
	return 0;	
}

Insert picture description here

Binary arrangement tree

The characteristics of the binary sort tree: all the left subtrees are smaller than the root node, all the right subtrees are larger than the root node, and the subtree is also a binary sort tree.
Insert picture description here

statement

typedef struct
{
	KeyType key;		//关键字域
	InfoType otherInfo;	//其他数据项
}ElemType;
typedef strcut BSTnode;
{
	ElemType data;
	struct BSTnode *lchild,*rchild;
}BSTndoe,*BSTree;

insert

void InsertBST(BSTree &T,ElemType e)
{
	if(!T)
	{
		BSTree S=new BSTree;
		S->data=e;
		S->lchild=S->rchild=NULL;
		T=S;
	}
	else if(e.key>T->data.key) InsertBST(T->rchild,e);
	else InsertBST(T->lchild,e);
}

create

void CreatBST(BSTree &T)
{
	T=NULL;
	cin>>e;
	while(e.key!=ENDFLAG)
	{
		InsertBST(T,e);
		cin>>e;
	}
}

Balanced binary tree

The search performance of a binary sort tree depends on the structure of the binary sort tree, and the shape of the binary tree depends on the data set

If it is ordered, the search time complexity is O (n). If the structure is reasonable, the search time complexity is O(log 2 n). In fact, the smaller the tree height, the faster the search. Therefore, a balanced binary tree, also known as AVL tree, is proposed.

A balanced binary tree or an empty tree has the following characteristics:
(1) The absolute value of the difference between the depth of the left and right subtrees does not exceed 1
(2) The left and right subtrees are also balanced binary trees

Insert picture description here
Insert picture description here


Balance adjustment method of balanced binary tree

Obviously, constructing a tree into a balanced binary tree is conducive to search efficiency.
So how to convert an unbalanced binary tree into a balanced binary tree:

If the balance factor (Balance Factor, BF) of a node on a binary tree is defined as the
difference between the depth of the left and right subtrees of the node, then the balance factor of all nodes on the balanced binary tree can only find -1, 0 and 1. As long as
the absolute value of the balance factor of a node on the binary tree is greater than 1, the binary tree is unbalanced

Insert picture description here
Insert picture description here


Insert picture description here


Insert picture description here


Insert picture description here


What is the meaning of the M order in the B tree?

Insert picture description here


Insert picture description here


B-tree background

Insert picture description here


Second detection method

time Tunnel


to be continued. . . (2021/1/16)

Guess you like

Origin blog.csdn.net/Touale/article/details/112709667