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 ...
随机推荐
- window.open 防止浏览器拦截
https://blog.csdn.net/sinat_37255207/article/details/89374416 网上试了很多方法 最终只有一种可以 var newWin = window. ...
- koa2学习笔记02 - 给koa2添加系统日志 —— node日志管理模块log4js
前言 没有日志系统的后台应用是没有灵魂的, 平时工作中每次我们遇到接口报错的时候, 都会叫后台的童鞋看下怎么回事, 这时后台的童鞋都会不慌不忙的打开一个骚骚的黑窗口. 一串噼里啪啦的命令输进去, 哐哐 ...
- Dynamics 365 可编辑子网格的字段禁用不可编辑
在365中引入了subgrid的行可编辑,那随之带来的一个问题就是,在主表单禁用的状态下,如何禁用行编辑呢,这里就用到了subgrid的OnRecordSelect方法. 代码很简单, 我这里是禁 ...
- 【10.15总结】绕过CSRF的Referer保护
今天下午可能要出远门,所以现在就把总结写好了. Write-up地址:[Critical] Bypass CSRF protection on IBM 这个CSRF漏洞存在于IBM的修改邮箱页面,修改 ...
- Verilog中使用'include实现参数化设计
前段时间在FPGA上用Verilog写了一个多端口以太网的数据分发模块,因为每个网口需要独立的MAC地址和IP地址,为了便于后期修改,在设计中使用parameter来定义这些地址和数据总线的位宽等常量 ...
- Lua 语言学习
详细讲解见菜鸟教程 Lua. 一.数据类型 -- 直接输出 print("hello") -- 全局变量 b = print(b) -- nil(空) print(type(a)) ...
- 20155327Exp2 后门原理与实践
20155327Exp2 后门原理与实践 一.实验说明 任务一:使用netcat获取主机操作Shell,cron启动 (0.5分) 任务二:使用socat获取主机操作Shell, 任务计划启动 (0. ...
- c++ 标准流文件
一.标准流stdin,stdout,stderr 标准输入流stdin: 是程序可以读取其输入的位置.缺省情况下,进程从键盘读取 stdin . fscanf(stdin,"%d%d%f ...
- 关于自动AC机
嗯,,,,自动AC机 在cena评测时: Const SourcePath:string='incantation'; InputFile:string='incantation.in'; Outpu ...
- 4538: [Hnoi2016]网络
4538: [Hnoi2016]网络 链接 分析: 整体二分. 对于一次操作,可以二分一个答案mid,判断权值大于mid的路径是否全部经过这个点.如果是 ,那么这次询问的答案在[l,mid-1]之间, ...