20162314 Experiment 2 - Tree
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
- LinkedBinaryTreeTest
- BinaryTree
- DecisionTree
- Expression Tree
- 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的更多相关文章
- 20162314 Experiment 1: Linear structure - experiment report.
Experiment report of Besti course:<Program Design & Data Structures> Class: 1623 Student N ...
- 20162314 Experiment 4 - Graph
Experiment report of Besti course:<Program Design & Data Structures> Class: 1623 Student N ...
- 20162314 Experiment 3 - Sorting and Searching
Experiment report of Besti course:<Program Design & Data Structures> Class: 1623 Student N ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- the longest distance of a binary tree
版权声明:欢迎查看本博客.希望对你有有所帮助 https://blog.csdn.net/cqs_2012/article/details/24880735 the longest distance ...
随机推荐
- UCOSii和Linux的区别和联系
UCOSii和Linux的区别和联系 想通过UCOSii来理解Linux的系统架构,故参考一些资料,简单整理了一下UCOSii和Linux的区别和联系,以此来更好的学习Linux. 其具体对比如下: ...
- linux-2.6内核驱动学习——jz2440之输入子系统
如果按照上一篇记录的那样,只有本公司的人或者自己才能使用驱动.想写出一个通用的驱动程序,让其他应用程序来无缝移植,需要使用现成的驱动——输入子系统. /drivers/input/input.c #d ...
- win10安装OpenSSL及简单的使用
学习IdentityServer过程中需要使用OpenSSL,OpenSSL是什么东西?百度百科的解释:在计算机网络上,OpenSSL是一个开放源代码的软件库包,应用程序可以使用这个包来进行安全通信, ...
- sqlserver 索引优化 CPU占用过高 执行分析 服务器检查
原文:sqlserver 索引优化 CPU占用过高 执行分析 服务器检查 1. 管理公司一台服务器,上面放的东西挺多的.有一天有个哥们告诉我现在程序卡的厉害.我给他说,是时候读点优化的书了.别一天到晚 ...
- PPAS通过DBLink,访问Oracle数据库
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL基础知识与基本操作索引页 回到顶级页面:PostgreSQL索引页 作者 高健@博客园 luckyjackga ...
- 【转载】MFC的Main函数跑哪去了
原文:http://blog.csdn.net/weiwenhp/article/details/8455471 习惯的思维 用习惯了C的人要看一个程序时首先会想到找到那个main函数在哪,然后再顺着 ...
- Git 解决添加到.gitignore的忽略项不生效的问题
今天又在.gitignore添加了一些忽略项,但是后来发现一些东西命名配置了忽略项却还是没起作用,so,分析原因,可能是在我添加忽略项之前,因为这些文件就早已经被提交了,所有他们已经在版本控制中,导致 ...
- mysql my.init
CLIENT SECTION 端口默认是3306,可以修改为别的端口 编码格式默认是latin,要修改成utf8 注意是utf8,不是utf-8 SERVER SECTION 端口也是默认3306 编 ...
- Excel表格生成sql语句
假如excel表格中有A.B.C三列数据,希望导入到数据库users表中,对应的字段分别是name,sex,age ,在你的excel表格中增加一列,利用excel的公式自动生成sql语句,方法如下: ...
- Map.containsKey(String key)
判断key有没有对应的value值有,返回true无,返回false