Download Part Seven

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Quadtree wikipedia , lookup

Red–black tree wikipedia , lookup

Interval tree wikipedia , lookup

B-tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
Class Notes of Data Structures
Dr. David B. Y. Tsai May., 2009
IM-2B
Part Seven
Trees
1. Trees are nonlinear types of data structures.
2. This structure is mainly used to represent data containing a
hierarchical relationship between elements. e.g. records, family
trees and table of contents.
Binary Trees
1. Definition :
A binary tree T is defined as a finite set of nodes, such that:
(1) T is empty (called the null tree or empty tree), or
(2) T contains a distinguished node R, called the root of T, and
the remaining nodes of T form an ordered pair of disjoint
binary trees T1 and T2.
2. Property :
(1) T1 and T2.are called, respectively, the left and right subtrees
of R.
(2) If T1. is nonempty, then its root is called the left successor of
R; similarly, if T2 in nonempty, the its root is called the right
successor of R.
(3) Any node N in a binary tree T has either 0, 1 or 2 successors.
The nodes with no successors are called terminal nodes (or
leaves).
(4) If node N is a terminal node, the both its left and right
subtrees are empty.
3. Terminology
(1) Parent:
Suppose N is a node in T with left successor S1 and right
successor S2. Then N is called the parent (or father) of S1
and S2. Similarly, S1 is called the left child (or son) of N, and
S2 is called the right child (or son) of N. S1 and S2 are said to
be siblings (or brothers)
(2) Descendant and Ancestor
A node L is called a descendant of node N (and N is called an
ancestor of L) if there is a succession of children from N to L.
(3) An edge
The line drawn from a node N of T to a successor is called an
edge.
(4) A path
A sequence of consecutive edges is called a path.
(5) A branch
A path ending in a leaf is called a branch.
(6) A level number
Each node in a binary tree T is assigned a level number. The
1
Class Notes of Data Structures
Dr. David B. Y. Tsai May., 2009
IM-2B
root R of the tree T is assigned the level number 0, and every
other node is assigned a level number which is 1 more than
the level number of its parent. In addition, those nodes
with the same level number are called to belong to the same
generation.
(7) Depth (or height)
The depth (or height) of a tree T is the maximum number of
nodes in a branch of T. This turns out to be 1 more than
the largest level number of T.
Extended Binary Trees: 2-Trees
A binary tree is call a 2-tree in which each node has either 0 or 2
children. In such a case, the nodes with 2 children are called
internal nodes, and the nodes with 0 children are called external
nodes.
Fig. 7-1
Fig. 7-2
Full Binary Trees
A full binary tree is a binary tree in which all of the leaves are on the
same level and every non-leaf node has two children.
Complete Binary Trees
A complete binary tree is a binary tree that is either full or full
through the next-to-last level, with the leaves on the last level as far
to the left as possible.
Fig. 7-3
Fig. 7-6
Fig. 7-4
Fig. 7-7
2
Fig. 7-5
Fig. 7-8
Class Notes of Data Structures
Dr. David B. Y. Tsai May., 2009
IM-2B
Traversing Binary Trees
There are three standard ways of traversing a binary Tree T with root
R. They, called preorder, inorder and postorder, are as follows:
Preorder:
1. Visit the root R.
2. Traverse the left subtree of R in preorder.
3. Traverse the right subtree of R in preorder.
Inorder:
1. Traverse the left subtree of R in inorder.
2. Visit the root R.
3. Traverse the right subtree of R in inorder.
Postorder:
1. Traverse the left subtree of R in postorder.
2. Traverse the right subtree of R in postorder.
3. Visit the root R.
A
c
B
D
E
H
G
F
J
L
K
J
Fig. 7-9
Traversing Binary Tress Using Recursive Algorithms
Inorder Traversal Algorithms Using Stacks
Initially push Null onto STACK and then set PTR = ROOT. Then repeat
the following step (1) and (2) until NULL is popped from STACK.
(1) Proceed down the left-most path rooted at PTR, pushing each
node N onto STACK and stopping when a node N with no left
child is pushed onto STACK.
(2) Pop and process the nodes on STACK. If NULL is popped, then Exit.
If a node N with a right child R(N) is processed, set PTR = R(N) (ex.
PTR = RIGHT[PTR]) and return to step (1).
Each node in the tree is exactly processed only once. The rules should be followed:
(1) A node removed from the stack should be immediately processed.
(2) Once a node is removed from the stack, do not push it back onto the stack.
3
Class Notes of Data Structures
Dr. David B. Y. Tsai May., 2009
IM-2B
EXAMPLE:Non-recursive preorder Traversal Algorithm
1. CurrentNode = Root;
//start the traversal at the root node
2. while (CurrentNode is not null or stack is nonempty)
if (CurrentNode is not null)
visit CurrentNode;
push CurrentNode.rlink onto stack;
CurrentNode = CurrentNode.llink;
else
pop stack into CurrentNode;
A
CurrentNode
c
B
D
E
F
E
H
G
J
L
C
K
J
stack
1.
2.
3.
4.
CurrentNode is pointing to node D at the moment.
Node A and node B have been visited so far.
Node C and node E have been pushed onto stack.
After visiting node D, push Null (CurrentNode.rlink) onto stack.
4