public class binarytree<Value> {
private Node root = null;
private class Node{
private Value val;
private int key;
private Node left,right;
private int N; //the number of the all nodes of its child nodes and itself
//private int num;//the number
public Node(Value val,int key,int N){
this.val = val; this.N = N;this.key = key;
} } public void walk(Node x) { //O(n) time
if(x!=null){
walk(x.left);
System.out.println(x.key);
walk(x.right);
}
} public Value get(Node x,int key){
if(x == null){
return null;
} if(x.key>key){
return get(x.left,key);
}
else if(x.key < key){
return get(x.right,key);
}
else{
return x.val;
}
}
private Node min(Node x){
if(x.left == null) return x;
return min(x.left);
} private Node deletemin(Node x){
if(x.left == null){return x.right;}
x.left = deletemin(x.left);
x.N = size(x.left) + size(x.right) + 1;
return x;
} private Node delete(Node x,int key){
if(key < x.key) x.left = delete(x.left,key);
else if(key > x.key) x.right = delete(x.right,key);
else{
if(x.left == null) return x.right;
if(x.right == null) return x.left;
Node t = x;
x = min(t.right);
x.right = deletemin(t.right);
x.left = t.left;
}
x.N = size(x.left) +size(x.right)+1;
return x; } private Node put(Node x,Value val,int key){
if(x == null) return new Node(val,key,1);
if(key < x.key) x.left = put(x.left,val,key);
else if(key > x.key) x.right = put(x.right,val,key);
else x.key = key;
x.N = size(x.left) + size(x.right) + 1;
return x;
}
private int size(Node x){
if(x == null){return 0;}
else return x.N;
} private Node select(Node x,int k){
if(x == null) return null;
int t = size(x.left);
if (t>k) return select(x.left,k);
else if(t<k) return select (x.right,k-t-1);
else return x;
} }

binarysearchtree的更多相关文章

  1. 二叉搜索树BinarySearchTree(C实现)

    头文件—————————————————————————————— #ifndef _BINARY_SEARCH_TREE_H_ #define _BINARY_SEARCH_TREE_H_ #inc ...

  2. 二叉树终极教程--BinarySearchTree

    BinarySearchTreeMap 的 实现 public interface Map<K extends Comparable<K>, V> { void put(K k ...

  3. BinarySearchTree二叉搜索树的实现

    /* 二叉搜索树(Binary Search Tree),(又:二叉查找树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; ...

  4. Tree--二叉树BinarySearchTree

    BinarySearchTreeMap的实现 1 public interface Map<K extends Comparable<K>, V> { 2 void put(K ...

  5. 自己实现数据结构系列五---BinarySearchTree

    一.二分搜索树: 1.代码: public class BST<E extends Comparable<E>> { private class Node{ public E ...

  6. LeetCode108_Convert SortedArray to BinarySearchTree(将有序数组转成二叉排序树) Java题解

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  7. BinarySearchTree(二叉搜索树)原理及C++代码实现

    BST是一类用途极广的数据结构.它有如下性质:设x是二叉搜索树内的一个结点.如果y是x左子树中的一个结点,那么y.key<=x.key.如果y是x右子树中的一个结点,那么y.key>=x. ...

  8. <数据结构>BinarySearchTree的基本操作总结

    目录 ADT 结构声明 核心操作集 各操作实现 Find(),Insert(),Delete() Traverse():前中后.层 ADT 结构声明 #include<stdio.h> # ...

  9. 数据结构笔记--二叉查找树概述以及java代码实现

    一些概念: 二叉查找树的重要性质:对于树中的每一个节点X,它的左子树任一节点的值均小于X,右子树上任意节点的值均大于X. 二叉查找树是java的TreeSet和TreeMap类实现的基础. 由于树的递 ...

随机推荐

  1. 第二阶段——个人工作总结DAY09

    1.昨天做了什么:昨天学习了有关后台的知识. 2.今天打算做什么:实现后台. 3.遇到的困难:还是不知道该如何来做.

  2. 【微信公众号开发】【10】JSJDK相关

    前言: 1,优点:官方提供的,会调用后还算使用方便,不用费劲了解各个原生组件 缺点:使用上有限制(如:上传文件有大小限制),很容易踩坑,部分安卓手机及电脑端不支持pjax 总结:上手容易,坑很多 2, ...

  3. position属性的总结

    static 默认.位置设置为 static 的元素,它始终会处于页面流给予的位置(static 元素会忽略任何 top.bottom.left 或 right 声明). relative 位置被设置 ...

  4. python-day72--django实现的cookie/session

    COOKIE 与 SESSION 一.概念cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此cookie就是在这样一个场景下诞生.cookie ...

  5. jq post

    var source=[]; $.ajax({ type: "post", url: "connectdb/select.jsp", data: {databa ...

  6. 使用formData上传文件,ajax上传

    项目是vue项目,直接贴部分代码了: html: <input type="file" name="fileTable" @change="ch ...

  7. PAT Rational Sum

    Rational Sum (20) 时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 Given N ration ...

  8. [洛谷 P2709] 小B的询问

    P2709 小B的询问 题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数 ...

  9. 九、持久层框架(MyBatis)

    一.基于MyBatis的对象关系配置(基于XML方式的配置) 注: MyBatis不能像Hibernate那样,在实体类上配置上注解或者配置xml映射文件,系统启动后就可以自动创建表.因为MyBati ...

  10. linux LVM详解

    1.创建及删除步骤1)创建:linux partition-->pv-->vg-->lv-->fs-->mount2)删除:umount-->lv-->vg- ...