Trees (Part 1, Theoretical)

16
Trees (Part 1, Theoretical) CSE 3318 Algorithms and Data Structures University of Texas at Arlington 1 10/12/2021

Transcript of Trees (Part 1, Theoretical)

Page 1: Trees (Part 1, Theoretical)

Trees

(Part 1, Theoretical)

CSE 3318 – Algorithms and Data Structures

University of Texas at Arlington

110/12/2021

Page 2: Trees (Part 1, Theoretical)

Trees

• Trees are a natural data structure for representing specific data.

– Family trees.

– Organizational chart of a corporation, showing who supervises who.

– Folder (directory) structure on a hard drive.

2

Page 3: Trees (Part 1, Theoretical)

Terminology• Root: 0

• Path: 0-2-6, 1-3, 1-3-4

• Parent vs child

• Ascendants vs descendants:

– ascendants of 3: 1,0 // descendants of 1: 5, 3,4

• Internal vs external nodes

(non-terminal vs terminal nodes)

• Leaves: 5,4,6,7

• M-ary trees (Binary trees)

• General trees

• Subtree3

0

1 2

5 3 6 7

4

Page 4: Trees (Part 1, Theoretical)

Terminology

• If Y is the parent of X, then X is called a child of Y.

– The root has no parents.

– Every other node, except for the root, has exactly one parent.

• A node can have 0, 1, or more children.

• Nodes that have children are called internal nodes

• Nodes that have no children are called external nodes, or leaves.

4

Page 5: Trees (Part 1, Theoretical)

5

A

B

C

Practice: - Give the level, depth and height for each of the red nodes.- How many nodes are on each level? __________________________________

Node level depth height

A

B

C

Terminology - Worksheet• The level of the root is defined to be 0.

• The level of each node is defined to be 1+ the level of its parent.

• The depth of a node is the number of edges from the root to the node. (It is equal to the level of that node.)

• The height of a node is the number of edges from the node to the deepest leaf. (Treat that node as the root of a small tree)

Page 6: Trees (Part 1, Theoretical)

Terminology - Answers

6

A

B

C

Practice: - Give the level, depth and height for each of the red nodes.- How many nodes are on each level? ________1, 2, 4, 8 __________________

Node level depth height

A 0 0 3

B 2 2 1

C 3 3 0

• The level of the root is defined to be 0.

• The level of each node is defined to be 1+ the level of its parent.

• The depth of a node is the number of edges from the root to the node. (It is equal to the level of that node.)

• The height of a node is the number of edges from the node to the deepest leaf. (Treat that node as the root of a small tree)

level

0

1

2

3

Page 7: Trees (Part 1, Theoretical)

M-ary Trees - Worksheet

• An M-ary tree is a tree where every node is either a leaf or it has exactly M children.

• Example: binary trees, ternary trees, ...

7

0

1

7

2

5 3

4

6

0

1

7

2

6

Is this a binary tree?

Is this a binary tree?

Page 8: Trees (Part 1, Theoretical)

M-ary Trees - Answers

• An M-ary tree is a tree where every node is either a leaf or it has exactly M children.

• Example: binary trees, ternary trees, ...

8

0

1

7

2

5 3

4

6

0

1

7

2

6

This is not a binary tree, node 3 has 1 child.

This is a binary tree.

Page 9: Trees (Part 1, Theoretical)

Types of binary trees• Perfect – each internal node has exactly 2 children

and all the leaves are on the same level.– E.g. ancestry tree (anyone will have exactly 2 parents).

• Full – every node has exactly 0 or 2 children. – Tree of recursive calls when there are 2 recursive calls

• E.g. tree generated by the Fibonacci recursive calls.

• => his properties are used in analyzing time complexity of such rec fcts.

– Binary tree.

• Complete tree – every level, except for possibly the last one is completely filled and on the last level, all the nodes are as far on the left as possible.– E.g. the heap tree.

– Height: and it can be stored as an array.

Reference: wikipedia (https://en.wikipedia.org/wiki/Binary_tree)9

Nlg

Perfect tree

Complete tree

Page 10: Trees (Part 1, Theoretical)

Perfect Binary Trees

10

Level Nodes per level

Sum of nodes from root up to this level

0 20 (=1) 21 – 1 (=1)

1 21 (=2) 22 – 1 (=3)

2 22 (=4) 23 – 1 (=7)

… …

i 2i 2i+1 – 1

… …

h-1 2h-1 2h-1

h 2h 2h+1 – 1

σ𝑘=0ℎ 2𝑘 = 2ℎ+1 − 1

1

2 3

7654

. . .

. . . . . . . . . . . . . . . . . . . . .

A perfect binary tree with N nodes and height h, has: • leaves (half the nodes are on the last level)• internal nodes (half the nodes are internal)• Height : • Levels : (= lg(N+1) ) 1lg N

2/N

2/N

Nh lg

In the other direction: N = 2h+1 -1

Page 11: Trees (Part 1, Theoretical)

– All levels are full, except possibly for the last level.

– At the last level:• Nodes are on the left.

• Empty positions are on the right.

– There is “no hole”

11

Good

BadBad Bad

2 1 1 3 4 1

3 5 4 3

7 5

91

2 3

4 5 6 7

8 9 10 11 12 13

Complete Tree

Page 12: Trees (Part 1, Theoretical)

Worksheet

• Self study: Give examples of trees that are:

– Perfect

– Full but not complete

– Complete but not full

– Neither full not complete

12

Page 13: Trees (Part 1, Theoretical)

133 6 2 1 0 4 0

7 5 1 4

8 4

9

2 4 1 3 5 5 5 5

3 5 5 5

7 5

9

1 3 5 5 5 5

3 5 5 5

7 5

9

2 1 1 3 5 5

3 5 5

7 5

9

E1

E3

E5E4

1 3 55 5

3 5 5 5

7 5

9E2

For each tree, say if it is full, complete or perfect.

Worksheet

Page 14: Trees (Part 1, Theoretical)

143 6 2 1 0 4 0

7 5 1 4

8 4

9

2 4 1 3 5 5 5 5

3 5 5 5

7 5

9

1 3 5 5 5 5

3 5 5 5

7 5

9

2 1 1 3 5 5

3 5 5

7 5

9

AnswersPerfect

Full, not complete

Neither full,nor complete

E1

E3

E5E4

For each tree, say if it is perfect, full or complete.

Neither full,nor complete

1 3 55 5

3 5 5 5

7 5

9

Complete,not full

E2

Page 15: Trees (Part 1, Theoretical)

Properties of Full Trees• A full binary tree (0/2 children) with P internal nodes has:

– P+1 external nodes.

– 2P edges (links).

– height at least lg P and at most P:• lg P if all external nodes are at the same level (perfect tree)

• P if each internal node has one external child.

15

Page 16: Trees (Part 1, Theoretical)

Proof

Prove that a full tree with P internal nodes has P+1 external leaves.

• Full tree property:

• What proof style will you use?

16