B-tree binary tree search

Binary tree search (B tree)

Tree binary sort (Binary Sort Tree) called a binary search tree, it is a very useful sort and search for special tree.

  • The definition of
    a binary sort treeA binary sort tree is either an empty tree or a binary tree with the following properties:

    (1) If its left subtree is not empty, the values ​​of all nodes on the left subtree are less than the value of its root node;

    (2) If its right subtree is not empty, the values ​​of all nodes on the right subtree are greater than the value of its root node;

    (3) Its left and right subtrees are also binary sort trees.

    The binary sort tree is defined recursively. From the definition, we can get an important property of the binary sort tree: when in-order traverses a binary tree, an ordered sequence with increasing node values ​​can be obtained.

  • Insertion of Binary Sort Tree The insertion of a
    node is to compare the size of its own key and the key of the root node. If it is greater than, then recursively insert the right subtree, if it is less, then recursively insert the left subtree until the node is empty. Code:

	//二叉排序树的插入
	void InsertBST(BSTree& T, ElemType e)
	{
    
    
		if (!T)//树空
		{
    
    
			BSTNode* S = new BSTNode;
			S->data = e;
			S->lchild = NULL;
			S->rchild = NULL;
			T = S;
		}
		else if (e.key > T->data.key)
		{
    
    
			InsertBST(T->rchild, e);
		}
		else
		{
    
    
			InsertBST(T->lchild, e);
		}
	}
	//二叉排序树的插入
  • The creation of binary sort tree
    Since we want to search through the B tree, the premise is that we have to have a B tree. How to create a B-tree. First, we create an empty tree, let the first node of the tree be the root node, and the following nodes, by comparing the key size of the tree node, to determine whether it is in the left subtree or the right subtree. Let's take a look at the code below:
	//二叉排序树的创建
	void CreateBST(BSTree& T)
	{
    
    
		T = NULL;//初始化空树
		ElemType e;
		cin >> e.key;
		while (e.key != EOF)//-1结束
		{
    
    
			InsertBST(T, e);//插入
			cin >> e.key;
		}
	}
//二叉排序树的创建
  • Binary sort tree search
    Binary tree search is very simple! First compare the key to be compared with the key of the root node, if it is less than, then compare with the key of the left subtree, if it is greater, compare with the key of the right subtree. Keep recursing until the node that matches the target keyword.
    Code:
	//二叉排序树的创建
	void CreateBST(BSTree& T)
	{
    
    
		T = NULL;//初始化空树
		ElemType e;
		cin >> e.key;
		while (e.key != EOF)//-1结束
		{
    
    
			InsertBST(T, e);//插入
			cin >> e.key;
		}
	}
	//二叉排序树的创建

The overall code implementation:

	//B树
	#include<iostream>
	using namespace std;
	typedef int KeyType;
	//二叉排序树的二叉链表储存表示
	typedef struct
	{
    
    
		KeyType key;//关键字
		int otherinfo;//其他信息
	}ElemType;
	
	typedef struct BSTNode
	{
    
    
		ElemType data;//结点数据域
		struct BSTNode* lchild, * rchild;
	}BSTNode,*BSTree;
	
	//二叉排序树的二叉链表储存表示
	
	//二叉排序树的插入
	void InsertBST(BSTree& T, ElemType e)
	{
    
    
		if (!T)//树空
		{
    
    
			BSTNode* S = new BSTNode;
			S->data = e;
			S->lchild = NULL;
			S->rchild = NULL;
			T = S;
		}
		else if (e.key > T->data.key)
		{
    
    
			InsertBST(T->rchild, e);
		}
		else
		{
    
    
			InsertBST(T->lchild, e);
		}
	}
	//二叉排序树的插入
	
	//二叉排序树的创建
	void CreateBST(BSTree& T)
	{
    
    
		T = NULL;//初始化空树
		ElemType e;
		cin >> e.key;
		while (e.key != EOF)//-1结束
		{
    
    
			InsertBST(T, e);//插入
			cin >> e.key;
		}
	}
	//二叉排序树的创建
	
	//二叉排序树的递归查找
	BSTree Search(BSTree T, KeyType key)
	{
    
    
		if (!T || T->data.key == key) return T;//树空或者查找成功返回结点指针
		else if (key > T->data.key)//若关键字大于当前节点的关键字,则在右子树中查找
		{
    
    
			Search(T->rchild, key);
		}
		else//若关键字小于当前节点的关键字,则在左子树中查找
		{
    
    
			Search(T->lchild, key);
		}
	}
	//二叉排序树的递归查找
	
	int main()
	{
    
    
		BSTree T = new BSTNode;
		CreateBST(T);
		cout << "请输入您要查找的节点:";
		KeyType key;
		cin >> key;
		BSTNode* S = new BSTNode;
		S = Search(T, key);
		cout <<  "您要查找的位置为:" << S->data.key;
		return 0;
	}

Guess you like

Origin blog.csdn.net/m0_43456002/article/details/104418242