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. 廖雪峰网站:学习python基础知识(一)

    1. python能做什么? 可以做日常任务,比如自动备份你的MP3:可以做网站,很多著名的网站包括YouTube就是Python写的:可以做网络游戏的后台,很多在线游戏的后台都是Python开发的. ...

  2. Python编码处理和文件路径处理

    #变量编码格式化 a='我是中文' print(u'%s'%a)------------------ 结果:我是中文 引用网址 #变量编码格式化 a='我是中文' print(a.encode('ut ...

  3. leetcode-algorithms-26 Remove Duplicates from Sorted Array

    leetcode-algorithms-26 Remove Duplicates from Sorted Array Given a sorted array nums, remove the dup ...

  4. SVN 多分支管理

    SVN 新建时可以选择性的建立三个文件夹 trunk            一般作为主开发的地方 branches       一般作为从trunk Copy过去的代码,形成分支 tags       ...

  5. html 巧用data-for藏自定义属性

    <div class="form-ele"> <label for="week" class="label col-1"& ...

  6. 【转】jquery.validate.js表单验证

    一.用前必备官方网站:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ API: http://jquery.bassist ...

  7. android library打包成aar形式供别的项目引用

    1.我们项目已经有library存在,我们有需求是需要把library供其他项目引用,而且不能让其他项目随意更改我们项目的代码. 2.Rebuild Project 后zxinglib生成aar文件, ...

  8. system.setProperties

    System.setProperty("http.proxyHost", "localhost");System.setProperty("http. ...

  9. linux下uptime命令详解

    uptime uptime 另外还有一个参数 -V(大写),是用来查询版本的 procps是一个实用程序包,主要包括ps top kill等程序主要用来显示与控制一些系统信息,进程状态之类的内容. 以 ...

  10. learning scala control statement

    1 .if satement 与其它语言不同的是,scala if statement 返回的是一个值 scala> val a = if ( 6 > 0 ) 1 else -1a: In ...