COSC2007 Data Structures II Chapter 10 Trees I. 2 Topics Terminology.
-
Upload
lauren-burns -
Category
Documents
-
view
220 -
download
2
Transcript of COSC2007 Data Structures II Chapter 10 Trees I. 2 Topics Terminology.
COSC2007 Data Structures II
Chapter 10
Trees I
2
Topics
Terminology
3
Introduction & Terminology
Review Position-oriented ADT:
Insert, delete, or ask questions about data items at specific position
Examples: Stacks, Queues Value-oriented ADT:
Insert, delete, or ask questions about data items containing a specific value
4
Introduction & Terminology
Hierarchical (Tree) structure: Parent-child relationship between elements of the
structure Nonlinear One-to-many relationships among the elements
Examples: Organization chart Library card-catalog Contents of Books More???
5
Introduction & Terminology Corporate structure
6
Introduction & Terminology
Example: Library card catalogCard
Catalog
Subject Catalog
Author Catalog
Title Catalog
A - Abbex Stafford , R - Stanford, R
Zon - Zz
Stafford, R. H.
Standish, T. A.
Stanford Research Institute
7
Introduction & Terminology Table of contents of a book:
8
Trees and Tree Terminology
A tree is a data structure that consists of a set of nodes and a set of edges (or branches) that connect nodes.
9
Introduction & Terminology A is the root node. B is the parent of D and E. C is the sibling of B D and E are the children of B D, E, F, G, I are external nodes, or
leaves A, B, C, H are internal nodes The depth (level) of E is 2 The height of the tree is 3/4 The degree of node B is 2
10
Introduction & Terminology Path from node n1 to nk:
A sequence of nodes n1, n2, … . nk such that there is an edge between each pair of nodes (n1, n2) (n2, n3), . . . (nk-1, nk)
Height h: The number of nodes on the longest path from the root
to a leaf Ancestor-descendent relationship
Generalization of parent-child relationship Ancestor of node n:
A node on the path from the root to n Descendent of node n:
A node on the path from n to a leaf
11
Introduction & Terminology
Nodes: Contains information of an element Each node may contain one or more pointers
to other nodes A node can have more than one immediate
successor (child) the lies directly below it Each node has at most one predecessor
(parent) the lies directly above it
12
Binary Trees A binary tree is an ordered tree in which every node has at most two children. A binary tree is:
either empty or consists of a
root node (internal node) a left binary tree and a right binary tree
Each node has at most two children Left child Right child
13
Binary Trees
Special trees Left (right) subtree of node n:
In a binary tree, the left (right) child (if any) of node n plus its descendants
Empty BT: A binary tree with no nodes
14
Number Of Nodes-height is h Minimum number of nodes in a binary tree: h
At least one node at each of first h levels. Minimum number of nodes in a binary tree: 2h - 1
All possible nodes at first h levels are present
15
Number Of Nodes & Height
Let n be the number of nodes in a binary tree whose height is h.
h <= n <= 2h – 1
log2(n+1) <= h <= n
16
A
B
D E
H I J K
C
F G
LM N O
Full Binary Trees In a full binary tree,
every leaf node has the same depth every non-leaf node (internal node) has two children.
A
B
D E
H I J K
C
F G
L
Not a full binary tree. A full binary tree.
17
Complete Binary Tree In a complete binary tree
every level except the deepest must contain as many nodes as possible ( that is, the tree that remains after the deepest level is removed must be full).
at the deepest level, all nodes are as far to the left as possible.
A
B
D E
H I J K
C
F G
L
Not a Complete Binary Tree
A
B
D E
H I J K
C
F G
L
A Complete Binary Tree
18
Proper Binary Tree In a proper binary tree,
each node has exactly zero or two children each internal node has exactly two children.
A
B
D E
H I J K
C
F G
L
A
B
D E
H I J K
C
F G
Not a proper binary tree A proper binary tree
19
An Aside: A Representation for Complete Binary Trees
Since tree is full, it maps nicely onto an array representation.
A
B
D E
H I J K
C
F G
L
0 1 2 3 4 5 6 7 8 9 10 11 12
A B C D E F G H I J K LT:
last
20
Properties of the Array Representation
Data from the root node is always in T[0]. Suppose some node appears in T[i]
data for its parent is always at location T[(i-1)/2]
(using integer division) data for it’s children nodes appears in locations
T[2*i+1] for the left child
T[2*i+2] for the right child formulas provide an implicit representation of the edges can use these formulas to implement efficient algorithms for
traversing the tree and moving around in various ways.
21
Not Complete or Full or Proper
A
B
D E
H I J K
C
F
L
22
Balanced vs. Unbalanced Trees 1
A
B
D E
H I J K
C
F G
L
root
A
B
D
E
H I
J K
C
F G
L
start
Sort of BalancedMostly Unbalanced
A binary tree in which the left and right subtrees of any node have heights that differ by at most 1
23
Binary search tree (BST)
A binary tree where The value in any node n is greater than the
value in every node in n’s left subtree The value in any node n is less than the
value of every node in n's right subtree The subtrees are binary search trees too
60
7020
10 40
5030
24
Comparing Trees
These two trees are not the same tree!
A
B
A
B
Why?