Experiment report of Besti

course:《Program Design & Data Structures》

Class: 1623

Student Name: Wang, Yixiao

Student Number:20162314

Tutor:Mr.Lou、Mr.Wang

Experiment date:2017.10.27

Secret level: Unsecretive

Experiment time:60 minutes

Major/Elective:Major

Experiment order:6

Experiment content

  1. LinkedBinaryTreeTest

  1. BinaryTree

  1. DecisionTree

  1. Expression Tree

  1. BinarySearchTree

6.Analyze Source Code (Red-Black Tree)

Experiment situation

Exp1 LinkedBinaryTreeTest

  • It's easy to finish this experiment.
  • To start with , import to form a new LinkedBinaryTree to start the experiment.
  • Next , use the method GetRight GetLeft to ealuation.
  • Then , new element "ABC".
  • Last , Assert.assertequals();

Exp2 BinaryTree

  • To start with , use the number to replace "A B C D ....."
  • Next , new newNode
  • Then, creat BinTree to get left child and right child
  • Use three methods preOrderTraverse,inOrderTraverse,postOrderTraverse.

Exp3 Decision Tree

  • To start with , creat a class TwentyQuestionsPlayer
  • String the Questions and the answers as the order
  • New them
  • Write a method play(),Yes =Y , No=N .
  • Next , write a function to finish this experiment.

Exp4 ExpressionTree

Exp5 BinarySearchTreeTest

  • To start with , creat a class BinarySearchTree
  • public method Node find, insert,preorder,inorder,postorder,getMinNode get MaxNode,Delete.
  • creat three new Nodes(id and name);
  • Node n1 n2 n3 n4 ...n7 n8
  • Use method insert to n4 n5 n6 n7 n8
  • Bst.inorder and Systemin.

Exp6 Red-Black Tree (Analyze Source Code)

  • TreeMap底层通过红黑树(Red-Black tree)实现,也就意味着containsKey(), get(), put(), remove()都有着log(n)的时间复杂度。

  • SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));

  • 结构调整过程包含两个基本操作:左旋(Rotate Left),右旋(RotateRight)。

  • Rotate Left

  //Rotate Left
private void rotateLeft(Entry<K,V> p) {
if (p != null) {
Entry<K,V> r = p.right;
p.right = r.left;
if (r.left != null)
r.left.parent = p;
r.parent = p.parent;
if (p.parent == null)
root = r;
else if (p.parent.left == p)
p.parent.left = r;
else
p.parent.right = r;
r.left = p;
p.parent = r;
}
}

  • Rotate Right
  //Rotate Right
private void rotateRight(Entry<K,V> p) {
if (p != null) {
Entry<K,V> l = p.left;
p.left = l.right;
if (l.right != null) l.right.parent = p;
l.parent = p.parent;
if (p.parent == null)
root = l;
else if (p.parent.right == p)
p.parent.right = l;
else p.parent.left = l;
l.right = p;
p.parent = l;
}
}
  • .get()

  //getEntry()方法
final Entry<K,V> getEntry(Object key) {
......
if (key == null)//不允许key值为null
throw new NullPointerException();
Comparable<? super K> k = (Comparable<? super K>) key;//使用元素的自然顺序
Entry<K,V> p = root;
while (p != null) {
int cmp = k.compareTo(p.key);
if (cmp < 0)//向左找
p = p.left;
else if (cmp > 0)//向右找
p = p.right;
else
return p;
}
return null;
}
  • .put()
  public V put(K key, V value) {
......
int cmp;
Entry<K,V> parent;
if (key == null)
throw new NullPointerException();
Comparable<? super K> k = (Comparable<? super K>) key;//使用元素的自然顺序
do {
parent = t;
cmp = k.compareTo(t.key);
if (cmp < 0) t = t.left;//向左找
else if (cmp > 0) t = t.right;//向右找
else return t.setValue(value);
} while (t != null);
Entry<K,V> e = new Entry<>(key, value, parent);//创建并插入新的entry
if (cmp < 0) parent.left = e;
else parent.right = e;
fixAfterInsertion(e);//调整
size++;
return null;
}

  //红黑树调整函数fixAfterInsertion()
private void fixAfterInsertion(Entry<K,V> x) {
x.color = RED;
while (x != null && x != root && x.parent.color == RED) {
if (parentOf(x) == leftOf(parentOf(parentOf(x)))) {
Entry<K,V> y = rightOf(parentOf(parentOf(x)));
if (colorOf(y) == RED) {//如果y为null,则视为BLACK
setColor(parentOf(x), BLACK); // 情况1
setColor(y, BLACK); // 情况1
setColor(parentOf(parentOf(x)), RED); // 情况1
x = parentOf(parentOf(x)); // 情况1
} else {
if (x == rightOf(parentOf(x))) {
x = parentOf(x); // 情况2
rotateLeft(x); // 情况2
}
setColor(parentOf(x), BLACK); // 情况3
setColor(parentOf(parentOf(x)), RED); // 情况3
rotateRight(parentOf(parentOf(x))); // 情况3
}
} else {
Entry<K,V> y = leftOf(parentOf(parentOf(x)));
if (colorOf(y) == RED) {
setColor(parentOf(x), BLACK); // 情况4
setColor(y, BLACK); // 情况4
setColor(parentOf(parentOf(x)), RED); // 情况4
x = parentOf(parentOf(x)); // 情况4
} else {
if (x == leftOf(parentOf(x))) {
x = parentOf(x); // 情况5
rotateRight(x); // 情况5
}
setColor(parentOf(x), BLACK); // 情况6
setColor(parentOf(parentOf(x)), RED); // 情况6
rotateLeft(parentOf(parentOf(x))); // 情况6
}
}
}
root.color = BLACK;
}

Code hosting

PSP5.1(Personal Software Process)

Steps Time percent
requirement 45minutes 16.7%
design 50minutes 18.5%
coding 1.5hours 32.2%
test 30minutes 11.1%
summary 55minutes 19.2%

20162314 Experiment 2 - Tree的更多相关文章

  1. 20162314 Experiment 1: Linear structure - experiment report.

    Experiment report of Besti course:<Program Design & Data Structures> Class: 1623 Student N ...

  2. 20162314 Experiment 4 - Graph

    Experiment report of Besti course:<Program Design & Data Structures> Class: 1623 Student N ...

  3. 20162314 Experiment 3 - Sorting and Searching

    Experiment report of Besti course:<Program Design & Data Structures> Class: 1623 Student N ...

  4. 20162314 《Program Design & Data Structures》Learning Summary Of The Ninth Week

    20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Ninth Week ...

  5. 20162314 《Program Design & Data Structures》Learning Summary Of The Seventh Week

    20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Seventh Wee ...

  6. 20162314 《Program Design & Data Structures》Learning Summary Of The Eleventh Week

    20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Eleventh We ...

  7. 20162314 《Program Design & Data Structures》Learning Summary Of The Tenth Week

    20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Tenth Week ...

  8. 20162314 《Program Design & Data Structures》Learning Summary Of The Eighth Week

    20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Eighth Week ...

  9. the longest distance of a binary tree

    版权声明:欢迎查看本博客.希望对你有有所帮助 https://blog.csdn.net/cqs_2012/article/details/24880735 the longest distance ...

随机推荐

  1. 四步掌握CAN节点隔离设计

    四步掌握CAN节点隔离设计 “隔离”是模块为CAN节点设备提供可靠数据传输的首要保障,通常隔离模块的“隔离”是指模块上电后,能为节点提供信号隔离及电源隔离,隔离电压等级以2500VDC.3500VDC ...

  2. window.open 防止浏览器拦截

    https://blog.csdn.net/sinat_37255207/article/details/89374416 网上试了很多方法 最终只有一种可以 var newWin = window. ...

  3. SQL用法总全

    https://www.yiibai.com/sql 比较全面 事务就是对数据库执行的工作单元.事务是完成逻辑顺序的工作,无论是在手动方式由用户或者自动地通过某种数据库程序的序列的单元. 事务是一个或 ...

  4. 数据结构与算法之二叉树 ——in dart

    用dart语言实现的二叉树,实现了插入.查找.删除,中序遍历.前序.后序遍历等功能. class BinaryTree<E extends Comparable> { Node<E& ...

  5. jQuery学习- 内容选择器

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. CF908G New Year and Original Order

    题面 题意翻译 给定$n<=10^{700}$,问$1$到$n$中每个数在各数位排序后得到的数的和.答案$mod\;10^9+7$. 题解 考虑设$f[i][j][k][0/1]$表示前$i$位 ...

  7. Redis学习之路(一)之缓存知识体系

    转自:https://www.unixhot.com/page/cache 缓存分层 缓存分级 内容 内容简介/主要技术关键词 用户层 DNS 浏览器DNS缓存 Firefox默认60秒,HTML5的 ...

  8. SSISDB1:使用SSISDB管理Package

    SSISDB 系列随笔汇总: SSISDB1:使用SSISDB管理Package SSISDB2:SSIS工程的操作实例 SSISDB3:Package的执行实例 SSISDB4:当前正在运行的Pac ...

  9. 用Angule Cli创建Angular项目

    Angular4.0来了,更小,更快,改动少 接下来为Angular4.0准备环境和学会使用Angular cli项目 1.环境准备: 1)在开始工作之前我们必须设置好开发环境 如果你的机器上还没有安 ...

  10. JMeter做http接口压力测试

    测前准备 用JMeter做接口的压测非常方便,在压测之前我们需要考虑这几个方面: 场景设定 场景分单场景和混合场景.针对一个接口做压力测试就是单场景,针对一个流程做压力测试的时候就是混合场景,需要多个 ...