[Postgraduate entrance examination data structure code question 1] Search and insertion in binary sorting tree (recursive and non-recursive implementation)

Title: Please use C language to write the binary linked list structure of the binary tree, and write a function in the binary search tree (binary sorting tree, binary search tree) Can search for a given keyword

Difficulty:

Characteristics of binary sorting trees (binary search trees, binary search trees, BST trees)

Binary sorting tree is also called binary search tree, which is a special kind of binary tree.
It is defined as: the binary tree sorting tree is either an empty tree or a binary tree with the following properties:
(1) If its left subtree is not Empty, then the values ​​of all nodes on the left subtree are less than the value of the root node;
(2) If its right subtree is not empty, then the values ​​of all nodes on the right subtree are The values ​​are greater than (or greater than or equal to) the value of the root node;
(3) Its left and right subtrees are also binary sorting trees.
This is a recursive definition. 

 Binary linked list structure of binary tree

#include<stdio.h>
#include<stdlib.h>
//二叉树的结点结构
typedef struct Node{
	int data;//存放结点数据
	struct Node *left;//左子树指针
	struct Node *right;//右子树指针 
}Node,*BiTree;

Find the specified keyword in the binary sort tree

recursive algorithm

Algorithm idea: According to the characteristics of the binary sorting tree, left<root<right, perform recursive traversal search

BiTree searchNode(BiTree root,int key){
	//递归出口
	if(root==NULL||root->data==key){
		return root;//返回存储待查找关键字的节点 
	} else if(key<root->data){
		return searchNode(root->left,key); 
	} else {
		return searchNode(root->right,key); 
	}
} 

non-recursive algorithm

BiTree searchNode(BiTree root,int key){
	//若树为空或者关键字等于根结点值则结束循环 
	while(root!=NULL&&key!=root->data){
		if(key<root->data){
			root=root->left;
		} else{
			root=root->right;
		}
	} 
	return root;
}


Binary search tree insertion

Algorithm idea: First determine whether the tree is an empty tree. If it is an empty tree, you need to use the first inserted node as the root node, use the malloc function in C language to apply for a node memory space, and Initialize the left and right subtree pointers to be empty; if it is not an empty tree, recursively proceed according to the characteristics of the binary sorting tree: left<root<right insert. Note: Node* in the parameter list represents the node pointer type of the number, &root means taking out the address of the current node.

recursive algorithm

//二叉搜索树的插入(递归方式)
void InsertNode(BiTree root,int key){
	//原始树为空则新插入的结点作为根结点 
	if(root==NULL){
		root=(BiTree)malloc(sizeof(Node));
		root->data=key;
		root->left=root->right=NULL;
	}else if(key<root->data){
		InsertNode(root->left,key);
	}else {
		InsertNode(root->right,key);
	}
} 

non-recursive algorithm

//二叉搜索树的插入(非递归方式)
int InsertNode(BiTree T,int key){
	if(!Search(T,e,NULL)){
		BiTree s=(BiTree)malloc(sizeof(Node));
		s->data=key;
		s->lchild=s->rchild=NULL;
		if(!p){
			T=s;root=T;
		}else if(key<p->data){
			p->lchild=s;
		}else{
			p->rchild=s;
		}
		return 1;
	}
	return 0;
} 

There is no recursion in the insertion, but in fact, the leaf node to be inserted is searched recursively first, with a little recursion in it.

 

3. Little knowledge points of C language: pointer type * and usage of address operator &

Reference article

Usage of pointer variable address character & in C language *Usage of pointer variable nameicon-default.png?t=N7T8https://wuyujin.blog.csdn.net/article/details/128752845? spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-3-128752845-blog-105318954.235%5Ev38%5Epc_relevant_anti_t3&depth_1-utm_source=distribute.pc _relevant.none- task-blog-2~default~CTRLIST~Rate-3-128752845-blog-105318954.235%5Ev38%5Epc_relevant_anti_t3&utm_relevant_index=6

C语言指针详解icon-default.png?t=N7T8https://blog.csdn.net/liu100m/article/details/90731422?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522169901195416800182115107%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=169901195416800182115107&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-2-90731422-null-null.142%5Ev96%5Epc_search_result_base2&utm_term=C%E8%AF%AD%E8%A8%80%E6%8C%87%E9%92%88&spm=1018.2226.3001.4187

Guess you like

Origin blog.csdn.net/qq_52487066/article/details/134105837