二分查找树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//创建内部类Node,Node代表的就是树节点
public  static  class  Node{
     int  key;
     Node leftChild;
     Node rightChild;
 
     public  Node( int  key){
         this .key=key;
         this .leftChild= null ;
         this .rightChild= null ;
     }
 
     public  Node( int  key,Node leftChild,Node rightChild){
         this .key=key;
         this .leftChild=leftChild;
         this .rightChild=rightChild;
     }
 
 
     public  int  getKey(){
         return  key;
     }
 
     @Override
     public  boolean   equals(Object o){
         Node node=(Node)o;
         return  getKey() == node.key;
     }
 
     public  Node left(){
         return  this .leftChild;
     }
 
     public  Node right(){
         return  this .rightChild;
     }
}

?
1
2
3
4
5
//成员变量以及构造函数
public  Node root;
public  BinarySearchTree(){
     root= null ;
}

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
public  boolean  isEmpty(){
     return  root ==  null  true  false ;
}
 
public  void  makeEmpty(){
     this .root= null ;
}
 
public  boolean  contains( int  target,Node node){
     if (node ==  null  || isEmpty()){
         return  false ;
     }
     if (target<node.key){
         return  contains(target, node.leftChild);
     } else  if (target>node.key){
         return  contains(target,node.rightChild);
     } else {
         return  true ;
     }
}
 
public  Node findMin(Node node){
     if (node== null  || isEmpty()){
         return  null ;
     }
     if (node!= null  && node.leftChild!= null ) {
         return  findMin(node.leftChild);
     } else {
         return  node;
     }
}
 
public  Node findMax(Node node){
     if (node== null  || isEmpty()){
         return  null ;
     }
     //这就是返回条件(递归结束条件)
     if (node.rightChild ==  null )
         return  node;
     return  findMax(node.rightChild);
}
 
//整体思路就是插入哪一个节点,就返回哪一个节点
public  Node insert( int  key,Node node){
 
     if (node== null ){
         //用于真实生成节点(递归结束条件)
         return  new  Node(key, null , null );
     } else  if (key < node.key){
         //用于建立与左节点间的关联
         node.leftChild=insert(key, node.leftChild);
     } else  if (key > node.key){
         //用于建立与右节点间的关联
         node.rightChild=insert(key, node.rightChild);
     } else  ;
 
     return  node;
}
 
public  void  insert( int  key){
     root=insert(key,root);
}
//思路与插入思路差不多,删除那个节点就返回哪一个节点
public  Node remove( int  key,Node node){
 
     if (node ==  null return  null ;
     if (key < node.key){
         node.leftChild=remove(key, node.leftChild);
     } else  if (key > node.key){
         node.rightChild=remove(key, node.rightChild);
     }
     //左右子树均非空
     else  if (node.leftChild !=  null  && node.rightChild !=  null ){
         //找到要移动的节点并替换掉,该过程出现新一轮的树枝生成过程
         node.key=findMin(root.rightChild).key;
         //这里负责新树枝的生成,因为这里要移动的是该节点的右分支,所以其实永远不会有删除动作发生,只会发生一点树枝的移动动作
         node.rightChild=remove(node.key, node.rightChild);
     }
     //左子树或者右子树为空,此时node节点即为要删除的节点或者不存在的删除节点
     else {
         node = (node.rightChild ==  null ) ? node.leftChild : node.rightChild;
     }
 
     return  node;
}
 
public  void  remove( int  key){
     root=remove(key, root);
}

以上就简单介绍了二人查找数的基本操作,当然用的是递归实现,但是也可以用到非递归的方法,这里不再给出。

平衡二分查找树,也称为平衡二叉查找树,是一种特殊的二叉树,属于B - 树在M = 2时的情况,是相当有用且重要的树数据结构,在涉及硬盘数据的大量数据查找操作场景中应用广泛[^1]。 ### 原理 它是一种自平衡的二叉搜索树,在二叉搜索树的基础上,通过特定的平衡机制来保证树的左右子树高度差不超过某个固定值(通常为1)。当对树进行插入或删除操作导致树的平衡被打破时,会通过旋转等操作重新调整树的结构,以维持树的平衡状态,从而保证树的高度始终保持在对数级别,进而保证查找、插入和删除操作的时间复杂度稳定在O(log n)。 ### 特点 - **平衡性**:树的左右子树高度差不超过1,这使得树的结构相对均匀,避免了二叉搜索树可能出现的退化为链表的情况,保证了较好的查找性能。 - **有序性**:满足二叉搜索树的性质,即左子树中的所有节点值小于根节点值,右子树中的所有节点值大于根节点值,这使得查找操作可以根据节点值的大小进行有方向的搜索,提高查找效率。 - **查找性能稳定**:由于树的平衡性,无论在最好情况、最坏情况还是平均情况下,查找时间复杂度都能保持在O(log n),避免了普通二叉搜索树在最坏情况下查找时间复杂度退化为O(n)的问题[^3]。 ### 应用 - **数据库索引**:在数据库系统中,平衡二分查找树可用于实现索引结构,如B - 树和B+树在本质上是对平衡二分查找树的扩展和优化,用于加速数据的查找、插入和删除操作,减少磁盘I/O次数,提高数据库的性能。 - **文件系统**:文件系统中也会使用类似的平衡树结构来管理文件和目录的索引,使得文件的查找和定位更加高效。 - **缓存系统**:在缓存系统中,平衡二分查找树可以用于实现缓存淘汰策略,根据数据的访问频率或时间等因素对缓存数据进行排序和管理,提高缓存的命中率。 ```python # 以下是一个简单的平衡二分查找树(AVL树)的Python实现示例 class TreeNode: def __init__(self, key): self.key = key self.left = None self.right = None self.height = 1 class AVLTree: def insert(self, root, key): if not root: return TreeNode(key) elif key < root.key: root.left = self.insert(root.left, key) else: root.right = self.insert(root.right, key) root.height = 1 + max(self.get_height(root.left), self.get_height(root.right)) balance = self.get_balance(root) # 左左情况 if balance > 1 and key < root.left.key: return self.right_rotate(root) # 右右情况 if balance < -1 and key > root.right.key: return self.left_rotate(root) # 左右情况 if balance > 1 and key > root.left.key: root.left = self.left_rotate(root.left) return self.right_rotate(root) # 右左情况 if balance < -1 and key < root.right.key: root.right = self.right_rotate(root.right) return self.left_rotate(root) return root def left_rotate(self, z): y = z.right T2 = y.left y.left = z z.right = T2 z.height = 1 + max(self.get_height(z.left), self.get_height(z.right)) y.height = 1 + max(self.get_height(y.left), self.get_height(y.right)) return y def right_rotate(self, z): y = z.left T3 = y.right y.right = z z.left = T3 z.height = 1 + max(self.get_height(z.left), self.get_height(z.right)) y.height = 1 + max(self.get_height(y.left), self.get_height(y.right)) return y def get_height(self, root): if not root: return 0 return root.height def get_balance(self, root): if not root: return 0 return self.get_height(root.left) - self.get_height(root.right) # 示例使用 avl = AVLTree() root = None keys = [9, 5, 10, 0, 6, 11, -1, 1, 2] for key in keys: root = avl.insert(root, key) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值