二叉搜索树的概念及插入操作

一,二叉搜索树

二叉搜索树也是在二叉树的基础上增加了一些约束,使得他成为后续平衡树、红黑树的基石,在工程上几乎用不到这棵树因为本身有很大问题,但后续树却都是他的变种。我们看看它增加了哪些约束使得他这么好用。

a.如果他的左子树不空,则左子树上所有结点的值均小于它的根结点的值。

b.若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值。

c.它的左、右树又分为二叉排序树。

二叉搜索树的概念及插入操作_第1张图片

二叉树的插入操作

通过上面约束,在寻找新节点插入位置时遵循左小右大,可以跟已有节点一一比较,小了往左边走,大了往右边走,但不能在两节点之间插入,因为这样很容易变成一个线性结构,所以二叉搜索树插入操作的第一个特点:新节点插入的位置只能是空,当我们遇到空位置就会停止递归,在空位置创建一个新节点并作为返回值赋值给双亲节点的左或右孩子

二,二叉搜索树的代码实现

1.头文件中的接口

#ifndef BINARYSEARCHTREE_H
#define BINARYSEARCHTREE_H
typedef int Element;

typedef struct _node {
	Element data;
	struct _node*left;
	struct _node*right;
}BSTNode;

typedef struct {
	BSTNode*root;
	int count;
	char*name;
}BSTree;

BSTree*createBSTree(char*name);

BSTNode* insertBSTree(BSTree*tree, Element data);

void inOrderBSTree(BSTree*tree);

int heightBSTree(BSTree*tree);

BSTNode* searchBSTree(BSTree*tree,Element data);
#endif //BINARYSEARCHTREE_H

2.将头文件的接口一一实现

#include "binarySearchTree.h"
#include 
#include 
BSTree * createBSTree(char *name) {
	BSTree*tree=malloc(sizeof(BSTree));
	if(tree == NULL) {
		return NULL;
	}
	tree->name=name;
	tree->root=NULL;
	tree->count=0;
	return tree;
}

static BSTNode*createBSTNode(Element e) {
	BSTNode*node=malloc(sizeof(BSTNode));
	if(node == NULL) {
		return NULL;
	}
	node->data=e;
	node->left=node->right=NULL;
	return node;
}

static BSTNode * insertBSTNode(BSTree *tree,BSTNode *node, Element e) {
	if (node == NULL) {
		tree->count++;
		return createBSTNode(e);
	}
	if (edata) {
		node->left=insertBSTNode(tree,node->left,e);
	}else if (e>node->data) {
		node->right=insertBSTNode(tree,node->right,e);
	}
	return node;
}

BSTNode * insertBSTree(BSTree *tree, Element e) {
	BSTNode*node=insertBSTNode(tree,tree->root,e);
	tree->root=node;
	return node;
}

static void visitBSTNode(BSTNode *node) {
	printf("%d ",node->data);
}

static void inOrderBSTNode(BSTNode *node) {
	if (node) {
		inOrderBSTNode(node->left);
		visitBSTNode(node);
		inOrderBSTNode(node->right);
	}
}

void inOrderBSTree(BSTree *tree) {
	if (tree == NULL) {
		return;
	}
	inOrderBSTNode(tree->root);
	printf("\n");
}

static int heightBSTNode(BSTNode*node) {
	if (node==NULL) {
		return 0;
	}
	int leftheight=heightBSTNode(node->left);
	int rightheight=heightBSTNode(node->right);
	if (leftheight>rightheight) {
		return ++leftheight;
	}
	return ++rightheight;
}

int heightBSTree(BSTree *tree) {
	return heightBSTNode(tree->root);
}

BSTNode* searchBSTree(BSTree *tree, Element data) {
	BSTNode*node=tree->root;
	while (node) {
		if (node->data==data) {
			return node;
		}
		if (node->dataleft;
		}else {
			node=node->right;
		}
	}
	return node;
}

3.测试代码是否有bug

#include "binarySearchTree.h"
#include 
#include 
#include 

void test() {
	BSTree*tree=createBSTree("stu");
	int arr[]={55,33,100,22};
	for (int i=0;icount);
	printf("height:%d\n",heightBSTree(tree));
}

static void findBSTree(BSTree*tree,int add,Element data) {
	BSTNode*node=NULL;
	for (int i=0;i

推荐一个数据结构的网站,里面可以演示一些操作的动态图:

https://www.cs.usfca.edu/~galles/visualization/Algorithms.html

测试2用到的函数:

①获取随机数,在头文件

srandf(time(NULL))

rand()%n+a

表示获取范围为(a,n+a)

②获取时间,clock函数在头文件

返回值类型clock_t不同系统不一样,通常long或long long,要返回秒需要除以CLOCKS_PER_SEC

你可能感兴趣的:(二叉搜索树的概念及插入操作)