212ⅹ5+288x5四年级下册简便计算算

loading...
变&速&箱:
驱动方式:
进气形式:
车体结构:
座&位&数:
新车抢先看
14.2-19.8万
10.78-15.38万
9.98-15.18万
23.28-36.38万
10.99-13.09万
7.69-12.79万
关注排行榜
139.88万起
199.98万起
2250.0万起
热门城市4S店
热门品牌4S店
最近看过的车当前位置: >>
五位数除以三位数竖式练习题
五位数除以三位数竖式练习题8= 9= 4=4=6=2=6=6=2=9=4=5=1=9=2=1 7=4=4=5=3=9=6=8=8=3=5=6=3=3=6=2 8=6=9=6=7=7=3=5=3=4=5=5=4=6=2=3 1=7=4=7=1=1=7=8=5=8=5=3=5=6=7=4 1=9=6=4=2=4=3=6=1=2=4=3=9=4=3=5 6=3=8=6=6=8=4=3=4=4=2=9=9=8=6=6 2=6=4=5=4=3=1=6=1=3=3=3=3=7=6=7 9=3=7=6=5=9=6=1=7=4=5=1=2=3=6=8 4=1=4=3=5=5=3=6=5=4=4=1=2=3=1=9 8=8=5=6=3=4=2=2=6=7=3=8=3=9=4=10 3=6=2=1=7=9=7=3=5=6=1=1=5=3=9=11 3=1=9=8=3=6=4=8=3=4=9=9=4=6=1=12 4=4=2=2=3=8=3=8=3=9=5=6=8=9=7=13 9=7=5=6=3=9=4=5=5=3=8=8=7=1=3=14 1=9=5=2=2=6=1=6=3=3=4=8=4=6=1=15 1=3=4=1=3=8=2=3=2=5=6=9=4=9=4=16 3=1=5=4=7=2=4=2=8=7=1=5=4=2=4=17 2=1=8=4=1=9=8=9=7=2=9=8=2=1=9=18 7=1=8=1=1=5=2=6=3=1=7=5=8=7=4=19 3=8=3=9=5=4=9=8=4=4=2=9=9=9=3=20 答案:8= 4= 6= 9= 1= 7= 5= 6= 3= 3= 8= 6= 3= 4= 4= 1= 7= 7= 8=112......660 148......117 78......465 123......165 19......460 188......107 217......54 175......4 224......42 174......14 141......172 100......343 55......554 42......637 197......65 28......318 251......34 98......166 180......1309= 6= 6= 4= 9= 4= 3= 8= 5= 3= 6= 7= 5= 5= 6= 7= 1= 8= 5=302......37 93......168 140......203 82......301 224......10 60......759 196......137 224......19 107......153 50......226 238......119 106......275 91......492 90......520 290......145 147......451 15......498 291......224 1254= 2= 2= 5= 2= 4= 9= 8= 6= 6= 9= 7= 3= 5= 2= 4= 1= 5= 3=70......255 146......65 49......134 83......633 42......307 65......737 276......123 318......177 502......47 68......700 236......83 91......495 74......268 405......10 63......289 119......40 27......63 59......207 39......22021 5= 1= 4= 3= 2= 9= 6= 6= 4= 4= 9= 2= 5= 1= 3= 3= 9= 6= 6= 4= 2=101......4 43......260 109......345 44......185 74......34 158......293 69......234 94......557 54......140 57......390 101......523 35......359 372......95 16......198 38......909 49......189 105......676 122......325 88......315 280......118 89......836= 9= 2= 6= 4= 4= 3= 6= 3= 2= 8= 6= 4= 6= 3= 7= 3= 5= 1= 5= 3=370......171 244......318 100......228 98......177 73......262 439......100 120......152 89......481 48......51 39......128 155......72 75......39 86......229 112......413 84......242 214......232 39......166 250......116 28......680 89......60 38......6547= 6= 4= 1= 3= 3= 8= 8= 4= 9= 6= 4= 3= 1= 3= 6= 7= 9= 7= 1= 6=222......291 159......399 73......270 122......7 42......102 132......106 412......81 462......108 53......607 308......59 134......109 292......131 37......892 16......726 93......80 99......500 170......68 375......122 95......241 32......298 185......19422 4= 3= 3= 4= 2= 8= 6= 2= 7= 3= 3= 1= 7= 6= 5= 3= 8= 4= 4= 4= 4=90......397 135......14 208......68 75......447 29......309 152......124 167......122 32......65 360......51 121......291 39......972 15......608 164......212 86......582 61......489 35......637 84......445 316......150 74......255 59......427 90......261= 5= 6= 4= 3= 8= 3= 2= 3= 9= 6= 7= 3= 1= 3= 1= 3= 8= 9= 6= 4=30......328 135......333 158......77 293......29 150......217 83......286 49......70 51......101 366......84 354......201 108......229 234......82 48......63 65......199 73......348 73......182 45......201 361......236 156......459 123......141 64......1994= 5= 5= 1= 1= 5= 4= 6= 8= 4= 2= 9= 5= 1= 9= 9= 6= 3= 9= 1= 2=51......915 123......77 66......232 85......214 19......63 355......58 103......348 109......59 86......614 111......255 24......276 182......160 115......377 130......37 169......232 113......730 68......185 50......69 122......236 105......32 53......20623 2= 3= 9= 8= 9= 6= 4= 3= 7= 1= 2= 1= 3= 4= 1= 1= 2= 5= 4= 3= 4=24......715 40......240 99......95 111......45 108......469 245......54 46......60 222......31 84......903 16......144 103......238 64......20 38......537 67......118 64......209 16......551 31......507 84......365 305......86 54......319 189......463= 8= 5= 9= 7= 3= 5= 8= 1= 9= 2= 6= 4= 6= 3= 3= 3= 6= 9= 1= 7=228......70 89......652 61......350 380......158 100......180 73......2 56......122 143......376 17......212 294......131 33......394 130......243 56......270 111......202 78......51 214......131 39......201 340......7 165......352 16......690 254......2868= 3= 6= 7= 5= 9= 5= 8= 3= 5= 6= 3= 8= 1= 4= 8= 2= 9= 4= 5= 2=111......399 66......336 128......212 113......675 68......682 102......824 62......554 105......605 87......343 143......241 72......37 42......19 127......481 17......744 55......253 603......35 95......130 213......296 49......702 88......287 74......6224 4= 7= 4= 2= 4= 8= 2= 2= 7= 1= 2= 1= 8= 3= 9= 9= 4= 9=62......587 217......219 62......234 45......57 85......134 577......38 101......193 68......283 86......238 29......188 74......207 10......688 185......112 188......1 136......311 163......402 116......132 138......4062= 1= 2= 1= 1= 9= 9= 1= 1= 1= 6= 7= 7= 8= 5= 8= 2= 9=41......487 24......483 26......664 16......682 14......581 97......26 133......577 32......323 21......268 25......316 200......210 157......52 168......30 96......480 63......597 123......644 37......220 115......2948= 5= 4= 8= 9= 7= 8= 9= 8= 5= 3= 5= 4= 3= 4= 4= 9= 3=136......572 76......580 51......782 234......253 532......3 100......81 418......210 289......130 295......233 54......348 207......74 99......475 143......42 83......320 171......67 42......518 447......150 67......38425
苏教版三年级数学上三位数除以一位数竖式计算500道练习题 - 488×8= 875÷7= 636×6= 875÷5= 276÷4= 177×3= 672÷2= 576×9= 96...最新苏教三年级三位数除以一位数竖式计算题 - 488÷8= 875÷7= 636÷6= 875÷5= 276÷4= 77÷3= 672÷2= 576÷9= 968÷8= 567...三年级下册一位数除多位数笔算除法专项练习题 - 一位数除三位数笔算除法练习题(1) 竖式计算并验算 156÷3= 434÷8= 605÷5= 863÷7= 172÷4= 543÷3=....
All rights reserved Powered by
www.tceic.com
copyright &copyright 。文档资料库内容来自网络,如有侵犯请联系客服。333+121×212+333=简便运算_百度知道
333+121×212+333=简便运算
333+121×212+333=简便运算
我有更好的答案
333+121×212+333=3x111+111x212+10x212+3x111=111x(3+212+3)+2120=111x218+2120=+218+2120==26318
采纳率:88%
来自团队:
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。红黑树(五)之 Java的实现 - 如果天空不死 - 博客园
随笔 - 278
评论 - 1094
前面分别介绍红黑树的、红黑树的和。本章介绍红黑树的Java实现,若读者对红黑树的理论知识不熟悉,建立先学习,再来学习本章。还是那句老话,红黑树的C/C++/Java实现,原理一样,择其一了解即可。
目录1. 2. 3. 4.
转载请注明出处:
更多内容:
(01)&(02)&(03)&(04)&(05)&(06)&
红黑树的介绍
红黑树(Red-Black Tree,简称R-B Tree),它一种特殊的二叉查找树。红黑树是特殊的二叉查找树,意味着它满足二叉查找树的特征:任意一个节点所包含的键值,大于等于左孩子的键值,小于等于右孩子的键值。除了具备该特性之外,红黑树还包括许多额外的信息。
红黑树的每个节点上都有存储位表示节点的颜色,颜色是红(Red)或黑(Black)。红黑树的特性:(1) 每个节点或者是黑色,或者是红色。(2) 根节点是黑色。(3) 每个叶子节点是黑色。 [注意:这里叶子节点,是指为空的叶子节点!](4) 如果一个节点是红色的,则它的子节点必须是黑色的。(5) 从一个节点到该节点的子孙节点的所有路径上包含相同数目的黑节点。
关于它的特性,需要注意的是:第一,特性(3)中的叶子节点,是只为空(NIL或null)的节点。第二,特性(5),确保没有一条路径会比其他路径长出俩倍。因而,红黑树是相对是接近平衡的二叉树。
红黑树示意图如下:
红黑树的Java实现(代码说明)
红黑树的基本操作是添加、删除和旋转。在对红黑树进行添加或删除后,会用到旋转方法。为什么呢?道理很简单,添加或删除红黑树中的节点之后,红黑树就发生了变化,可能不满足红黑树的5条性质,也就不再是一颗红黑树了,而是一颗普通的树。而通过旋转,可以使这颗树重新成为红黑树。简单点说,旋转的目的是让树保持红黑树的特性。旋转包括两种:左旋 和 右旋。下面分别对红黑树的基本操作进行介绍。
1. 基本定义
public class RBTree&T extends Comparable&T&& {
private RBTNode&T& mR
private static final boolean RED
private static final boolean BLACK = true;
public class RBTNode&T extends Comparable&T&& {
// 关键字(键值)
RBTNode&T&
RBTNode&T&
RBTNode&T&
public RBTNode(T key, boolean color, RBTNode&T& parent, RBTNode&T& left, RBTNode&T& right) {
this.key =
this.color =
this.parent =
this.left =
this.right =
RBTree是红黑树对应的类,RBTNode是红黑树的节点类。在RBTree中包含了根节点mRoot和红黑树的相关API。注意:在实现红黑树API的过程中,我重载了许多函数。重载的原因,一是因为有的API是内部接口,有的是外部接口;二是为了让结构更加清晰。
对x进行左旋,意味着"将x变成一个左节点"。
左旋的实现代码(Java语言)
* 对红黑树的节点(x)进行左旋转
* 左旋示意图(对节点x进行左旋):
--(左旋)-.
private void leftRotate(RBTNode&T& x) {
// 设置x的右孩子为y
RBTNode&T& y = x.
// 将 &y的左孩子& 设为 &x的右孩子&;
// 如果y的左孩子非空,将 &x& 设为 &y的左孩子的父亲&
x.right = y.
if (y.left != null)
y.left.parent =
// 将 &x的父亲& 设为 &y的父亲&
y.parent = x.
if (x.parent == null) {
this.mRoot =
// 如果 &x的父亲& 是空节点,则将y设为根节点
if (x.parent.left == x)
x.parent.left =
// 如果 x是它父节点的左孩子,则将y设为&x的父节点的左孩子&
x.parent.right =
// 如果 x是它父节点的左孩子,则将y设为&x的父节点的左孩子&
// 将 &x& 设为 &y的左孩子&
// 将 &x的父节点& 设为 &y&
x.parent =
对y进行左旋,意味着"将y变成一个右节点"。
右旋的实现代码(Java语言)
* 对红黑树的节点(y)进行右旋转
* 右旋示意图(对节点y进行左旋):
--(右旋)-.
private void rightRotate(RBTNode&T& y) {
// 设置x是当前节点的左孩子。
RBTNode&T& x = y.
// 将 &x的右孩子& 设为 &y的左孩子&;
// 如果"x的右孩子"不为空的话,将 &y& 设为 &x的右孩子的父亲&
y.left = x.
if (x.right != null)
x.right.parent =
// 将 &y的父亲& 设为 &x的父亲&
x.parent = y.
if (y.parent == null) {
this.mRoot =
// 如果 &y的父亲& 是空节点,则将x设为根节点
if (y == y.parent.right)
y.parent.right =
// 如果 y是它父节点的右孩子,则将x设为&y的父节点的右孩子&
y.parent.left =
// (y是它父节点的左孩子) 将x设为&x的父节点的左孩子&
// 将 &y& 设为 &x的右孩子&
// 将 &y的父节点& 设为 &x&
y.parent =
将一个节点插入到红黑树中,需要执行哪些步骤呢?首先,将红黑树当作一颗二叉查找树,将节点插入;然后,将节点着色为红色;最后,通过"旋转和重新着色"等一系列操作来修正该树,使之重新成为一颗红黑树。详细描述如下:第一步: 将红黑树当作一颗二叉查找树,将节点插入。& & & &红黑树本身就是一颗二叉查找树,将节点插入后,该树仍然是一颗二叉查找树。也就意味着,树的键值仍然是有序的。此外,无论是左旋还是右旋,若旋转之前这棵树是二叉查找树,旋转之后它一定还是二叉查找树。这也就意味着,任何的旋转和重新着色操作,都不会改变它仍然是一颗二叉查找树的事实。
好吧?那接下来,我们就来想方设法的旋转以及重新着色,使这颗树重新成为红黑树!
第二步:将插入的节点着色为"红色"。& & & &为什么着色成红色,而不是黑色呢?为什么呢?在回答之前,我们需要重新温习一下红黑树的特性:(1) 每个节点或者是黑色,或者是红色。(2) 根节点是黑色。(3) 每个叶子节点是黑色。 [注意:这里叶子节点,是指为空的叶子节点!](4) 如果一个节点是红色的,则它的子节点必须是黑色的。(5) 从一个节点到该节点的子孙节点的所有路径上包含相同数目的黑节点。& & & 将插入的节点着色为红色,不会违背"特性(5)"!少违背一条特性,就意味着我们需要处理的情况越少。接下来,就要努力的让这棵树满足其它性质即可;满足了的话,它就又是一颗红黑树了。o(&&)o...哈哈
第三步: 通过一系列的旋转或着色等操作,使之重新成为一颗红黑树。& & & &第二步中,将插入节点着色为"红色"之后,不会违背"特性(5)"。那它到底会违背哪些特性呢?& & & &对于"特性(1)",显然不会违背了。因为我们已经将它涂成红色了。& & & &对于"特性(2)",显然也不会违背。在第一步中,我们是将红黑树当作二叉查找树,然后执行的插入操作。而根据二叉查找数的特点,插入操作不会改变根节点。所以,根节点仍然是黑色。& & & &对于"特性(3)",显然不会违背了。这里的叶子节点是指的空叶子节点,插入非空节点并不会对它们造成影响。& & & &对于"特性(4)",是有可能违背的!& & & &那接下来,想办法使之"满足特性(4)",就可以将树重新构造成红黑树了。
添加操作的实现代码(Java语言)
* 将结点插入到红黑树中
* 参数说明:
node 插入的结点
// 对应《算法导论》中的node
private void insert(RBTNode&T& node) {
RBTNode&T& y = null;
RBTNode&T& x = this.mR
// 1. 将红黑树当作一颗二叉查找树,将节点添加到二叉查找树中。
while (x != null) {
cmp = node.key.compareTo(x.key);
if (cmp & 0)
node.parent =
if (y!=null) {
cmp = node.key.compareTo(y.key);
if (cmp & 0)
this.mRoot =
// 2. 设置节点的颜色为红色
node.color = RED;
// 3. 将它重新修正为一颗二叉查找树
insertFixUp(node);
* 新建结点(key),并将其插入到红黑树中
* 参数说明:
key 插入结点的键值
public void insert(T key) {
RBTNode&T& node=new RBTNode&T&(key,BLACK,null,null,null);
// 如果新建结点失败,则返回。
if (node != null)
insert(node);
内部接口 -- insert(node)的作用是将"node"节点插入到红黑树中。外部接口 -- insert(key)的作用是将"key"添加到红黑树中。
添加修正操作的实现代码(Java语言)
* 红黑树插入修正函数
* 在向红黑树中插入节点之后(失去平衡),再调用该函数;
* 目的是将它重新塑造成一颗红黑树。
* 参数说明:
node 插入的结点
// 对应《算法导论》中的z
private void insertFixUp(RBTNode&T& node) {
RBTNode&T& parent,
// 若&父节点存在,并且父节点的颜色是红色&
while (((parent = parentOf(node))!=null) && isRed(parent)) {
gparent = parentOf(parent);
//若&父节点&是&祖父节点的左孩子&
if (parent == gparent.left) {
// Case 1条件:叔叔节点是红色
RBTNode&T& uncle = gparent.
if ((uncle!=null) && isRed(uncle)) {
setBlack(uncle);
setBlack(parent);
setRed(gparent);
// Case 2条件:叔叔是黑色,且当前节点是右孩子
if (parent.right == node) {
RBTNode&T&
leftRotate(parent);
// Case 3条件:叔叔是黑色,且当前节点是左孩子。
setBlack(parent);
setRed(gparent);
rightRotate(gparent);
//若&z的父节点&是&z的祖父节点的右孩子&
// Case 1条件:叔叔节点是红色
RBTNode&T& uncle = gparent.
if ((uncle!=null) && isRed(uncle)) {
setBlack(uncle);
setBlack(parent);
setRed(gparent);
// Case 2条件:叔叔是黑色,且当前节点是左孩子
if (parent.left == node) {
RBTNode&T&
rightRotate(parent);
// Case 3条件:叔叔是黑色,且当前节点是右孩子。
setBlack(parent);
setRed(gparent);
leftRotate(gparent);
// 将根节点设为黑色
setBlack(this.mRoot);
insertFixUp(node)的作用是对应"上面所讲的第三步"。它是一个内部接口。
5. 删除操作
将红黑树内的某一个节点删除。需要执行的操作依次是:首先,将红黑树当作一颗二叉查找树,将该节点从二叉查找树中删除;然后,通过"旋转和重新着色"等一系列来修正该树,使之重新成为一棵红黑树。详细描述如下:第一步:将红黑树当作一颗二叉查找树,将节点删除。& & & &这和"删除常规二叉查找树中删除节点的方法是一样的"。分3种情况:
① 被删除节点没有儿子,即为叶节点。那么,直接将该节点删除就OK了。
② 被删除节点只有一个儿子。那么,直接删除该节点,并用该节点的唯一子节点顶替它的位置。
③ 被删除节点有两个儿子。那么,先找出它的后继节点;然后把&它的后继节点的内容&复制给&该节点的内容&;之后,删除&它的后继节点&。在这里,后继节点相当于替身,在将后继节点的内容复制给"被删除节点"之后,再将后继节点删除。这样就巧妙的将问题转换为"删除后继节点"的情况了,下面就考虑后继节点。 在"被删除节点"有两个非空子节点的情况下,它的后继节点不可能是双子非空。既然"的后继节点"不可能双子都非空,就意味着"该节点的后继节点"要么没有儿子,要么只有一个儿子。若没有儿子,则按"情况① "进行处理;若只有一个儿子,则按"情况② "进行处理。
第二步:通过"旋转和重新着色"等一系列来修正该树,使之重新成为一棵红黑树。& & & & 因为"第一步"中删除节点之后,可能会违背红黑树的特性。所以需要通过"旋转和重新着色"来修正该树,使之重新成为一棵红黑树。
删除操作的实现代码(Java语言)
* 删除结点(node),并返回被删除的结点
* 参数说明:
node 删除的结点
private void remove(RBTNode&T& node) {
RBTNode&T& child,
// 被删除节点的"左右孩子都不为空"的情况。
if ( (node.left!=null) && (node.right!=null) ) {
// 被删节点的后继节点。(称为"取代节点")
// 用它来取代"被删节点"的位置,然后再将"被删节点"去掉。
RBTNode&T& replace =
// 获取后继节点
replace = replace.
while (replace.left != null)
replace = replace.
// "node节点"不是根节点(只有根节点不存在父节点)
if (parentOf(node)!=null) {
if (parentOf(node).left == node)
parentOf(node).left =
parentOf(node).right =
// "node节点"是根节点,更新根节点。
this.mRoot =
// child是"取代节点"的右孩子,也是需要"调整的节点"。
// "取代节点"肯定不存在左孩子!因为它是一个后继节点。
child = replace.
parent = parentOf(replace);
// 保存"取代节点"的颜色
color = colorOf(replace);
// "被删除节点"是"它的后继节点的父节点"
if (parent == node) {
// child不为空
if (child!=null)
setParent(child, parent);
parent.left =
replace.right = node.
setParent(node.right, replace);
replace.parent = node.
replace.color = node.
replace.left = node.
node.left.parent =
if (color == BLACK)
removeFixUp(child, parent);
node = null;
if (node.left !=null) {
child = node.
child = node.
parent = node.
// 保存"取代节点"的颜色
color = node.
if (child!=null)
child.parent =
// "node节点"不是根节点
if (parent!=null) {
if (parent.left == node)
parent.left =
parent.right =
this.mRoot =
if (color == BLACK)
removeFixUp(child, parent);
node = null;
* 删除结点(z),并返回被删除的结点
* 参数说明:
tree 红黑树的根结点
z 删除的结点
public void remove(T key) {
RBTNode&T&
if ((node = search(mRoot, key)) != null)
remove(node);
内部接口 -- remove(node)的作用是将"node"节点插入到红黑树中。外部接口 -- remove(key)删除红黑树中键值为key的节点。
删除修正操作的实现代码(Java语言)
* 红黑树删除修正函数
* 在从红黑树中删除插入节点之后(红黑树失去平衡),再调用该函数;
* 目的是将它重新塑造成一颗红黑树。
* 参数说明:
node 待修正的节点
private void removeFixUp(RBTNode&T& node, RBTNode&T& parent) {
RBTNode&T&
while ((node==null || isBlack(node)) && (node != this.mRoot)) {
if (parent.left == node) {
other = parent.
if (isRed(other)) {
// Case 1: x的兄弟w是红色的
setBlack(other);
setRed(parent);
leftRotate(parent);
other = parent.
if ((other.left==null || isBlack(other.left)) &&
(other.right==null || isBlack(other.right))) {
// Case 2: x的兄弟w是黑色,且w的俩个孩子也都是黑色的
setRed(other);
parent = parentOf(node);
if (other.right==null || isBlack(other.right)) {
// Case 3: x的兄弟w是黑色的,并且w的左孩子是红色,右孩子为黑色。
setBlack(other.left);
setRed(other);
rightRotate(other);
other = parent.
// Case 4: x的兄弟w是黑色的;并且w的右孩子是红色的,左孩子任意颜色。
setColor(other, colorOf(parent));
setBlack(parent);
setBlack(other.right);
leftRotate(parent);
node = this.mR
other = parent.
if (isRed(other)) {
// Case 1: x的兄弟w是红色的
setBlack(other);
setRed(parent);
rightRotate(parent);
other = parent.
if ((other.left==null || isBlack(other.left)) &&
(other.right==null || isBlack(other.right))) {
// Case 2: x的兄弟w是黑色,且w的俩个孩子也都是黑色的
setRed(other);
parent = parentOf(node);
if (other.left==null || isBlack(other.left)) {
// Case 3: x的兄弟w是黑色的,并且w的左孩子是红色,右孩子为黑色。
setBlack(other.right);
setRed(other);
leftRotate(other);
other = parent.
// Case 4: x的兄弟w是黑色的;并且w的右孩子是红色的,左孩子任意颜色。
setColor(other, colorOf(parent));
setBlack(parent);
setBlack(other.left);
rightRotate(parent);
node = this.mR
if (node!=null)
setBlack(node);
removeFixup(node, parent)是对应"上面所讲的第三步"。它是一个内部接口。
红黑树的Java实现(完整源码)
下面是红黑树实现的完整代码和相应的测试程序。(1) 除了上面所说的"左旋"、"右旋"、"添加"、"删除"等基本操作之后,还实现了"遍历"、"查找"、"打印"、"最小值"、"最大值"、"创建"、"销毁"等接口。(2) 函数接口大多分为内部接口和外部接口。内部接口是private函数,外部接口则是public函数。(3) 测试代码中提供了"插入"和"删除"动作的检测开关。默认是关闭的,打开方法可以参考"代码中的说明"。建议在打开开关后,在草稿上自己动手绘制一下红黑树。
红黑树的实现文件(RBTree.java)
* Java 语言: 红黑树
* @author skywang
8 public class RBTree&T extends Comparable&T&& {
private RBTNode&T& mR
private static final boolean RED
private static final boolean BLACK = true;
public class RBTNode&T extends Comparable&T&& {
// 关键字(键值)
RBTNode&T&
RBTNode&T&
RBTNode&T&
public RBTNode(T key, boolean color, RBTNode&T& parent, RBTNode&T& left, RBTNode&T& right) {
this.key =
this.color =
this.parent =
this.left =
this.right =
public T getKey() {
public String toString() {
return ""+key+(this.color==RED?"(R)":"B");
public RBTree() {
mRoot=null;
private RBTNode&T& parentOf(RBTNode&T& node) {
return node!=null ? node.parent : null;
private boolean colorOf(RBTNode&T& node) {
return node!=null ? node.color : BLACK;
private boolean isRed(RBTNode&T& node) {
return ((node!=null)&&(node.color==RED)) ? true : false;
private boolean isBlack(RBTNode&T& node) {
return !isRed(node);
private void setBlack(RBTNode&T& node) {
if (node!=null)
node.color = BLACK;
private void setRed(RBTNode&T& node) {
if (node!=null)
node.color = RED;
private void setParent(RBTNode&T& node, RBTNode&T& parent) {
if (node!=null)
node.parent =
private void setColor(RBTNode&T& node, boolean color) {
if (node!=null)
node.color =
* 前序遍历"红黑树"
private void preOrder(RBTNode&T& tree) {
if(tree != null) {
System.out.print(tree.key+" ");
preOrder(tree.left);
preOrder(tree.right);
public void preOrder() {
preOrder(mRoot);
* 中序遍历"红黑树"
private void inOrder(RBTNode&T& tree) {
if(tree != null) {
inOrder(tree.left);
System.out.print(tree.key+" ");
inOrder(tree.right);
public void inOrder() {
inOrder(mRoot);
* 后序遍历"红黑树"
private void postOrder(RBTNode&T& tree) {
if(tree != null)
postOrder(tree.left);
postOrder(tree.right);
System.out.print(tree.key+" ");
public void postOrder() {
postOrder(mRoot);
* (递归实现)查找"红黑树x"中键值为key的节点
private RBTNode&T& search(RBTNode&T& x, T key) {
if (x==null)
int cmp = key.compareTo(x.key);
if (cmp & 0)
return search(x.left, key);
else if (cmp & 0)
return search(x.right, key);
public RBTNode&T& search(T key) {
return search(mRoot, key);
* (非递归实现)查找"红黑树x"中键值为key的节点
private RBTNode&T& iterativeSearch(RBTNode&T& x, T key) {
while (x!=null) {
int cmp = key.compareTo(x.key);
if (cmp & 0)
else if (cmp & 0)
public RBTNode&T& iterativeSearch(T key) {
return iterativeSearch(mRoot, key);
* 查找最小结点:返回tree为根结点的红黑树的最小结点。
private RBTNode&T& minimum(RBTNode&T& tree) {
if (tree == null)
return null;
while(tree.left != null)
tree = tree.
public T minimum() {
RBTNode&T& p = minimum(mRoot);
if (p != null)
return null;
* 查找最大结点:返回tree为根结点的红黑树的最大结点。
private RBTNode&T& maximum(RBTNode&T& tree) {
if (tree == null)
return null;
while(tree.right != null)
tree = tree.
public T maximum() {
RBTNode&T& p = maximum(mRoot);
if (p != null)
return null;
* 找结点(x)的后继结点。即,查找"红黑树中数据值大于该结点"的"最小结点"。
public RBTNode&T& successor(RBTNode&T& x) {
// 如果x存在右孩子,则"x的后继结点"为 "以其右孩子为根的子树的最小结点"。
if (x.right != null)
return minimum(x.right);
// 如果x没有右孩子。则x有以下两种可能:
// (01) x是"一个左孩子",则"x的后继结点"为 "它的父结点"。
// (02) x是"一个右孩子",则查找"x的最低的父结点,并且该父结点要具有左孩子",找到的这个"最低的父结点"就是"x的后继结点"。
RBTNode&T& y = x.
while ((y!=null) && (x==y.right)) {
* 找结点(x)的前驱结点。即,查找"红黑树中数据值小于该结点"的"最大结点"。
public RBTNode&T& predecessor(RBTNode&T& x) {
// 如果x存在左孩子,则"x的前驱结点"为 "以其左孩子为根的子树的最大结点"。
if (x.left != null)
return maximum(x.left);
// 如果x没有左孩子。则x有以下两种可能:
// (01) x是"一个右孩子",则"x的前驱结点"为 "它的父结点"。
// (01) x是"一个左孩子",则查找"x的最低的父结点,并且该父结点要具有右孩子",找到的这个"最低的父结点"就是"x的前驱结点"。
RBTNode&T& y = x.
while ((y!=null) && (x==y.left)) {
* 对红黑树的节点(x)进行左旋转
* 左旋示意图(对节点x进行左旋):
--(左旋)-.
private void leftRotate(RBTNode&T& x) {
// 设置x的右孩子为y
RBTNode&T& y = x.
// 将 &y的左孩子& 设为 &x的右孩子&;
// 如果y的左孩子非空,将 &x& 设为 &y的左孩子的父亲&
x.right = y.
if (y.left != null)
y.left.parent =
// 将 &x的父亲& 设为 &y的父亲&
y.parent = x.
if (x.parent == null) {
this.mRoot =
// 如果 &x的父亲& 是空节点,则将y设为根节点
if (x.parent.left == x)
x.parent.left =
// 如果 x是它父节点的左孩子,则将y设为&x的父节点的左孩子&
x.parent.right =
// 如果 x是它父节点的左孩子,则将y设为&x的父节点的左孩子&
// 将 &x& 设为 &y的左孩子&
// 将 &x的父节点& 设为 &y&
x.parent =
* 对红黑树的节点(y)进行右旋转
* 右旋示意图(对节点y进行左旋):
--(右旋)-.
private void rightRotate(RBTNode&T& y) {
// 设置x是当前节点的左孩子。
RBTNode&T& x = y.
// 将 &x的右孩子& 设为 &y的左孩子&;
// 如果"x的右孩子"不为空的话,将 &y& 设为 &x的右孩子的父亲&
y.left = x.
if (x.right != null)
x.right.parent =
// 将 &y的父亲& 设为 &x的父亲&
x.parent = y.
if (y.parent == null) {
this.mRoot =
// 如果 &y的父亲& 是空节点,则将x设为根节点
if (y == y.parent.right)
y.parent.right =
// 如果 y是它父节点的右孩子,则将x设为&y的父节点的右孩子&
y.parent.left =
// (y是它父节点的左孩子) 将x设为&x的父节点的左孩子&
// 将 &y& 设为 &x的右孩子&
// 将 &y的父节点& 设为 &x&
y.parent =
* 红黑树插入修正函数
* 在向红黑树中插入节点之后(失去平衡),再调用该函数;
* 目的是将它重新塑造成一颗红黑树。
* 参数说明:
node 插入的结点
// 对应《算法导论》中的z
private void insertFixUp(RBTNode&T& node) {
RBTNode&T& parent,
// 若&父节点存在,并且父节点的颜色是红色&
while (((parent = parentOf(node))!=null) && isRed(parent)) {
gparent = parentOf(parent);
//若&父节点&是&祖父节点的左孩子&
if (parent == gparent.left) {
// Case 1条件:叔叔节点是红色
RBTNode&T& uncle = gparent.
if ((uncle!=null) && isRed(uncle)) {
setBlack(uncle);
setBlack(parent);
setRed(gparent);
// Case 2条件:叔叔是黑色,且当前节点是右孩子
if (parent.right == node) {
RBTNode&T&
leftRotate(parent);
// Case 3条件:叔叔是黑色,且当前节点是左孩子。
setBlack(parent);
setRed(gparent);
rightRotate(gparent);
//若&z的父节点&是&z的祖父节点的右孩子&
// Case 1条件:叔叔节点是红色
RBTNode&T& uncle = gparent.
if ((uncle!=null) && isRed(uncle)) {
setBlack(uncle);
setBlack(parent);
setRed(gparent);
// Case 2条件:叔叔是黑色,且当前节点是左孩子
if (parent.left == node) {
RBTNode&T&
rightRotate(parent);
// Case 3条件:叔叔是黑色,且当前节点是右孩子。
setBlack(parent);
setRed(gparent);
leftRotate(gparent);
// 将根节点设为黑色
setBlack(this.mRoot);
* 将结点插入到红黑树中
* 参数说明:
node 插入的结点
// 对应《算法导论》中的node
private void insert(RBTNode&T& node) {
RBTNode&T& y = null;
RBTNode&T& x = this.mR
// 1. 将红黑树当作一颗二叉查找树,将节点添加到二叉查找树中。
while (x != null) {
cmp = node.key.compareTo(x.key);
if (cmp & 0)
node.parent =
if (y!=null) {
cmp = node.key.compareTo(y.key);
if (cmp & 0)
this.mRoot =
// 2. 设置节点的颜色为红色
node.color = RED;
// 3. 将它重新修正为一颗二叉查找树
insertFixUp(node);
* 新建结点(key),并将其插入到红黑树中
* 参数说明:
key 插入结点的键值
public void insert(T key) {
RBTNode&T& node=new RBTNode&T&(key,BLACK,null,null,null);
// 如果新建结点失败,则返回。
if (node != null)
insert(node);
* 红黑树删除修正函数
* 在从红黑树中删除插入节点之后(红黑树失去平衡),再调用该函数;
* 目的是将它重新塑造成一颗红黑树。
* 参数说明:
node 待修正的节点
private void removeFixUp(RBTNode&T& node, RBTNode&T& parent) {
RBTNode&T&
while ((node==null || isBlack(node)) && (node != this.mRoot)) {
if (parent.left == node) {
other = parent.
if (isRed(other)) {
// Case 1: x的兄弟w是红色的
setBlack(other);
setRed(parent);
leftRotate(parent);
other = parent.
if ((other.left==null || isBlack(other.left)) &&
(other.right==null || isBlack(other.right))) {
// Case 2: x的兄弟w是黑色,且w的俩个孩子也都是黑色的
setRed(other);
parent = parentOf(node);
if (other.right==null || isBlack(other.right)) {
// Case 3: x的兄弟w是黑色的,并且w的左孩子是红色,右孩子为黑色。
setBlack(other.left);
setRed(other);
rightRotate(other);
other = parent.
// Case 4: x的兄弟w是黑色的;并且w的右孩子是红色的,左孩子任意颜色。
setColor(other, colorOf(parent));
setBlack(parent);
setBlack(other.right);
leftRotate(parent);
node = this.mR
other = parent.
if (isRed(other)) {
// Case 1: x的兄弟w是红色的
setBlack(other);
setRed(parent);
rightRotate(parent);
other = parent.
if ((other.left==null || isBlack(other.left)) &&
(other.right==null || isBlack(other.right))) {
// Case 2: x的兄弟w是黑色,且w的俩个孩子也都是黑色的
setRed(other);
parent = parentOf(node);
if (other.left==null || isBlack(other.left)) {
// Case 3: x的兄弟w是黑色的,并且w的左孩子是红色,右孩子为黑色。
setBlack(other.right);
setRed(other);
leftRotate(other);
other = parent.
// Case 4: x的兄弟w是黑色的;并且w的右孩子是红色的,左孩子任意颜色。
setColor(other, colorOf(parent));
setBlack(parent);
setBlack(other.left);
rightRotate(parent);
node = this.mR
if (node!=null)
setBlack(node);
* 删除结点(node),并返回被删除的结点
* 参数说明:
node 删除的结点
private void remove(RBTNode&T& node) {
RBTNode&T& child,
// 被删除节点的"左右孩子都不为空"的情况。
if ( (node.left!=null) && (node.right!=null) ) {
// 被删节点的后继节点。(称为"取代节点")
// 用它来取代"被删节点"的位置,然后再将"被删节点"去掉。
RBTNode&T& replace =
// 获取后继节点
replace = replace.
while (replace.left != null)
replace = replace.
// "node节点"不是根节点(只有根节点不存在父节点)
if (parentOf(node)!=null) {
if (parentOf(node).left == node)
parentOf(node).left =
parentOf(node).right =
// "node节点"是根节点,更新根节点。
this.mRoot =
// child是"取代节点"的右孩子,也是需要"调整的节点"。
// "取代节点"肯定不存在左孩子!因为它是一个后继节点。
child = replace.
parent = parentOf(replace);
// 保存"取代节点"的颜色
color = colorOf(replace);
// "被删除节点"是"它的后继节点的父节点"
if (parent == node) {
// child不为空
if (child!=null)
setParent(child, parent);
parent.left =
replace.right = node.
setParent(node.right, replace);
replace.parent = node.
replace.color = node.
replace.left = node.
node.left.parent =
if (color == BLACK)
removeFixUp(child, parent);
node = null;
if (node.left !=null) {
child = node.
child = node.
parent = node.
// 保存"取代节点"的颜色
color = node.
if (child!=null)
child.parent =
// "node节点"不是根节点
if (parent!=null) {
if (parent.left == node)
parent.left =
parent.right =
this.mRoot =
if (color == BLACK)
removeFixUp(child, parent);
node = null;
* 删除结点(z),并返回被删除的结点
* 参数说明:
tree 红黑树的根结点
z 删除的结点
public void remove(T key) {
RBTNode&T&
if ((node = search(mRoot, key)) != null)
remove(node);
* 销毁红黑树
private void destroy(RBTNode&T& tree) {
if (tree==null)
if (tree.left != null)
destroy(tree.left);
if (tree.right != null)
destroy(tree.right);
tree=null;
public void clear() {
destroy(mRoot);
mRoot = null;
* 打印"红黑树"
-- 节点的键值
* direction
0,表示该节点是根节点;
-1,表示该节点是它的父结点的左孩子;
1,表示该节点是它的父结点的右孩子。
private void print(RBTNode&T& tree, T key, int direction) {
if(tree != null) {
if(direction==0)
// tree是根节点
System.out.printf("%2d(B) is root\n", tree.key);
// tree是分支节点
System.out.printf("%2d(%s) is %2d's %6s child\n", tree.key, isRed(tree)?"R":"B", key, direction==1?"right" : "left");
print(tree.left, tree.key, -1);
print(tree.right,tree.key,
public void print() {
if (mRoot != null)
print(mRoot, mRoot.key, 0);
红黑树的测试文件(RBTreeTest.java)
* Java 语言: 二叉查找树
* @author skywang
7 public class RBTreeTest {
private static final int a[] = {10, 40, 30, 60, 90, 70, 20, 50, 80};
private static final boolean mDebugInsert = false;
// "插入"动作的检测开关(false,关闭;true,打开)
private static final boolean mDebugDelete = false;
// "删除"动作的检测开关(false,关闭;true,打开)
public static void main(String[] args) {
int i, ilen = a.
RBTree&Integer& tree=new RBTree&Integer&();
System.out.printf("== 原始数据: ");
for(i=0; i& i++)
System.out.printf("%d ", a[i]);
System.out.printf("\n");
for(i=0; i& i++) {
tree.insert(a[i]);
// 设置mDebugInsert=true,测试"添加函数"
if (mDebugInsert) {
System.out.printf("== 添加节点: %d\n", a[i]);
System.out.printf("== 树的详细信息: \n");
tree.print();
System.out.printf("\n");
System.out.printf("== 前序遍历: ");
tree.preOrder();
System.out.printf("\n== 中序遍历: ");
tree.inOrder();
System.out.printf("\n== 后序遍历: ");
tree.postOrder();
System.out.printf("\n");
System.out.printf("== 最小值: %s\n", tree.minimum());
System.out.printf("== 最大值: %s\n", tree.maximum());
System.out.printf("== 树的详细信息: \n");
tree.print();
System.out.printf("\n");
// 设置mDebugDelete=true,测试"删除函数"
if (mDebugDelete) {
for(i=0; i& i++)
tree.remove(a[i]);
System.out.printf("== 删除节点: %d\n", a[i]);
System.out.printf("== 树的详细信息: \n");
tree.print();
System.out.printf("\n");
// 销毁二叉树
tree.clear();
红黑树的Java测试程序
前面已经给出了红黑树的测试代码(RBTreeTest.java),这里就不再重复说明。下面是测试程序的运行结果:
== 原始数据: 10 40 30 60 90 70 20 50 80
== 前序遍历: 30 10 20 60 40 50 80 70 90
== 中序遍历: 10 20 30 40 50 60 70 80 90
== 后序遍历: 20 10 50 40 70 90 80 60 30
== 最小值: 10
== 最大值: 90
== 树的详细信息:
30(B) is root
10(B) is 30's
left child
20(R) is 10's
right child
60(R) is 30's
right child
40(B) is 60's
left child
50(R) is 40's
right child
80(B) is 60's
right child
70(R) is 80's
left child
90(R) is 80's
right child
阅读(...) 评论()}

我要回帖

更多关于 六年级简便计算 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信