java随机生成一棵二叉树_随机二叉树(Treap) Java实现

本文提供了一种使用Java实现随机二叉搜索树(Treap)的方法,包括插入、删除、查找等操作。Treap通过结合随机优先级和二叉搜索树的特性来保证操作的平均时间复杂度。
摘要由CSDN通过智能技术生成

importjava.util.ArrayList;importjava.util.LinkedList;importjava.util.List;importjava.util.Random;importjava.util.concurrent.Callable;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjava.util.concurrent.Future;importjava.util.concurrent.FutureTask;importjava.util.concurrent.locks.ReentrantLock;/*** 随机二叉树的 Java代码实现

*

*@authorxiemalin

**/publicclassTreapextendsReentrantLock {privateNode root;publicvoidinsert(floatkey,floatfix) {try{

Node node=newNode(key, fix);

lock();if(root==null) {

root=node;return;

}

insert(root, node);

}finally{

unlock();

}

}publicvoidset(floatkey,floatfix) {try{

Node node=newNode(key, fix);

lock();if(root==null) {

root=node;return;

}try{

insert(root, node);

}catch(KeyExistException e) {

remove(root, key);

insert(root, node);

}

}finally{

unlock();

}

}privatevoidinsert(Node root, Node node) {floatkey=node.key;if(key

root.left=newNode(node.key, node.fix);

}else{

insert(root.left, node);

}if(root.left.fix>root.fix) {

rotateRight(root);

}

}elseif(key>root.key) {if(root.right==null) {

root.right=newNode(node.key, node.fix);

}else{

insert(root.right, node);

}if(root.right.fix>root.fix) {

rotateLeft(root);

}

}else{//key exist ignorethrownewKeyExistException("key="+key+"already exist");//root.fix = node.fix;}

}publicNode remove(floatkey) {try{

lock();returnremove(root, key);

}finally{

unlock();

}

}publicNode remove(Node root,floatkey) {if(root==null) {returnnull;

}if(key

}elseif(key>root.key) {returnremove(root.right, key);

}else{if(root.left==null&&root.right==null) {

swapLocatePoint(root,null);returnroot;

}elseif(root.left==null) {

swapLocatePoint(root, root.right);returnroot;

}elseif(root.right==null) {

swapLocatePoint(root, root.left);returnroot;

}else{//has left and right nodeif(root.left.fix

rotateLeft(root);

root=find(root.key);returnremove(root, key);

}else{

rotateRight(root);

root=find(root.key);returnremove(root, key);

}

}

}

}publicNode find(floatkey) {returnfind(root, key);

}publicNode find(Node root,floatkey) {if(root==null) {returnnull;

}if(key==root.key) {returnroot;

}else{if(key

}else{returnfind(root.right, key);

}

}

}publicNode findParent(Node root,floatkey, Node parent) {if(root==null) {returnnull;

}if(key==root.key) {returnparent;

}else{if(key

}else{returnfindParent(root.right, key, root);

}

}

}/***   a

*    \

*     x

*    / \

*   nll y

*

* 左转之后的结果

*    a

*     \

*      y

*     / \

*   x null

*    \

*  (y.left=null)

*

*@paramx*/privatevoidrotateLeft(Node x) {//rotate to left on node x//左旋当前节点Node y=x.right;//把当前节点的右节点,复制给yx.right=y.left;//把当前节点的右节点的左子树复制给当前节点y.left=x;//swapLocatePoint(x, y);

}privatevoidswapLocatePoint(Node x, Node y) {

Node parent=findParent(root, x.key,null);if(parent==null) {

root=y;return;

}if(parent.left==x) {

parent.left=y;

}else{

parent.right=y;

}

}publicString toString() {if(root==null) {return"";

}

StringBuffer buffer=newStringBuffer(200);

flat(buffer, root);returnbuffer.toString();

}publicvoidflat(StringBuffer buffer, Node

9b8a8a44dd1c74ae49c20a7cd451974e.png nodes) {

buffer.append("\n");if(nodes!=null&&nodes.length>0) {

Listlist=newLinkedList();booleanhasValue=false;for(Node node : nodes) {

buffer.append(node).append("|");if(node.left!=null) {

list.add(node.left);

hasValue=true;

}else{

list.add(newEmptyNode());

}if(node.right!=null) {

list.add(node.right);

hasValue=true;

}else{

list.add(newEmptyNode());

}

}

buffer=buffer.deleteCharAt(buffer.length()-1);if(hasValue) {

flat(buffer, list.toArray(newTreap.Node[list.size()]));

}

}

}/***    a

*     \

*      x

*     / \

*    y null

*

* 右转之后的结果

*    a

*     \

*      y

*     / \

*   null x

*       /

*   (y.right=null)

*

*@paramx*/privatevoidrotateRight(Node x) {//rotate to right on node xNode y=x.left;

x.left=y.right;

y.right=x;

swapLocatePoint(x, y);

}publicstaticvoidmain(String[] args) {

Treap t=newTreap();

t.insert(0.0f,0.845f);

System.out.println(t);

t.insert(0.5f,0.763f);

System.out.println(t);

t.insert(0.25f,0.244f);

System.out.println(t);

t.insert(1.0f,0.347f);

System.out.println(t);

t.insert(1.25f,0.925f);

System.out.println(t);//System.out.println(t.remove(10));t.remove(10f);

System.out.println(t);finalTreap t2=newTreap();

t2.insert(9.0f,0.554f);

t2.insert(8.0f,0.701f);

t2.insert(12.5f,0.516f);

t2.insert(7.0f,0.141f);

t2.insert(4.0f,0.340f);

t2.insert(2.0f,0.286f);

t2.insert(3.0f,0.402f);

t2.insert(1.0f,0.646f);

t2.insert(5.0f,0.629f);

t2.insert(10.0f,0.244f);

t2.insert(11.0f,0.467f);

t2.insert(12.0f,0.794f);

t2.insert(13.0f,0.667f);

t2.insert(6.0f,0.375f);

System.out.println(t2);finalRandom r=newRandom();longtime=System.currentTimeMillis();

ExecutorService es=Executors.newFixedThreadPool(3);

Listfutures=newArrayList(3);for(inti=0; i<3; i++) {

FutureTaskfuture=newFutureTask(newCallable() {publicString call() {for(inti=0; i<1000000; i++) {

t2.set(r.nextFloat(), r.nextFloat());

}returnnull;

}});

es.execute(future);

futures.add(future);

}for(Future future : futures) {try{

future.get();

}catch(InterruptedException e) {//TODO Auto-generated catch blocke.printStackTrace();

}catch(ExecutionException e) {//TODO Auto-generated catch blocke.printStackTrace();

}

}

System.out.println("time took:"+(System.currentTimeMillis()-time));

time=System.currentTimeMillis();

System.out.println(t2.find(6.0f));

System.out.println("time took:"+(System.currentTimeMillis()-time));

es.shutdown();

}classNode {floatkey;floatfix;

Node left;

Node right;publicNode() {

}/***@paramkey

*@paramfix*/publicNode(floatkey,floatfix) {super();this.key=key;this.fix=fix;

}publicString toString() {returnkey+"";

}

}classEmptyNodeextendsNode {publicString toString() {return"NULL";

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值