【算法导论之六】二叉排序树(Binary Search Tree)

1. 二叉排序树的定义

    它或者是一棵空树;或者是具有下列性质的二叉树:

           (1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;

           (2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值;

           (3)左、右子树也分别为二叉排序树;

2. java实现代码(注:以下代码参考网上优秀代码,均已编写实现)

/*
 * 注:该代码实现了二叉查找树的所有操作:包括,插入、删除、查找、排序输出、前驱、后继等。
 * */
import java.util.ArrayList;
import java.util.List;
public class BinarySearchTree {
    // 二叉查找树的根节点
	private TreeNode root = null ;
	private List<TreeNode> nodelist = new ArrayList<TreeNode>();
	
	// 二叉树的节点类
	private class TreeNode{
		private int key ;
		private TreeNode leftChild ;
		private TreeNode rightChild ;
		private TreeNode parent ;
		// 构造器
		public TreeNode(int key , TreeNode leftChild , TreeNode rightChild , TreeNode parent)
		{
			this.key = key ;
			this.leftChild = leftChild ;
			this.rightChild = rightChild ;
			this.parent = parent ;
		}
		
		public int getKey()
		{
			return key ;
		}
		
		public String toString()
		{
		    String leftkey = (leftChild == null ? "" : String.valueOf(leftChild.key));
		    String rightkey = (rightChild == null ? "" : String.valueOf(rightChild.key));
		    return "("+leftkey+","+key+","+rightkey+")";
		}
	}
	
	/*
	 * 判断二叉查找树是否为空;若为空,返回true,否则返回false
	 * */
	public boolean isEmpty()
	{
		if(root == null){
			return true ;
		}else{
			return false ;
		}
	}
	
	/*
	 * 对于某些二叉查找树操作(比如删除关键字)来说,若树为空,则抛出异常。
	 * */
	public void TreeEmpty() throws Exception
	{
	     if(isEmpty()){
	    	 throw new Exception("树为空!"
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个二排序的完整实现,包括定义二排序的查找算法函数、二排序的插入算法函数、建立二排序的插入算法、二排序删除一个结点的操作实现、打印二排序: ```c #include <stdio.h> #include <stdlib.h> // 定义二排序结点的结构体 struct node { int data; struct node *left_child; struct node *right_child; }; // 查找函数 struct node* search(struct node *root, int data) { // 如果查找的结点为空,返回 NULL if (root == NULL) { return NULL; } // 如果查找的值等于根结点的值,返回根结点 if (data == root->data) { return root; } // 如果查找的值小于根结点的值,在左子中查找 if (data < root->data) { return search(root->left_child, data); } // 如果查找的值大于根结点的值,在右子中查找 else { return search(root->right_child, data); } } // 插入函数 struct node* insert(struct node *root, int data) { // 如果根结点为空,创建一个新结点并返回 if (root == NULL) { struct node *new_node = (struct node*)malloc(sizeof(struct node)); new_node->data = data; new_node->left_child = NULL; new_node->right_child = NULL; return new_node; } // 如果插入的值比根结点的值小,在左子中插入 if (data < root->data) { root->left_child = insert(root->left_child, data); } // 如果插入的值比根结点的值大,在右子中插入 else if (data > root->data) { root->right_child = insert(root->right_child, data); } // 如果插入的值等于根结点的值,返回根结点 else { return root; } return root; } // 建立二排序的插入算法 struct node* build_binary_search_tree(int data[], int n) { struct node *root = NULL; int i; for (i = 0; i < n; i++) { root = insert(root, data[i]); } return root; } // 删除函数 struct node* delete(struct node *root, int data) { struct node *temp; if (root == NULL) { printf("Element not found\n"); } else if (data < root->data) { root->left_child = delete(root->left_child, data); } else if (data > root->data) { root->right_child = delete(root->right_child, data); } else { if (root->left_child && root->right_child) { temp = find_minimum(root->right_child); root->data = temp->data; root->right_child = delete(root->right_child, root->data); } else { temp = root; if (root->left_child == NULL) { root = root->right_child; } else if (root->right_child == NULL) { root = root->left_child; } free(temp); } } return root; } // 找到子中的最小值结点 struct node* find_minimum(struct node *root) { if (root == NULL) { return NULL; } else if (root->left_child == NULL) { return root; } else { return find_minimum(root->left_child); } } // 打印二排序 void print_binary_search_tree(struct node *root) { if (root != NULL) { print_binary_search_tree(root->left_child); printf("%d ", root->data); print_binary_search_tree(root->right_child); } } // 测试插入、查找、删除、打印函数 int main() { int data[] = { 5, 3, 7, 1, 4, 6, 8 }; struct node *root = build_binary_search_tree(data, 7); printf("Binary search tree: "); print_binary_search_tree(root); printf("\n"); printf("Search for 6: %d\n", search(root, 6)->data); root = delete(root, 5); printf("Binary search tree after deleting 5: "); print_binary_search_tree(root); printf("\n"); return 0; } ``` 以上代码中,我们定义了一个二排序的结点结构体,包含了结点的值以及左右子结点的指针。然后定义了查找函数、插入函数、建立二排序的插入算法、删除函数、找到子中的最小值结点函数和打印二排序函数。最后我们测试了插入、查找、删除、打印函数,可以看到二排序的操作按照预期执行,且结果正确。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值