数据结构与算法-二叉排序树

二叉排序树

定义
二叉排序树(Binary Sort Tree)又称二叉查找树、二叉搜索树。 它或者是一棵空树;或者是具有下列性质的二叉树:

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

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

(3)左、右子树也分别为二叉排序树;
在这里插入图片描述

思路

  • 添加节点
    1) 判断被插结点是其父亲结点的左、右儿子。将被插结点作为叶子结点插入。
    2) 若二叉树为空。则首先单独生成根结点。

  • 删除节点
    1)先去找到target节点
    2)找到target节点的父节点parent
    3)确定target是parent的左子节点L还是右子节点R
    4)根据前面的情况来对应删除

    删除叶子节点:
    左子节点parent.left = null
    右子节点 parent.right = null

    删除只有一颗子树的节点
    确定target的子节点是左子节点L还是右子节点R
    target是parent的左子节点L还是右子节点R
    target是parent的左子节点 -> target的子节点是左子节点
    parent.left = target.left
    其他情况依次类推

    删除有两颗子树的节点
    从target的右子树找到最小的节点
    用一个临时变量将最小节点的值保存temp
    删除该最小节点
    target.value = temp

代码实现

class BinarySortTree {
	
	Node root;

	public BinarySortTree() {
	}
	
	public void add(Node node) {
		if (root == null) {
			root = node;
		} else {
			root.add(node);
		}
	}
	
	public void infixOrder() {
		if (root != null) {
			root.infixOrder();
		} else {
			System.out.println("二叉排序树为空,不能遍历");
		}
	}
	
	public Node search(int value) {
		
		if (root == null) {
			return null;
		} else {
			return root.search(value);
		}
	}
	
	public Node searchParent(int value) {
		
		if (root == null) {
			return null;
		} else {
			return root.searchParent(value);
		}
	}
	
	public void delNode(int value) {
		if (root == null) {
			return;
		} else {
			Node targetNode = search(value);
			if (targetNode == null) {
				return;
			}
			
			//如果只可二叉排序树只有一个节点
			if (root.left == null && root.right == null) {
				root = null;
				return;
			}
			Node parent = searchParent(value);
			//删除叶子节点情况
			if (targetNode.left == null && targetNode.right == null) {
				if (parent.left != null && parent.left.value == value) {
					parent.left = null;
				} else if (parent.right != null && parent.right.value == value) {
					parent.right = null;
				}
			} else if (targetNode.left != null && targetNode.right != null) {
				targetNode.value = delRightTreeMin(targetNode.right);
			} else {
				if (parent == null) {
					if (targetNode.left != null) {
						root = targetNode.left;
					} else {
						root = targetNode.right;
					}
				} else {
					//如果target有左子节点
					if (targetNode.left != null) {
						if (parent.left.value == value) {
							parent.left = targetNode.left;
						} else {
							parent.right = targetNode.left;
						}
					} else {
						if (parent.left.value == value) {
							parent.left = targetNode.right;
						} else {
							parent.right = targetNode.right;
						}
					}
				}
			}
		}
	}
	
	/**
	 *   删除node节点
	 * @param node 当做二叉排序树的根节点
	 * @return	返回以node为根节点的二叉排序树右子树最小节点的值
	 */
	public int delRightTreeMin(Node node) {
		Node target = node;
		while (target.left != null) {
			target = target.left;
		}
		//这时temp指向了最小节点
		delNode(target.value);
		return target.value;
	}
	
}

class Node {
	
	int value;
	
	Node left;
	
	Node right;

	public Node(int value) {
		this.value = value;
	}
	
	//添加节点
	public void add(Node node) {
		//递归形式添加节点,需要满足二叉排序树的要求
		if (node == null) {
			return;
		}
		
		//判断传入节点的值和当前节点的值进行比较
		if (node.value < this.value) {
			if (this.left == null) {
				this.left = node;
			} else {
				this.left.add(node);
			}
		} else {
			if (this.right == null) {
				this.right = node;
			} else {
				this.right.add(node);
			}
		}
	}
	
	public void infixOrder() {
		
		if (this.left != null) {
			this.left.infixOrder();
		}
		
		System.out.println(this);
		
		if (this.right != null) {
			this.right.infixOrder();
		}
	}
	
	//查找要删除的节点
	public Node search(int value) {
		if (this.value == value) {
			return this;
		} else if (value < this.value) {
			if (this.left == null) {
				return null;
			}
			return this.left.search(value);
		} else {
			if (this.right == null) {
				return null;
			}
			return this.right.search(value);
		}
	}
	
	//查找要删除节点的父节点
	public Node searchParent(int value) {
		if ((this.left != null && this.left.value == value) 
				|| this.right != null && this.right.value == value) {
			return this;
		} else {
			if (value < this.value && this.left != null) {
				return this.left.searchParent(value);
			} else if (value >= this.value && this.right != null) {
				return this.right.searchParent(value);
			} else {
				return null;  //没有父节点
			}
		}
	}

	@Override
	public String toString() {
		return "Node [value=" + value + "]";
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
描述 用函数实现如下二叉排序树算法: (1) 插入新结点 (2) 前序、中序、后序遍历二叉树 (3) 中序遍历的非递归算法 (4) 层次遍历二叉树 (5) 在二叉树中查找给定关键字(函数返回值为成功1,失败0) (6) 交换各结点的左右子树 (7) 求二叉树的深度 (8) 叶子结点数 Input 第一行:准备建树的结点个数n 第二行:输入n个整数,用空格分隔 第三行:输入待查找的关键字 第四行:输入待查找的关键字 第五行:输入待插入的关键字 Output 第一行:二叉树的先序遍历序列 第二行:二叉树的中序遍历序列 第三行:二叉树的后序遍历序列 第四行:查找结果 第五行:查找结果 第六行~第八行:插入新结点后的二叉树的先、中、序遍历序列 第九行:插入新结点后的二叉树的中序遍历序列(非递归算法) 第十行:插入新结点后的二叉树的层次遍历序列 第十一行~第十三行:第一次交换各结点的左右子树后的先、中、后序遍历序列 第十四行~第十六行:第二次交换各结点的左右子树后的先、中、后序遍历序列 第十七行:二叉树的深度 第十八行:叶子结点数 Sample Input 7 40 20 60 18 50 56 90 18 35 30 Sample Output 40 20 18 60 50 56 90 18 20 40 50 56 60 90 18 20 56 50 90 60 40 1 0 40 20 18 30 60 50 56 90 18 20 30 40 50 56 60 90 18 30 20 56 50 90 60 40 18 20 30 40 50 56 60 90 40 20 60 18 30 50 90 56 40 60 90 50 56 20 30 18 90 60 56 50 40 30 20 18 90 56 50 60 30 18 20 40 40 20 18 30 60 50 56 90 18 20 30 40 50 56 60 90 18 30 20 56 50 90 60 40 4 4
二叉排序树,也称为二叉搜索树,是一种数据结构,它是一棵二叉树,其中每个节点都包含一个键值,且左子树中的所有键值都小于该节点的键值,右子树中的所有键值都大于该节点的键值。通过这种方式,我们可以快速地对数据进行查找、插入和删除操作。 在排序问题中,我们可以使用减治法来实现二叉排序树算法。具体来说,我们可以将排序问题分解为两个子问题:首先,将前半部分数据排序;然后,将后半部分数据排序。在排序过程中,我们可以利用二叉排序树的特性来建立一棵树,并将数据按照顺序插入到树中。当所有数据插入完毕后,我们可以通过遍历树来将数据按照升序输出。 下面是使用C++实现二叉排序树算法的示例代码: ```cpp #include <iostream> using namespace std; // 二叉排序树结构体 struct BSTNode { int data; // 数据 BSTNode* left; // 左子树指针 BSTNode* right; // 右子树指针 }; // 创建新节点 BSTNode* createNode(int data) { BSTNode* newNode = new BSTNode; newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; } // 插入节点 BSTNode* insertNode(BSTNode* root, int data) { if (root == NULL) { return createNode(data); } if (data < root->data) { root->left = insertNode(root->left, data); } else { root->right = insertNode(root->right, data); } return root; } // 中序遍历输出 void inorderTraversal(BSTNode* root) { if (root != NULL) { inorderTraversal(root->left); cout << root->data << " "; inorderTraversal(root->right); } } // 测试函数 int main() { int arr[] = { 6, 3, 8, 2, 5, 7, 9 }; int n = sizeof(arr) / sizeof(arr[0]); BSTNode* root = NULL; for (int i = 0; i < n; i++) { root = insertNode(root, arr[i]); } inorderTraversal(root); return 0; } ``` 在上述代码中,我们首先定义了一个二叉排序树结构体,并实现了创建节点、插入节点、中序遍历输出等函数。接着,我们定义了一个测试函数,用于测试二叉排序树算法的正确性。在测试函数中,我们首先定义了一个数据数组,并计算出数据个数。然后,我们通过循环将每个数据插入到二叉排序树中。最后,我们调用中序遍历函数来输出排序后的数据。 通过实验对比分析,我们可以发现,使用减治法实现二叉排序树算法的时间复杂度为O(nlogn),空间复杂度为O(n),与其他排序算法相比,效率较高。但是,在某些情况下,由于二叉排序树的不平衡性,可能会导致算法效率下降,因此我们需要采取一些措施来保证二叉排序树的平衡性,例如AVL树、红黑树等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值