• No results found

Introduction to Trees

N/A
N/A
Protected

Academic year: 2020

Share "Introduction to Trees"

Copied!
74
0
0

Loading.... (view fulltext now)

Full text

(1)

Introduction to Trees

 

Data Structures

(2)

Basic Tree Concepts

A tree consists of a finite set of elements, called nodes, and a  finite set of directed lines, called branches, that connect the  nodes.

Tree

Nodes

Branches

(3)

Basic Tree Concepts (cons.)

Degree:

Indegree When the branch is directed toward the node, it is  indegree branch.

Outdegree When the branch is directed away from the node,  it is an outdegree branch.

Degree of the node: indegree + outdegree

AA

BB EE FF

CC DD GG HH II

The node B indegree = 1 The node B outdegree = 2 The node B degree =1+ 2 = 3

(4)

Basic Tree Concepts (Cont.)

Root

If the tree is not empty, the first node is called the root.

Indegree = ZERO

With the exception of the root, all of the nodes in a tree must  have an indegree of exactly one; that is, they may have only  one predecessor.

AA

BB EE FF

CC DD GG HH II

(5)

Basic Tree Concepts (Cont.)

All nodes in the tree can have zero, one, or more branches 

leaving them; that is, they may have outdegree of zero, one, or  more.

Leaf 

It is any node with an outdegree of zero, that is, a node  with no successors.

AA

BB EE FF

CC DD GG HH II

(6)

Basic Tree Concepts (Cont.)

Internal node

A node that is not a root or a leaf

AA

BB EE FF

CC DD GG HH II

(7)

Basic Tree Concepts (Cont.)

Parent node

It has a successor node, its outdegree is > 0

Child node? (vs.)

It has a predecessor node, its indegree is 1 

Siblings

Two or more nodes with the same parent

AA

BB EE FF

CC DD GG HH II

(8)

Basic Tree Concepts (Cont.)

Ancestor 

Any node in the path from the root to the node

Descendent

Any node in the path below the parent node

All nodes in the paths from a given node to a leaf node

AA

BB EE FF

CC DD GG HH II

(9)

Basic Tree Concepts (Cont.)

Branch AF Branch

AF

Branch FI Branch FI

Parents: A, B, F

Children: B, E, F, C, D, G, H, I

Siblings: {B, E, F}, {C, D}, {G, H, I}

Leaves: C, D, E, G, H, I Internal nodes: B, F

AA

BB EE FF

CC DD GG HH II

(10)

Basic Tree Concepts (Cont.)

Path 

Sequence of nodes in which each node is adjacent to the  next node

Level 

Level of a node is its distance from the root

The root has a zero distance from itself -> it is at level (0)

Siblings levels are the same

AA

BB EE FF

CC DD GG HH II

Level 0 Level 1

Level 2

Path from the root to node I is AFI

Path from the root to node I is AFI

(11)

Basic Tree Concepts (Cont.)

Height

Height of a tree=the level of the leaf of the longest path + 1

The height of an empty tree is -1

Depth == Height 

AA

BB EE FF

CC DD GG HH II

Level 0 Level 1

Level 2

Path from the root to node I is AFI

Path from the root to node I is AFI

(12)

Basic Tree Concepts (Cont.)

Subtree

Any connected structure below the root

First node = root -> used to name the subtree

Subtrees can be divided into subtrees

A single node is a subtree

AA

BB EE FF

CC DD GG HH II

Subtree B

Subtree B Root of Subtree IRoot of Subtree I

(13)

Recursive Definition of a tree

A tree is a set of nodes that either:

Is empty

Has a designated node, called the root, from which  hierarchically

descend zero or more subtrees, which are all subtrees

(14)

Basic Tree Concepts (Cont.)

Tree Representation

Tree generally implemented in the computer using pointers

Outside the computer there are 3 representations

(15)

Tree Representation:

1- General tree

Computer Computer

CaseCase

CPUCPU CD ROMCD ROM

ALUALU ROMROM

Floppy disk Floppy

disk

Controlle r Controlle

r

. . .

. . .

. . . . . .

. . .

• Chart format: the notation of figures

(16)

Tree representation:

2- Indented list

Part Number Description

301 Computer

301-1 Case

. . . . . .

301-2 CPU

301-2-1 Controller

301-2-1 ALU . . . . . . 301-2-9 ROM

301-3 F Disk

. . . . . .

301-9 CD-ROM

. . . . . .

Bill-of-materials systems, parts list represent the assembly structure of an item

(17)

Tree representation 3- parenthetical listing

Algebraic expressions

Open parenthesis ( indicates the start of a new level

Closing parenthesis ) completes the current level and moves up one level in the tree

A (B(C D) E F (G H I)) A (B(C D) E F (G H I))

(18)

Binary trees

Binary tree: 

no node can have more than two subtrees.

A node can have 0, 1, or 2 subtrees (L and R)

Null tree: 

tree with no nodes

Symmetry is not a tree requirement

AA

BB EE

CC DD FF

Right subtree Left subtree

(19)

19

AA AA AA

BB BB

AA

BB CC

AA

BB CC

DD EE

AA

BB

CC

AA

BB

CC

(20)

Height of Binary Trees

Given N is the number of node in a binary tree

Maximum Height: Hmax = N

Minimum height: Hmin = [Log2N]+1

The shorter tree the easier it is to locate any desired node in  the tree.

AA

BB

CC

AA

BB CC

(21)

Number of Nodes in Binary Trees

21

Given H is the height of a binary tree

Minimum number of nodes: Nmin = H and

Maximum number of nodes: Nmax = 2H- 1

AA

BB

CC

AA

BB CC

(22)

22

Level Max nodes 0 1

1 2 2 4 3 8 4 16

N = 2 level

But: height = level +1 So: N = 2 height -1 

Max height = [log2N] +1

Binary trees

(23)

Binary Trees

Balance 

Balance factor: 

The difference in height between its left  and right subtree 

Balanced tree

Balance factor = 0 and its subtrees are  also balanced

Balanced tree in general

If the height of its subtrees differs by no  more than 1 (B= -1, 0, 1)

and its subtrees are also balanced

B = HL - HR B = HL - HR

(24)

AA AA AA

BB BB

AA

BB CC

AA

BB CC

DD EE

AA

BB

CC

AA

BB

CC

0 0 1 -1

0 1

-2 2

(25)

Binary Trees

Complete tree 

Has the maximum number of entries for its height 

The max num is reached when the last level is full 

Nmax = 2H - 1

Nearly complete tree

Has the minimum height of its nodes

And all nodes in the last level are found in the left

Hmin = [log2N]+1

(26)

AA AA

BB CC

AA

BB CC

DD EE

AA

BB EE

CC DD FF GG

AA

BB CC

DD EE FF

AA

BB CC

DD

Nearly complete trees (at level 2) Complete trees (at levels 0, 1 and 2)

(27)

Binary trees

Each node in the structure must contain the data to be stored  and two pointers, one in L subtree and one in the R subtree  

(28)

Binary tree traversal

Requires that each node of the tree be processed once and only  once in a predetermined sequence

Depth-First traversal

» process all of the descendents of a child before going on to  the next child.

Breadth-First traversal

» Each level is completely processed before the next level is  started

A

B E

C D F

A

B E

C D F

(29)

Depth-First traversal

Given a binary tree having a root, LST, RST:

Define 6 DFT sequences

3 2

1

Preorder traversal NLR

L R

3 1

2

L R

2 1

3

L R

Inorder traversal LNR

Postorder traversal LRN

Standard Traversals

(30)

Depth-First traversal

Preorder traversal: NLR

root goes before the left and right subtrees

3 2

1

Preorder traversal NLR

L R

(31)

Depth-First traversal

31

A

B E

C D F

(32)

Depth-First traversal

32

A

B E

C D F

PROCESSING ORDER

(33)

Depth-First traversal

33

A

B E

C D F

PROCESSING ORDER A

(34)

Depth-First traversal

34

A

B E

C D F

PROCESSING ORDER A

(35)

Depth-First traversal

35

A

B E

C D F

PROCESSING ORDER AB

(36)

Depth-First traversal

36

A

B E

C D F

PROCESSING ORDER AB

(37)

Depth-First traversal

37

A

B E

C D F

PROCESSING ORDER ABC

(38)

Depth-First traversal

38

A

B E

C D F

PROCESSING ORDER ABC

(39)

Depth-First traversal

39

A

B E

C D F

PROCESSING ORDER ABCD

(40)

Depth-First traversal

40

A

B E

C D F

PROCESSING ORDER ABCD

(41)

Depth-First traversal

41

A

B E

C D F

PROCESSING ORDER ABCDE

(42)

Depth-First traversal

42

A

B E

C D F

PROCESSING ORDER ABCDE

(43)

Depth-First traversal

43

A

B E

C D F

PROCESSING ORDER ABCDEF

(44)

Depth-First traversal

44

3 1

2

L R

Inorder traversal LNR

Inorder traversal: LNR

(IN) root is processed in between subtrees

(45)

Depth-First traversal

45

PROCESSING ORDER

A

B E

C D F

(46)

Depth-First traversal

46

PROCESSING ORDER

A

B E

C D F

(47)

Depth-First traversal

47

PROCESSING ORDER

A

B E

C D F

(48)

Depth-First traversal

48

PROCESSING ORDER

A

B E

C D F

(49)

Depth-First traversal

49

PROCESSING ORDER C

A

B E

C D F

(50)

Depth-First traversal

50

PROCESSING ORDER CB

A

B E

C D F

(51)

Depth-First traversal

51

PROCESSING ORDER CB

A

B E

C D F

(52)

Depth-First traversal

52

PROCESSING ORDER CBD

A

B E

C D F

(53)

Depth-First traversal

53

PROCESSING ORDER CBDA

A

B E

C D F

(54)

Depth-First traversal

54

PROCESSING ORDER CBDA

A

B E

C D F

(55)

Depth-First traversal

55

PROCESSING ORDER CBDAE

A

B E

C D F

(56)

Depth-First traversal

56

PROCESSING ORDER CBDAE

A

B E

C D F

(57)

Depth-First traversal

57

PROCESSING ORDER CBDAEF

A

B E

C D F

(58)

Depth-First traversal

58

2 1

3

L R

Postorder traversal LRN

Postorder traversal: LRN

(Post) root is processed after (post) subtrees

(59)

Depth-First traversal

59

PROCESSING ORDER

A

B E

C D F

(60)

Depth-First traversal

60

PROCESSING ORDER

A

B E

C D F

(61)

Depth-First traversal

61

PROCESSING ORDER

A

B E

C D F

(62)

Depth-First traversal

62

PROCESSING ORDER

A

B E

C D F

(63)

Depth-First traversal

63

PROCESSING ORDER C

A

B E

C D F

(64)

Depth-First traversal

64

PROCESSING ORDER C

A

B E

C D F

(65)

Depth-First traversal

65

PROCESSING ORDER CD

A

B E

C D F

(66)

Depth-First traversal

66

PROCESSING ORDER CDB

A

B E

C D F

(67)

Depth-First traversal

67

PROCESSING ORDER CDB

A

B E

C D F

(68)

Depth-First traversal

68

PROCESSING ORDER CDB

A

B E

C D F

(69)

Depth-First traversal

69

PROCESSING ORDER CDBF

A

B E

C D F

(70)

Depth-First traversal

PROCESSING ORDER CDBFE

A

B E

C D F

(71)

Depth-First traversal

PROCESSING ORDER CDBFEA

A

B E

C D F

(72)

Breadth-First traversal

PROCESSING ORDER

A

B E

C F

ABECDF

D

(73)

Implementation

It is a routine, when the whole structure of BST is put into two  classes. 

Main class BinarySearchTree is a public interface and

 BSTNode mean for private use inside the main class.

 This division is necessary, because some operations, like 

removal, may result in an empty tree, which means that tree even  doesn't have a root node.

(74)

C++ Code

References

Related documents