Department of Computing and Information Systems
COMP10002 Foundations of Algorithms
Semester 2, 2014
Assignment 2
Learning Outcomes
In this project you will demonstrate your understanding of dynamic memory and linked data structures.
You will also extend your skills in terms of program design, testing, and debugging.
Trees, Trees, Trees
The Binary Search Tree (BST) is a linked data structure that can be used to implement a dictionary data
structure. In this project you will write an “interpreter” that manipulates items in a BST, supporting a
small number of operations such as inserting and printing.
To get started, copy and study the program ass2-skel.c linked from the assignment FAQ page at
http://people.eng.unimelb.edu.au/ammoffat/teaching/10002/ass2/. The FAQ page is also
linked from the LMS. The skeleton program compiles cleanly, and provides the framework necessary for
a simple “language” for manipulating binary search trees.
Commands in this language are all one letter long, and have either zero or one string arguments
each. A stub function is provided for each type of command that is to be implemented; and the main
program and auxiliary functions are all complete and should not require any modification. Each stage of
the project requires that you implement one or more of the following commands:
“i”, insert an item;
“p”, generate a simple printout of the tree that has been built;
“t”, tabulate some statistics about the tree that has been formed;
“r”, rotate an item one level closer to the root;
“s”, generate a snazzy printout of the tree that has been built; and
“f”, free all of the space currently allocated to the tree, and restart from an empty tree.
The program can either take its input from stdin or from a file named on the command line. In the latter
case, the commands are echoed as they are read, to provide understandable output. The interpreter loop
ends, and the program exits, when end of file is encountered.
Stage 1 – Inserting and Printing (9/15 marks)
Complete the functions do insert() and do print s().
Insertion should follow the usual binary tree ordering, and youmay simply call strcmp() to establish
ordering. If a string is already present in the tree, and another insert command for that string is issued,
the tree should be left unchanged. That is, the tree should not contain duplicate strings. Note that there is
no use of polymorphism in this project, and you do not need to make use of function pointers or void*
pointers. That is, your code will be more like listops.c than treeops.c; and should all be in one file.
The function do print s() draws a simple picture of the tree as it stands at that point in time,
without changing the tree at all. The root of the tree should be against the left margin. The strings should
be reverse alphabetic down the page, with each node of the tree indented five more characters than its
1parent. Here is one possible sequence of inputs and outputs (turn the page 90 degrees clockwise to see
the tree):
> i mary
> i had
> i a
> i little
> i lamb
> p
mary
little
lamb
had
a
>
Stage 2 – Tree Size (11/15 marks)
Now add in the body of the do tabulate() function. It prints a small table reporting the number of
items in the tree, and the average and maximum depth in the tree of those items. The root of the tree is
at depth 1. Continuing on from the previous example, a “t” command would report:
> t
size : 5
avg depth: 2.60
max depth: 4
>
Stage 3 – Edge Rotations (13/15 marks)
Ok, now for something different. The diagram on the next page shows what is meant by an edge rotation
in a BST: starting with the tree on the left, a rotation at the node whose key is the string “d” results in the
tree on the right; conversely, starting with the tree on the right, an edge rotation at the node whose key
is “b” results in the tree on the left. Note how one of the subtrees is reassigned in a manner that ensures
that the post-rotation tree is still a binary search tree. A rotation alters the structure of a tree, but not
its contents. For example, if subtree E is larger than subtree A, the tree on the right will have a smaller
average node depth, and thus better average searching and insertion characteristics.
Write the body of the function do rotate() that identifies the location of its argument in the tree,
and then rotates the tree at that position. If the argument string is at the root of the tree, or if the
argument string does not appear anywhere in the tree, then the original tree should be returned unchanged.
Warning: there are a lot of pointers to be re-assigned, and multiple cases to be considered. Draw pictures
on paper first, before you try and write the code. Continuing the same example, we would have:
2b
b
b d
A
C A E C
E
rotate at
rotate at
d
d
Edge rotation in a Binary Search Tree. The lower node is “rotated” in to the position
previously occupied by its parent, then subtrees are rearranged and all pointers
updated so that the tree is still in alphabetic order.
> r had
> p
mary
little
lamb
had
a
> t
size : 5
avg depth: 2.40
max depth: 4
>
Stage 4 – Resetting the Tree (14/15 marks)
The “f” command frees up all space that has been allocated to the tree and its strings by making calls to
free(), and resets the tree back to an empty initial state:
> f
> t
size : 0
>
The tree should be freed up from the leaves back toward the root, so that you don’t end up with a memory
leak caused by unreachable data.
Stage 5 – Snazzy Printing (15/15 marks)
For one last mark, add the details of the second “snazzy” print command. For the same tree as before,
(after rotating “had”), the output would be
3> s
+--
+--mary
| | +--
| +--little
| | +--
| +--lamb
| +--
had
| +--
+--a
+--
>
There are more examples linked from the FAQ page.
Purely for Fun (and not for submission)
Implement a “d” command that deletes the indicated string from the tree. If the string is a leaf, deletion is
easy. If it is an internal node, locate the alphabetically next node and swap it in to the position occupied
by the string being deleted.
The boring stuff...
This project is worth 15% of your final mark. A rubric explaining the expectations that you will be
marked will be provided shortly via the LMS.
You need to submit your program for assessment; detailed instructions on how to do that will be
posted on the LMS once submissions are opened. Submission will not be done via the LMS; instead you
will need to log in to a Unix server and submit your files to a software system known as submit. You
can (and should) use submit both early and often – to get used to the way it works, and also to check
that your program compiles correctly on our test system, which has some different characteristics to the
lab machines. Only the last submission that you make before the deadline will be marked.
You may discuss your work during your workshop, and with others in the class, but what gets typed
into your program must be individual work, not copied from anyone else. So, do not give hard copy
or soft copy of your work to anyone; do not “lend” your “Uni backup” memory stick to others for any
reason at all; and do not ask others to give you their programs “just so that I can take a look and get
some ideas, I won’t copy, honest”. The best way to help your friends in this regard is to say a very
firm “no” when they ask for a copy of, or to see, your program, pointing out that your “no”, and their
acceptance of that decision, is the only thing that will preserve your friendship. A sophisticated program
that undertakes deep structural analysis of C code identifying regions of similarity will be run over all
submissions in “compare every pair” mode. Students whose programs are so identified will be referred
to the Student Center. See https://academichonesty.unimelb.edu.au for more information.
Deadline: Programs not submitted by 10:00am on Monday 20 October will lose penalty marks at
the rate of two marks per day or part day late. Students seeking extensions for medical or other “outside
my control” reasons should email Alistair, ammoffat@unimelb.edu.au as soon as possible after those
circumstances arise.
Marks and a sample solution will be available on the LMS by Monday 3 November.
And remember, algorithms are fun!
4

c语言二叉树的更多相关文章

  1. Hello world!(内含自己编写的C语言二叉树同学录)

      修改:刷了一段时间的题,水平渐涨,发现同学录真的要做成市面可行的应用的话,应该按学号建立二叉平衡树,红黑树是一个可行的选择. 在同学的推荐下,来到博客园来找志同道合的人交流代码.3个月后参加蓝桥杯 ...

  2. c语言二叉树基本操作

    编译器为vs2013 #include "stdafx.h" #include<malloc.h> #include<stdlib.h> #define O ...

  3. C语言二叉树的建立与遍历

    二叉树的建立和遍历都要用到递归,先暂时保存一下代码,其中主要是理解递归的思想,其它的就都好理解了.这里是三种遍历方式,其实理解一种,其它的几个就都理解了,就是打印出来的顺序不一样而已.建立和遍历的方式 ...

  4. [数据结构]C语言二叉树的实现

    树和图是数据结构中比较麻烦的东西,里面涉及的概念比较多,也最有用, 就比如一般树广泛应用于人工智能的博弈上,而基于图的广度优先和深度优先搜索也广泛应用于人工智能寻路上面 首先我们要把树进行分类: &g ...

  5. Go语言二叉树定义及遍历算法实现

    // binary_tree 二叉树 package Algorithm import ( "reflect" ) // 二叉树定义 type BinaryTree struct ...

  6. C语言二叉树的创建、(先中后序)遍历以及存在的问题

    #include<stdlib.h> #include<stdio.h> #define True 1 #define False 0 typedef char TElemTy ...

  7. go语言 二叉树

    package main import ( "fmt" "reflect" ) type BinaryNode struct { Data interface{ ...

  8. C语言二叉树遍历及路径查找

    #include<iostream> #include<stdio.h> #include<math.h> #include<malloc.h> usi ...

  9. c语言二叉树的递归建立

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <malloc.h&g ...

随机推荐

  1. (转)MVC中的Repository模式

    1.首先创建一个空的MVC3应用程序,命名为MyRepository.Web,解决方案命名为MyRepository. 2.添加一个类库项目,命名为MyRepository.DAL,添加一个文件夹命名 ...

  2. Linux内核分析之跟踪分析Linux内核的启动过程

    一.实验过程 使用实验楼虚拟机打开shell cd LinuxKernel/ qemu -kernel linux-/arch/x86/boot/bzImage -initrd rootfs.img ...

  3. BZOJ3175 Tjoi2013 攻击装置(二分图匹配)

    传送门 Description 给定一个01矩阵,其中你可以在0的位置放置攻击装置.每一个攻击装置(x,y)都可以按照"日"字攻击其周围的 8个位置(x-1,y-2),(x-2,y ...

  4. SQL中的取整函数FLOOR、ROUND、CEIL、TRUNC、SIGN

    1 trunc(value,precision)按精度(precision)截取某个数字,不进行舍入操作.2 round(value,precision)根据给定的精度(precision)输入数值. ...

  5. 不均匀的Windows处理器编组

    不均匀的Windows处理器编组 之前写过一篇文章,关于SQLSERVER能识别多少个逻辑CPU的,前些天在论坛里有人问Windows处理器编组是如何划分的?? SQLSERVER到底能识别多少个逻辑 ...

  6. 基于 IdentityServer3 实现 OAuth 2.0 授权服务数据持久化

    最近花了一点时间,阅读了IdentityServer的源码,大致了解项目整体的抽象思维.面向对象的重要性; 生产环境如果要使用 IdentityServer3 ,主要涉及授权服务,资源服务的部署负载的 ...

  7. 每周一道数据结构(四)A*算法&博弈树α-β剪枝

    A*算法/博弈树 前阵子考试学了A*算法.博弈树和回溯,自己真是愚蠢至极,根本没就搞明白这些,所以对于这些算法问道的话就不能说清楚,也记不住,所以才有了这篇笔记.在这里感谢面试我的那位工程师~~ A* ...

  8. ios 手动添加mapview

    1,首先选中Build Phases ,在Link Binary With Libraries 下添加MapKit.framework框架 2,在头文件(.h文件)处添加:#import <Ma ...

  9. hadoop面试时的一些问题解答

    一.         linux部分 请阐述swap分区作用,您认为hadoop集群中的linux是否必须有swap分区? 答:在Linux中,如果一个进程的内存空间不足,那么,它会将内存中的部分数据 ...

  10. iOS开发之静态库(二)—— .a

    前面已经介绍了iOS中静态库的基本概念和特点,这里就不再多废话,直接上代码 编译环境Xcode5.1 + Mac OS X 10.9.3 新建静态库项目 Xcode中创建静态库的模板有两个,一个是创建 ...