重庆大学数据结构英文课件Trees_04

合集下载

Ch04 TreesPPT课件

Ch04 TreesPPT课件
( A ( B ( E ( K, L ), Fb),raCn(cGhe)s, D. ( H ( M ), I, J ) ) ) Hmmm... That’s not good.
B
E
K
F
L
A
C
G
H
M
D
I
J
❖ FirstChild-NextSibling Representation
Element FirstChild NextSibling
A BC D
length of path ::= number of edges on the path.
E F G HI J
depth of ni ::= length of the unique path K L
M
from the root to ni. Depth(root) = 0.
height of ni ::= length of the longest path from ni to a leaf. Height(leaf) = 0, and height(D) = 2.
leaf ( terminal node ) ::= a node with degree 0 (no children).
§1 Preliminaries
path from n1 to nk ::= a (unique) sequence of nodes n1, n2, …, nk such that ni is the parent of ni+1 for 1 i < k.
visit ( tree ); for (each child C of tree )
preorder ( C ); } }

平衡二叉树

平衡二叉树

Vocabulary





树 tree 子树 subtree 森林 forest 根 root 叶子 leaf 结点 node 深度 depth 层次 level 双亲 parents 孩子 children 兄弟 brother 祖先 ancestor 子孙 descentdant
性质4

具有n个结点的接近完全二叉树的深度为
证明:假设深度为k,则根据性质2和接近完全 二叉树的定义有

性质5

如果对一棵有n个结点的接近完全二叉树(其深度为log2n+1,下取 整)的结点按层序编号(从第1层到第log2n+1层,每层从左到右), 则对任一结点i(1≤i≤n),有 (1)如果i=1,则结点i是二叉树的根,无双亲;如果i>1,则其双 亲PARENT(i)是结点[ i/2]。 (2)如果2i>n,则结点i无左孩子(结点i为叶子结点);否则其左孩 子LCHILD(i)是结点2i (3)如果2i+l>n,则结点i无右孩 子;否则其右孩子只RCHILD(i) 是结点2i+1
A B C D
E
F Right subtree
Left subtree
Rotate the tree clockwise by 45
A
N
45
B E K
NCBiblioteka FNNDN
G
NN
H M
NN
I
N
J
NN
L
NN
Element Left Right
Left
Right
Several binary trees
depth of ni ::= length of the unique path K from the root to ni. Depth(root) = 0.

数据结构与算法-第五章 Trees

数据结构与算法-第五章 Trees

18
h 1 m 1 根据树的性质3可得:m 1
h m 1 <n≤ m 1
乘(m-1)后得:
mh-1<n(m-1)+1≤mh
以m为底取对数后得:h-1<logm(n(m-1)+1)≤h 即 logm(n(m-1)+1)≤h<logm(n(m-1)+1)+1 h=logm(n(m-1)+1) 结论得证。
A(B(E,F),C(G(J)),D(H,I(K,L,M)))
括号表示法
11
5.1.3
树的基本术语(Basic terminology)
结点(node)——度不为零的结点称为非终端结点,又叫分支 结点。度为零的结点称为终端结点或叶结点。
结点的度(degree)——结点拥有的子树数 叶子(leaf)——度为0的结点 孩子(child)——在一棵树中,每个结点的后继 双亲(parents)——孩子结点的上层结点叫该结点的~ 兄弟(sibling)——同一双亲的孩子
树的定义(Definition)
树的基本术语(Basic terminology) 树的性质(Property) 树的基本运算(Basic operation) 树的存储结构(Physical structure)
4
5.1.2 树的表示(Representation)
5.1.1 树的定义(Definition)
16
Property 3
h m 1 高度为h的பைடு நூலகம்次树至多有 m 1
个结点。
Prove :由树的性质 2 可知 , 第 i 层上最多结点数为 mi-1(i=1,2,…,h), 显然当高度为 h 的 m 次树 ( 即度为 m 的 树 ) 上每一层都达到最多结点数时 ,整个 m次树具有最 多结点数,因此有: 整个树的最多结点数=每一层最多结点数之和 m 1 0 1 2 h-1 =m +m +m +…+m = m 1 。

chapter4数据结构课件

chapter4数据结构课件

Arrays
- static: the size of stack is given initially Linked lists - dynamic: never become full We will explore implementations based on array and linked list, if we use good programming principles the calling routines do not need to know which method is being used.
an an-1 a2 a1
top
an an-1 a2 a1
top
Status Pop(SqStack &s,SElemType &e)
Pop and return the element at the top of the stack Don‟t forget to decrement top
S.base = (SElemType *)realloc(S.base, (S.stacksize+ STACKINCREMENT)*sizeof(SElemType)); if (!S.base) exit(OVERFLOW); S.top = S.base + S.stacksize; S.stacksize += STACKINCREMENT;
Data Structure
Software College Northeast University
4
Data Structures -- Stacks
Attributes of Stack stacksize: the max size of stack top: the Address of the top element of stack base: points to structure storing elements of stack Operations of Stack IsEmpty: return true if stack is empty, return false otherwise IsFull: return true if stack is full, return false otherwise GetTop: return the element at the top of stack Push: add an element to the top of stack Pop: delete the element at the top of stack displayStack: print all the data in the stack

重庆大学数据结构ppt06_linear+list_04

重庆大学数据结构ppt06_linear+list_04

Data Structure
Linear Lists_04
Mechanism for extracting keys

One approach: to require all record types to support some particular method that returns the key value. Problem: this approach does not work when the same record type is meant to be stored in multiple dictionaries, each keyed by a different field of the record.
Data Structure
Unit 6 Lists Application Dictionaries
College of Computer Science, CQU
Outline

The ADT for a simple dictionary Example:A payroll record implementation A dictionary search example Implementation for a class representing a key-value pair A dictionary implemented with an unsorted array-based list Dictionary implementation using a sorted array-based list
Here, payroll records are stored in two dictionaries, one organized by ID and the other organized by name. Both dictionaries are implemented with an unsorted array-based list.

2016数据结构课件-第四章_树

2016数据结构课件-第四章_树
2.叶结点、分支结点
度为0的结点被称为叶结点;度 0的结点被称为 分支结点。
在图4.1中: B有一个子结点E,度为1;A有三个 子结点B、C和D(换言之,A是B、C和D的父结 点),度为3,这棵树的度也为3 .
3.结点的层数 树形T中结点的层数递归定义如下: ⑴ root(T)层数为零; ⑵ 其余结点的层数为其前驱结点的层数加1 .
证毕。
*** 二叉树顺序存储
要存储一棵二叉树,必须存储其所有结点 的数据信息、左孩子和右孩子地址,既可 用顺序结构存储,也可用链接结构存储。
二叉树的顺序存储是指将二叉树中所有结 点按层次顺序存放在一块地址连续的存储 空间中,同时反映出二叉树中结点间的逻 辑关系。
*** 二叉树顺序存储
对于完全二叉树,结点的层次顺序反映了其结构, 可按层次顺序给出一棵完全二叉树之结点的编号,事 实上,这就是完全二叉树的顺序存储方法,结点编号 恰好反映了结点间的逻辑关系。只要对二叉树之结点 按照层次顺序进行编号,就可利用一维数组A来存储 一棵含有n个结点的完全二叉树,其中A[1]存储二叉 树的根结点,A[i]存储二叉树中编号为i的结点,并且 结点A[i]的左孩子(若存在)存放在A[2i]处,而A[i]的 右孩子(若存在)存放在A[2i1]处。
从根结点到某个结点的路径长度恰为该结点的层数。
5. 子孙结点、祖先结点 一棵树中若存在结点vm到vn的路径,则称vn为vm的子孙结 点,vm为vn的祖先结点。
不难看出,一个结点到它的某个子孙结点有且仅有一 条路径。树中结点之间的父子关系具有如下特征:
(1)树中任一结点都可以有零个或多个直接后继(即 子结点),但至多只能有一个直接前驱(即父结点)。
图4.1为一棵由根结点A出发的树,其中,A有三 个子结点B、C和D(换句话说,A是B、C和D的父结 点);B有一个子结点E;E有一个子结点F;C有两个 子结点G和H;D有一个子结点I; F,G,H,I是叶子 结点,因为它们没有子结点。

离散数学课件英文版-Trees.ppt

离散数学课件英文版-Trees.ppt
is a unique vw-path. Let e=(x,y), then e is the unique path between x and y. So, there is no xy-path in T-{e}, which means that T-{e} is no longer connected.
v0
Properties of Rooted Tree
Let (T, v0) be a rooted tree. Then (a) There are no cycles in T.
(b) v0 is the only root of T.
(c) Each vertex other than the root has in-degree one,
Using rooted tree to represent a arithmetic expressions:
branching vertices corresponding operators
leaves corresponding operands
/
+
+
Example: (a*(b+c)+d*(e*f))/(g+(h-i)*j)
13
3
2
11
Tree Searching
Tree recursive algorithm to search all vertices:
Inorder: left, root, right
h, d, i, b, e, a, f, c, g
a
Preorder: root, left, right b
Even if a vertex has only one offspring, its subtree can be identified as left or right by its location in the digraph.

《Trees》PPT课件【优秀课件】

《Trees》PPT课件【优秀课件】
We get wood from trees. We use wood to make a lot of things. We use wood to make … We also use wood to make …
Do a survey. What can these trees give us? Complete the form.
Spring comes. The bird comes back, but she cannot see the tree. Bird: Where’s Mr Tree? Grass: Some workers cut him down and
took him to a factory.
The bird and the tree
The bird flies to the factory. Bird: I’m looking for my friend Mr Tree.
Where is he? Gate: The workers cut him into wood. They
used the wood to make matches. Then they took the matches to a village.
Think and say
What can we get from trees? What can we usually make from trees?
We get wood from trees. What do we use wood for? Give a report.
Give a report.
11 Trees
What can trees do?
Trees make our city beautiful.

数据结构第4章树PPT学习教案

数据结构第4章树PPT学习教案

(c)
(d)
(二叉树 (b)仅有一个根结点的二叉树 (c)右子树为空的二叉树 (d) 左子树为空的二叉树 (e)左右子树均非空的二叉树
二叉树是n(n≥0)个结点的有限集,它或者是空集
(n=0),或者由一个根结点及两棵互不相交的、分别称
作这个根的左子树和右子树的二叉树组成。
结构的能力; 2)具有应用二叉树解决实际问题的能力。 3、素质目标 养成良好的完成工作任务、团队合作、良好沟通、创新
思维和解决问题的能力。
第1页/共47页
4.1 树的概念 树型结构是一类重要的非线性结构。树型结构是 结点之间有分支,并具有层次关系的结构。它非常类似 于自然界中的树。 树结构在客观世界中是大量存在的,在计算机领 域中也有着广泛的应用,如在编译程序中,用树来表示 源程序的语法结构;在数据库系统可用树来组织信息。 1、树的递归定义:树是n(n≥0)个结点的有限集T,T为 空时称为空树,否则它满足如下两个条件: (1)有且仅有一个特定的称为根(Root)的结点; (2)其余的结点可分为m(m≥0)个互不相交的有限子集Tl ,T2,…,Tm,其中每个子集本身又是一棵树,并称 其为根的子树(Subtree)。 注意:树的递归定义刻画了树的固有特性:一棵非空 树是由若干棵子树构成的,而子树又可由若干棵更小的
⑤点。内它树部们,结都而点是T:12B的除的根根子F结树又点。有之显两外然H棵的T互11分不是支相只结交含点的一统子个称树根为{结I}内和点部{EJ结的},
⑥开其始本结身点又:都根是结只点含又一称个为根开结始点结的点树。。
6
第3页/共47页
2)例结如点:之间的关系
A 第1层
①孩子结点:树中某个结点B 的子C 树的根D 称为第2层该结点的

《Trees》ppt课件【完美版课件】

《Trees》ppt课件【完美版课件】
We should … We shouldn’t …
The bird and the tree
The bird and the tree are friends. The bird often sings songs in the tree.
The bird and the tree
Autumn comes. The bird has to fly to the south. Bird: See you next spring, Mr Tree. Tree: See you, Miss Bird. I’ll miss you.
Kinds of trees apple tree rubber tree cocoa pine olive …
Things for us
Check the answers.
Kinds of Things for us
trees
apple tree
apples
rubber rubber
tree
Think and say
The bird and the tree
The bird sings a sad song to the fire and then flies away.
The bird and the tree
Review the words
Think and say
Why are trees important?
What can we get from the trees? Complete the spidergram below.
use drink
things we get from trees
eat wear
Give a report

【重庆大学本科四门专业课PPT】数据结构linear list_02

【重庆大学本科四门专业课PPT】数据结构linear list_02
next->element; // Remember value
Link<E>* ltemp = curr->next; // Remember link node
if (tail == curr->next) tail = curr; // Reset tail
curr->next = curr->next->next; // Remove from list
template <typename E> class LList: public List<E> {
private:
Link<E>* head; // Pointer to list header
Link<E>* tail; // Pointer to last element
Link<E>* curr; // Access to current element
Link(const E& elemval, Link* nextval =NULL)
{ element = elemval; next = nextval; }
Link(Link* nextval =NULL) { next = nextval; }
};
node
Element next
Data Structure
Insertion
Inserting a new element is a three-step process: First, the new list node is created and the new
element is stored into it. Second, the next field of the new list node is

Queue

Queue


Insert: Enqueue
Delete: Dequeue First element: Front Last element: Rear
Data Structure
04 Queue
Example: Queue of Char
insert insert insert
„c‟ „b‟ „a‟ „b‟ „a‟
head = tail = 0;
enQueue enQueue enQueue enQueue (q, (q, (q, (q, ‘a’); ‘b’); ‘c’); ‘d’); 1
2 „c‟ „b‟ 3
tail
0 head
Data Structure
„a‟ 4 5
04 Queue
Circular Queue Demo

A refinement of the lazy approach is the circular queue
head = tail = 0;
enQueue enQueue enQueue enQueue enQueue (q, (q, (q, (q, (q, ‘a’); ‘b’); ‘c’); ‘d’); ‘e’); 1
Data Structure
2 1 „c‟ „b‟ „d‟ „e‟ 4 5
04 Queuetail
3
0
„a‟
Circular Queue Demo

A refinement of the lazy approach is the circular queue
Empty: head == tail; Full: 2 1 „c‟ „b‟ „d‟ „e‟ 4 5
2 „c‟ „b‟ „d‟ 3

数据结构--树 ppt课件

数据结构--树 ppt课件

A BC D
树的度::=
madxeg (nro)ed ee
no tdreee
例如, degree of this tree = 3.
父节点(Parent) ::= 有子树的节点是 其子树根节点的父节点。
E F G HI J
KL
M
子节点(Child) ::= 若A节点是B节点的父节点,则B节点是A节点 的子节点,也叫孩子节点。
A BC D E F G HI J
KL
M
B
E
F
A
C
G
H
D
I
J
§1 预备知识
因此,每个节点的大小取决于 子树数目。噢,这样并不太好! K L M
§1 预备知识
❖ 儿子-兄弟表示法
Element FirstChild NextSibling
A BC D E F G HI JFra bibliotekKLM
A
N
B
C
D
N
E
F G HI J
第四章 树
§1 预备知识
客观世界中许多事物存在层次关系 ◦ 人类社会家谱 ◦ 社会组织结构 ◦ 图书信息管理
图书





文 学
医 药 卫
农 业 科
工 业 技
生学术



哲 学 理


界 哲

洲 哲
宗 教
论学

电 工 技 术
计 算… 机
建 筑 科 学
水 利 工 程
宗 教 分 析 研 究
宗 教… 理 论 与 概 况
} }
T ( N ) = O( N )

数据结构lecture12 trees(3)

数据结构lecture12 trees(3)

Introduction to Trees(3)Course informationTitle Data Structure (tree) Speaker Xiong QianDepartment Electronic and informationengineeringInstrument multimediaDate Mon, 14/5 Time 90 minutes Learning Objectives1 One application of binary tree--expression trees2 Comprehend the concept and the process of threaded binary treesKey Point1 threaded binary treesWay of TeachingPlan to give the lecture by clear explanation with particular examples.Make use of all kinds of instruments especially multimedia.To attract the attention of students through putting questions and other interactive method.Try to lead the students their way of thinking.Course Layout1. review Tree(1、2) 3 minutes2.new content 1.expression trees 35 minutes2. threaded binary trees50 minutes3. summary 1.review the key points and the difficulties2.questions from students3.homework2 minuteContent7-5 EXPRESSION TREESOne interesting series of binary tree applications are expression trees. An expression is a sequence of tokens that follow prescribed rules. A token may be either an operand or an operator. In this discussion, we consider only binary arithmetic operators in the form operand-operator-operand. The standard arithmetic operators are + _* /An expression tree is a binary tree with the following properties:1. Each leaf is an operand.2. The root and internal nodes are operators.3. Subtrees are subexpressions, with the root being an operator.Figure 7-15 is an infix expression and its expression tree.For an expression tree, the three standard traversals represent the three different expression formats: infix. postfix, and prefix. The inorder traversal produces the infix expression, the postorder traversal produces the postfix expression, and the preorder traversal produces the prefix expression.Infix TraversalTo demonstrate the infix traversal of an expression tree, let’s write an algorithm that traverses the tree and prints the expression. When we print the infix expression tree, we must add an opening parenthesis at the beginning of each expression and a closing parenthesis at the end of each expression. Because the root of the tree and each of its subtrees represent a subexpression, we print the opening parenthesis when we start a tree or subtree and the closing parenthesis when we have processed all of its children. Figure 7-16 shows the placement of the parentheses as we walk through the tree using an inorder traversal.Postfix TraversalThe postfix traversal of an expression uses the basic postorder traversal of any binary tree. Note that it does not require parentheses. The pseudocode is shown In Algorithm 7-7.Prefix TraversalThe final expression tree traversal is the prefix traversal. It uses the standard preorder tree traversal. Again, no parentheses are necessary. The pseudocode is shown in Algorithm 7-8.7-6 THREADED BINARY TREE中文补充。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

11
Building Human Trees

Create a collection of n initial Huffman trees, each of which is a single leaf node containing one of the letters. Put the n partial trees onto a priority queue organized by weight (frequency). Next, remove the first two trees (the ones with lowest weight) from the priority queue. Join these two trees together to create a new tree whose root has the two trees as children, and whose weight is the sum of the weights of the two trees. Put this new tree back into the priority queue. This process is repeated until all of the partial Huffman trees have been combined into one.
Data Structure
Tree & Binary Trees (4)
College of Computer Science, CQU
Outline

Introduction Weighted Path Length Huffman tree Huffman Codes
Data Structure
Data Structure 05 Tree & Binary Tree_01
17
Implementation for Huffman Tree
template <typename E> class HuffTree { private: HuffNode<E>* Root; // Tree root public: HuffTree(E& val, int freq) // Leaf constructor { Root = new LeafNode<E>(val, freq); } // Internal node constructor HuffTree(HuffTree<E>* l, HuffTree<E>* r) { Root = new IntlNode<E>(l->root(), r->root()); } ˜HuffTree() {} // Destructor HuffNode<E>* root() { return Root; } // Get root int weight() { return Root->weight(); } // Root weight };

1
2
PL = 3*1+2*3 = 9
3 6 7
4 8
5
Data Structure
05 Tree & Binary Tree_04
9
Weighted Path Length

weighted path length of a leaf is its weight times its depth. weighted path length of a tree is the sum of weighted path lengths of every leaf.
At first, there are 13 partial trees.
Data Structure
05 Tree & Binary Tree_04
13
Huffman Tree Construction
838
330
508
156
174
270
238
A A
80
O
76 81
T
93 126 144
E
125 113
05 Tree & Binary Tree_04
6


Data Structure
Introduction

The tree corresponding to our example:
0 1 0 1
e
a
t
In a tree, no leaf can be the ancestor of another leaf. Therefore, no encoding of a character can be a prefix of an encoding of another character (prefix code).
WPL

w
k 1
n
k
P Lk

Huffman tree has the minimum WPL
Data Structure
05 Tree & Binary Tree_04
10
Huffman tree

C is Huffman tree.
Data Structure
05 Tree & Binary Tree_04
Data Structure 05 Tree & Binary Tree_04
3
Introduction
Data Structure
05 Tree & Binary Tree_04
4
Introduction

To avoid such ambiguities, we can use prefix codes. In a prefix code, the bit string for a character never occurs as the prefix (first part) of the bit string for another character. For example, the encoding of e with 0, a with 10, and t with 11 is a prefix code. How can we now encode the word tea? The encoding is 11010. This bit string is unique, it can only encode the word tea.
05 Tree & Binary Tree_04
1
Introduction

We usually encode strings by assigning fixedlength codes to all characters in the alphabet (for example, 8-bit coding in ASCII). However, if different characters occur with different frequencies, we can save memory and reduce transmittal time by using variablelength encoding. The idea is to assign shorter codes to characters that occur more often.
05 Tree & Binary Tree_04
5


Data Structure
Introduction

We can represent prefix codes using binary tree, where the characters are the labels of the leaves in the tree. The edges of the tree are labeled so that an edge leading to a left child is assigned a 0 and an edge leading to a right child is assigned a 1. The bit string used to encode a character is the sequence of labels of the edges in the unique path from the root to the leaf labeled with this character.
D
40
L
41
R
61
S
65
N
71
I
72 58
H
55
C
31
U
27
14
Data Structure
05 Tree & Binary Tree_01
Implementation for Huffman Tree Nodes
// Huffman tree node abstract base class template <typename E> class HuffNode { public: virtual ˜HuffNode() {} // Base destructor virtual int weight() = 0; // Return frequency virtual bool isLeaf() = 0; // Determine type }; template <typename E> // Leaf node subclass class LeafNode : public HuffNode<E> { private: E it; // Value int wgt; // Weight public: LeafNode(const E& val, int freq) // Constructor { it = val; wgt = freq; } int weight() { return wgt; } E val() { return it; } bool isLeaf() { return true; } };
相关文档
最新文档