This action might not be possible to undo. Are you sure you want to continue?
+
• Family Tree • Parse Trees
– e.g. for x +(y *(z+w))
x y
* + z w
• Trees to organize data bases / file systems
– e.g. The UNIX file system
• Search Trees
Intro to Data Structures and Algorithms © ADT  Trees, Slide 1
Rooted Trees Basic Terminology
• • • • • • nodes (vertices) root parent children siblings f degree
a b c d e
g
h
i
j
k
l
m
Intro to Data Structures and Algorithms ©
ADT  Trees, Slide 2
1
Trees  Recursive Definition
• The empty tree L has no nodes • Given trees T1, T2, …, Tk with roots r1, r2, …,rk respectively, and a node r, we can form the tree T by making r the root, and making r1, r2, …,rk the children of r
r T1
Intro to Data Structures and Algorithms ©
T2
Tk
ADT  Trees, Slide 3
Trees  More Terminology
• • • • • • • path ancestor descendent subtree leaf height depth
a b c d e
f
g
h
i
j
k
l
m
Intro to Data Structures and Algorithms ©
ADT  Trees, Slide 4
2
Example  Book
• Book
– Chapter 1
• Section 1.1 • Section 1.2
Ch 1 Ch 2 Ch 3 Book
– Chapter 2
• Section 2.1
S 1.1
S 1.2
S 2.1
S 2.2
S 2.3
– Subsection 2.1.1 – Subsection 2.1.2
S 2.1.1
S 2.1.2
• Section 2.2 • Section 2.3
– Chapter 3
Intro to Data Structures and Algorithms © ADT  Trees, Slide 5
Ordered Trees
The children of each node are ordered
a a
¹
b c c b
LeftmostChild RightSibling
Intro to Data Structures and Algorithms © ADT  Trees, Slide 6
3
Binary Trees
Recursive Definition: A binary tree • contains no nodes (L), or, • has 3 disjoint sets of nodes:
root
left
right
– a root – a binary subtree called its left subtree – a binary subtree called its right subtree
Intro to Data Structures and Algorithms ©
ADT  Trees, Slide 7
Example
Binary Trees
3 3
¹
7 5 4
2
2
7
4
1
1
5
Ordered Tree
Binary Tree
6
Intro to Data Structures and Algorithms ©
6
ADT  Trees, Slide 8
4
Full Binary Tree: Each node either • has degree 0 = is a leaf or 2 • has degree 2
3
7
4
1
5
6
Intro to Data Structures and Algorithms ©
ADT  Trees, Slide 9
Positional Trees
Recursive Definition: A kary tree • contains no nodes (L), or, • has k+1 disjoint sets of nodes:
– a root – k kary subtrees
Intro to Data Structures and Algorithms ©
ADT  Trees, Slide 10
5
Complete Binary Tree
Intro to Data Structures and Algorithms ©
ADT  Trees, Slide 11
Complete kary Tree
• All leaves have same depth • All internal nodes have degree k • kary tree of height h has: – k nodes at depth 1 – k*k = k2 nodes at depth 2 ... – k*k*...*k = kh leaves kh1 – 1 + k + k2+...+kh1 = internal nodes k1
Intro to Data Structures and Algorithms © ADT  Trees, Slide 12
6
Tree ADT
MakeTree(r,T1,…,Tk)  make a tree T with root r and T1,T2,…,Tk as its subtrees Root(T)  return the root of tree T Parent(n,T)  return the parent of node n L if n is the root LeftmostChild(n,T)  return the first child of n L if n is a leaf RightSibling(n,T)  return the right sibling of n L if n is rightmost
Intro to Data Structures and Algorithms © ADT  Trees, Slide 13
Binary Tree ADT
MakeTree(r,Tl,Tr)  make a tree T with root r and Tl,Tr as its subtrees Root(T)  return the root of tree T Parent(n,T)  return the parent of node n L if n is the root LeftChild(n,T)  return the left child of n L if n is a leaf RightChild(n,T)  return the right child of n L if n is rightmost
Intro to Data Structures and Algorithms © ADT  Trees, Slide 14
7
Trees  Implementation
1 2 4 5 6 7 8 1 0 2 1 3 1 4 2 5 2 6 5 7 5 8 5 9 3 10 3 9 3 10
Parent representation
Intro to Data Structures and Algorithms © ADT  Trees, Slide 15
Trees  Implementation
root
1
2 4 9
3 5 10
1 2 4 5 6 7 8 9 3 10
2 3 4 5 6 7 8 9 10
6
7
8
root
1
Intro to Data Structures and Algorithms ©
head
ADT  Trees, Slide 16
8
Binary Tree Data structure
root
Intro to Data Structures and Algorithms ©
ADT  Trees, Slide 17
Kary Tree Representaion
root
. . .
Parent Child1 Child2 Childk
Intro to Data Structures and Algorithms ©
ADT  Trees, Slide 18
9
Tree Data structure
root
Intro to Data Structures and Algorithms ©
ADT  Trees, Slide 19
A B E C F G D H
Example
element 0 1 2 3 4 5 6 7 A B C D E F G H parent 1 0 0 0 2 2 2 3 leftmost child 1 1 4 7 1 1 1 1
right sibling 1 2 3 1 5 6 1 1
ADT  Trees, Slide 20
Intro to Data Structures and Algorithms ©
10
Example
element 0 1 2 3 4 5 6 A B C D E F G parent 1 0 0 2 2 1 5 leftmost child 1 1 3 1 1 6 1 right sibling 1 2 1 4 1 1 1
ADT  Trees, Slide 21
Intro to Data Structures and Algorithms ©
Tree Walks
InOrderTreeWalk(x) if x ¹ NIL InOrderTreeWalk(left[x]) print key[x] InOrderTreeWalk(right[x])
PostOrderTreeWalk(x) 1 if x ¹ NIL 2 PostOrderTreeWalk(left[x]) 3 PostOrderTreeWalk(right[x]) 4 print key[x]
1 2 3 4
PreOrderTreeWalk(x) 1 if x ¹ NIL 2 print key[x] 3 PreOrderTreeWalk(left[x]) 4 PreOrderTreeWalk(right[x])
Running Time:
Intro to Data Structures and Algorithms ©
O(n)
ADT  Trees, Slide 22
11
Example
1 2 4 8 5 6 9 3 7
PreOrder: InOrder: PostOrder:
1 2 4 5 8 3 6 9 7 4 2 8 5 1 6 9 3 7 4 8 5 2 9 6 7 3 1
Intro to Data Structures and Algorithms ©
ADT  Trees, Slide 23
Example:
Arithmetic Tree:
a + *

+
b
c
d
e
PreOrder: InOrder: PostOrder:
Intro to Data Structures and Algorithms ©
+ a *  b c + d e ( (a) + ( (bc) * (d+e) ) ) a b c – d e + * +
ADT  Trees, Slide 24
12
This action might not be possible to undo. Are you sure you want to continue?
We've moved you to where you read on your other device.
Get the full title to continue listening from where you left off, or restart the preview.