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. 【Java】【4】关于Java中的自增自减

    摘要:理解j = j++与j = ++j的区别:正确用法:直接用j++,不要用前两种 正文: import java.util.*; public class Test{ public static ...

  2. zsh切换bash bash切换zsh

    切换bash(需要sudo) chsh -s /bin/bash 切换zsh(不需要sudo) chsh -s /bin/zsh 注意:如果输入命令和密码后提示:no change made. 请加上 ...

  3. python-思路整理-虚拟环境

    虚拟环境: 如需多个版本的django或其他框架开发代码时,就可以用虚拟环境 pip3 install virtualenv # 创建虚拟环境 virtualenv virtualenv env1 # ...

  4. PAT 1035 Password

    1035 Password (20 分)   To prepare for PAT, the judge sometimes has to generate random passwords for ...

  5. 一、集合框架(关于ArrayList,LinkedList,HashSet,LinkedHashSet,TreeSet)

    一.ArrayList 解决了数组的局限性,最常见的容器类,ArrayList容器的容量capacity会随着对象的增加,自动增长.不会出现数组边界的问题. package collection;   ...

  6. Shelld5的使用

    Shelld的连接7步   ·   · huhu_k: 想和你相遇.

  7. C++ Leetcode Median of Two Sorted Arrays

    坚持每天刷一道题的小可爱还没有疯,依旧很可爱! 题目:There are two sorted arrays nums1 and nums2 of size m and n respectively. ...

  8. Tomcat和JDK版本的对应关系

    当我们在谈论Tomcat与JDK版本的对应关系的时候,我们实际上在讨论两个问题. 第一个是,我们想安装了某个版本的Tomcat(比如Tomcat7),需要安装哪个版本的JDK,才能把Tomcat运行起 ...

  9. Wireshark:No interfaces found解决方法(Windows 10)

    启动Wireshark时有时会报“No interfaces found”,找不到网卡进行截包.造成这种情况的原因可能有两个,一是npf服务没启动,二是当前用启对网卡没有拦截权限. 一.npf服务未启 ...

  10. VisualSVN+TortoiseSVN搭建版本控制系统教程

    Tortoise VisualSVN用作SVN的服务端,TortoiseSVN用作SVN的客户端. 一.安装和配置VisualSVN 1.1安装VisualSVN 下载链接:https://www.v ...