You are on page 1of 12

Trees

+
• Family Tree
• Parse Trees x *
– e.g. for x +(y *(z+w))
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
a
• parent
b c d e
• children
• siblings
f g h i j
• degree

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 Tk
T2
Intro to Data Structures and Algorithms © ADT - Trees, Slide 3

Trees - More Terminology


• path
• ancestor
a
• descendent
• subtree b c d e

• leaf
f g h i j
• height
• depth
k l m

Intro to Data Structures and Algorithms © ADT - Trees, Slide 4

2
Example - Book
• Book Book

– Chapter 1
• Section 1.1 Ch 1 Ch 2 Ch 3

• Section 1.2
– Chapter 2 S 1.1 S 1.2 S 2.1 S 2.2 S 2.3

• Section 2.1
– 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

Leftmost-Child
Right-Sibling

Intro to Data Structures and Algorithms © ADT - Trees, Slide 6

3
Binary Trees
Recursive Definition: root
A binary tree
• contains no nodes (L),
or,
left right
• has 3 disjoint sets of nodes:
– 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

¹ 2 7
2 7

4 1 5
4 1 5

Ordered Tree
Binary Tree 6
6

Intro to Data Structures and Algorithms © ADT - Trees, Slide 8

4
Full Binary Tree:
Each node either 3
• has degree 0 = is a leaf
or
• has degree 2 2 7

4 1 5

Intro to Data Structures and Algorithms © ADT - Trees, Slide 9

Positional Trees
Recursive Definition:

A k-ary tree
• contains no nodes (L),
or,
• has k+1 disjoint sets of nodes:
– a root
– k k-ary sub-trees

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 k-ary Tree


• All leaves have same depth
• All internal nodes have degree k

• k-ary tree of height h has:


– k nodes at depth 1
– k*k = k2 nodes at depth 2
...
– k*k*...*k = kh leaves
kh-1
– 1 + k + k2+...+kh-1 = internal nodes
k-1

Intro to Data Structures and Algorithms © ADT - Trees, Slide 12

6
Tree ADT
Make-Tree(r,T1,…,Tk) - make a tree T with root r
and T1,T2,…,Tk as its sub-trees

Root(T) - return the root of tree T

Parent(n,T) - return the parent of node n


L if n is the root

Leftmost-Child(n,T) - return the first child of n


L if n is a leaf

Right-Sibling(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


Make-Tree(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

Left-Child(n,T) - return the left child of n


L if n is a leaf

Right-Child(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 3

4 5 9 10

6 7 8

1 2 3 4 5 6 7 8 9 10

0 1 1 2 2 5 5 5 3 3

Parent representation
Intro to Data Structures and Algorithms © ADT - Trees, Slide 15

Trees - Implementation
root
1 2 3
1 2 4 5
3 9 10
2 3
4

5 6 7 8
4 5 9 10
6

7
6 7 8
8 root 1

10

head
Intro to Data Structures and Algorithms © ADT - Trees, Slide 16

8
Binary Tree Data structure
root

Intro to Data Structures and Algorithms © ADT - Trees, Slide 17

K-ary 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

B C D

Example E F G H

element parent leftmost child right sibling

0 A -1 1 -1
1 B 0 -1 2
2 C 0 4 3
3 D 0 7 -1
4 E 2 -1 5
5 F 2 -1 6
6 G 2 -1 -1
7 H 3 -1 -1

Intro to Data Structures and Algorithms © ADT - Trees, Slide 20

10
Example

element parent leftmost child right sibling

0 A -1 1 -1
1 B 0 -1 2
2 C 0 3 -1
3 D 2 -1 4
4 E 2 -1 -1
5 F -1 6 -1
6 G 5 -1 -1

Intro to Data Structures and Algorithms © ADT - Trees, Slide 21

Tree Walks

InOrder-Tree-Walk(x)
1 if x ¹ NIL
2 InOrder-Tree-Walk(left[x])
3 print key[x]
4 InOrder-Tree-Walk(right[x])

PreOrder-Tree-Walk(x) PostOrder-Tree-Walk(x)
1 if x ¹ NIL 1 if x ¹ NIL
2 print key[x] 2 PostOrder-Tree-Walk(left[x])
3 PreOrder-Tree-Walk(left[x]) 3 PostOrder-Tree-Walk(right[x])
4 PreOrder-Tree-Walk(right[x]) 4 print key[x]

Running Time: O(n)

Intro to Data Structures and Algorithms © ADT - Trees, Slide 22

11
Example
1

2 3

4 5 6 7

8 9

PreOrder: 1 2 4 5 8 3 6 9 7
InOrder: 4 2 8 5 1 6 9 3 7
PostOrder: 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: + a * - b c + d e
InOrder: ( (a) + ( (b-c) * (d+e) ) )
PostOrder: a b c – d e + * +

Intro to Data Structures and Algorithms © ADT - Trees, Slide 24

12

You might also like