Tải bản đầy đủ (.pdf) (63 trang)

Chap5 treeconcept 123

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.25 MB, 63 trang )

Tree concepts
Dept. Computer
Science

Tree concepts and Binary Tree

Basic Tree Concepts
Binary Trees
Expression Trees

Data Structures and Algorithms

Binary Search Trees

Dept. Computer Science
Faculty of Computer Science and Engineering
Ho Chi Minh University of Technology, VNU-HCM

Tree concepts.1


Overview

Tree concepts
Dept. Computer
Science

1 Basic Tree Concepts
Basic Tree Concepts
Binary Trees


2 Binary Trees

Expression Trees
Binary Search Trees

3 Expression Trees

4 Binary Search Trees

Tree concepts.2


Tree concepts
Dept. Computer
Science

Basic Tree Concepts
Binary Trees

Basic Tree Concepts

Expression Trees
Binary Search Trees

Tree concepts.3


Tree concepts

Basic Tree Concepts


Dept. Computer
Science

Definition

A tree (cây) consists of a finite set of elements, called nodes
(nút), and a finite set of directed lines, called branches
(nhánh), that connect the nodes.

Basic Tree Concepts
Binary Trees
Expression Trees

a

c

b

e

Binary Search Trees

f

d

g


h

i
Tree concepts.4


Tree concepts

Basic Tree Concepts
• Degree of a node (Bậc của nút): the number of
branches associated with the node.
• Indegree branch (Nhánh vào): directed branch toward
the node.
• Outdegree branch (Nhánh ra): directed branch away
from the node.

Dept. Computer
Science

Basic Tree Concepts
Binary Trees
Expression Trees
Binary Search Trees

For the node d:
• Degree = 4

a

c


b

e

f

• Indegree branches: ad
→ indegree = 1
• Outdegree branches:
dg, dh, di
→ outdegree = 3

d

g

h

i
Tree concepts.5


Tree concepts

Basic Tree Concepts

Dept. Computer
Science


• The first node is called the root.
• indegree of the root = 0
• Except the root, the indegree of a node = 1
• outdegree of a node = 0 or 1 or more.
a

e

f

Binary Trees
Expression Trees

root

c

b

Basic Tree Concepts

Binary Search Trees

d

g

h

i

Tree concepts.6


Basic Tree Concepts
Terms

• A root (nút gốc) is the first node with an indegree of
zero.
• A leaf (nút lá) is any node with an outdegree of zero.
• A internal node (nút nội) is not a root or a leaf.
• A parent (nút cha) has an outdegree greater than
zero.
• A child (nút con) has an indegree of one.
→ a internal node is both a parent of a node and a
child of another one.
• Siblings (nút anh em) are two or more nodes with the
same parent.
• For a given node, an ancestor is any node in the path
from the root to the node.
• For a given node, an descendent is any node in the
paths from the node to a leaf.

Tree concepts
Dept. Computer
Science

Basic Tree Concepts
Binary Trees
Expression Trees
Binary Search Trees


Tree concepts.7


Basic Tree Concepts

Tree concepts
Dept. Computer
Science

Terms

• A path (đường đi) is a sequence of nodes in which
each node is adjacent to the next one.

Basic Tree Concepts
Binary Trees
Expression Trees

• The level (bậc) of a node is its distance from the root.
→ Siblings are always at the same level.

Binary Search Trees

• The height (độ cao) of a tree is the level of the leaf in
the longest path from the root plus 1.
• A subtree (cây con) is any connected structure below
the root.

Tree concepts.8



Tree concepts

Basic Tree Concepts

Dept. Computer
Science

a

Level 0

Branch ad
Level 1

c

b

Basic Tree Concepts

d

Binary Trees
Expression Trees

Branch di
Level 2


e

f

• Parents: a, b, d
• Children:
b, c, d, e, f, g, h, i
• Leaves: c, e, f, g, h, i

g

h

Binary Search Trees

i

• Internal nodes: b, d
• Siblings:
{b, c, d}, {e, f }, {g, h, i}
• Height = 3
Tree concepts.9


Tree concepts

Basic Tree Concepts

Dept. Computer
Science


a
Subtree b

Subtree d

Basic Tree Concepts
Binary Trees
Expression Trees

c

b

e

f

Binary Search Trees

d

g

h

i

Tree concepts.10



Tree concepts

Tree representation

Dept. Computer
Science

a

• organization chart
a

b

c

b

Basic Tree Concepts

e

d

f
e

f


g

h

i

Binary Trees
Expression Trees
Binary Search Trees

c
d

• parenthetical listing

a (b (e f ) c d (g h i))
• indented list

g
h
i

Tree concepts.11


Applications of Trees

Tree concepts
Dept. Computer
Science


• Representing hierarchical data

Basic Tree Concepts
Binary Trees

• Storing data in a way that makes it easily
searchable (ex: binary search tree)

Expression Trees
Binary Search Trees

• Representing sorted lists of data
• Network routing algorithms

Tree concepts.12


Tree concepts
Dept. Computer
Science

Basic Tree Concepts
Binary Trees

Binary Trees

Expression Trees
Binary Search Trees


Tree concepts.13


Tree concepts

Binary Trees

Dept. Computer
Science

A binary tree node cannot have more than two subtrees.
Basic Tree Concepts

a
Left subtree

Binary Trees

Right subtree

Expression Trees
Binary Search Trees

b

e

d

f


g

Tree concepts.14


Binary Trees Properties
• To store N nodes in a binary tree:

Tree concepts
Dept. Computer
Science

• The minimum height: Hmin = log2 N + 1 or
Hmin = log2 (N + 1)
• The maximum height: Hmax = N
Basic Tree Concepts

• Given a height of the binary tree, H:
• The minimum number of nodes: Nmin = H
• The maximum number of nodes: Nmax = 2H − 1

Binary Trees
Expression Trees
Binary Search Trees

Balance

The balance factor of a binary tree is the difference in height
between its left and right subtrees.


B = HL − HR
Balanced tree:
• balance factor is 0, -1, or 1
• subtrees are balanced

Tree concepts.15


Tree concepts

Binary Trees Properties

Dept. Computer
Science

Complete tree

N = Nmax = 2H − 1

a
Basic Tree Concepts

The last level is full.

c

b

Binary Trees

Expression Trees

e

d

g

f

Binary Search Trees

Nearly complete tree

H = Hmin = log2 N + 1

a

Nodes in the last level are
on the left.

c

b
d

e
Tree concepts.16



Binary Tree Structure
Definition

A binary tree is either empty, or it consists of a node called
root together with two binary trees called the left and the
right subtree of the root.

Tree concepts
Dept. Computer
Science

Basic Tree Concepts
Binary Trees
Expression Trees
Binary Search Trees

Tree concepts.17


Binary Tree Structure: Linked implementation

Tree concepts
Dept. Computer
Science

node
d a t a <dataType>
l e f t


r i g h t


end node



binaryTree
r o o t


end b i n a r y T r e e

Basic Tree Concepts
Binary Trees
Expression Trees
Binary Search Trees

// G e n e r a l d a t a T y e :
dataType
k e y <keyType>
f i e l d 1 <... >
f i e l d 2 <... >
...
f i e l d n <... >
end d a t a T y p e

Tree concepts.18


Binary Tree Structure: Array-based implementation
Suitable for complete tree, nearly complete tree.

Tree concepts
Dept. Computer
Science

Basic Tree Concepts


Binary Trees
Expression Trees
Binary Search Trees

Hình: Conceptual

binaryTree
d a t a <a r r a y o f dataType>
end b i n a r y T r e e

Hình: Physical
Tree concepts.19


Binary Tree Traversals

Tree concepts
Dept. Computer
Science

• Depth-first traversal (duyệt theo chiều sâu): the
processing proceeds along a path from the root
through one child to the most distant descendent
of that first child before processing a second child,
i.e. processes all of the descendents of a child before
going on to the next child.

Basic Tree Concepts
Binary Trees
Expression Trees

Binary Search Trees

• Breadth-first traversal (duyệt theo chiều rộng): the
processing proceeds horizontally from the root to
all of its children, then to its children’s children, i.e.
each level is completely processed before the next
level is started.

Tree concepts.20


Depth-first traversal

Tree concepts
Dept. Computer
Science

• Preorder traversal
• Inorder traversal
• Postorder traversal

Basic Tree Concepts
Binary Trees
Expression Trees
Binary Search Trees

Tree concepts.21


Preorder traversal (NLR)


Tree concepts
Dept. Computer
Science

In the preorder traversal, the root is processed first, before
the left and right subtrees.
Basic Tree Concepts
Binary Trees
Expression Trees
Binary Search Trees

Tree concepts.22


Preorder traversal (NLR)

Tree concepts
Dept. Computer
Science

1
2
3
4
5
6
7
8
9


10

11

Algorithm preOrder(val root )
Traverse a binary tree in node-left-right sequence.
Pre: root is the entry node of a tree or subtree
Post: each node has been processed in order
if root is not null then
process(root)
preOrder(root->left)
preOrder(root->right)
end
Return
End preOrder

Basic Tree Concepts
Binary Trees
Expression Trees
Binary Search Trees

Tree concepts.23


Inorder traversal (LNR)

Tree concepts
Dept. Computer
Science


In the inorder traversal, the root is processed between its
subtrees.
Basic Tree Concepts
Binary Trees
Expression Trees
Binary Search Trees

Tree concepts.24


Inorder traversal (LNR)

Tree concepts
Dept. Computer
Science

1
2
3
4
5
6
7
8
9

10

11


Algorithm inOrder(val root )
Traverse a binary tree in left-node-right sequence.
Pre: root is the entry node of a tree or subtree
Post: each node has been processed in order
if root is not null then
inOrder(root->left)
process(root)
inOrder(root->right)
end
Return
End inOrder

Basic Tree Concepts
Binary Trees
Expression Trees
Binary Search Trees

Tree concepts.25


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×