重庆大学数据结构英文课件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.
  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; } };
相关文档
最新文档