Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm...

30
Chapter 4: Trees Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java

Transcript of Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm...

Page 1: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Chapter 4: TreesChapter 4: Trees

Part I: General Tree Concepts

Mark Allen Weiss: Data Structures and Algorithm Analysis in Java

Page 2: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Trees

DefinitionsRepresentationBinary treesTraversalsExpression trees

2

Page 3: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Definitions

3

tree - a non-empty collection of vertices & edgesvertex (node) - can have a name and carry other associated informationpath - list of distinct vertices in which successive vertices are connected by edgesany two vertices must have one and only one path between them else its not a tree

a tree with N nodes has N-1 edges

Page 4: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Definitions

root - starting point (top) of the tree

parent (ancestor) - the vertex “above” this vertex

child (descendent) - the vertices “below” this vertex

4

Page 5: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Definitions

leaves (terminal nodes) - have no children

level - the number of edges between this node and the root

ordered tree - where children’s order is significant

5

Page 6: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Definitions

Depth of a node - the length of the path from the root to that node• root: depth 0

Height of a node - the length of the longest path from that node to a leaf• any leaf: height 0

Height of a tree: The length of the longest path from the root to a leaf

6

Page 7: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Balanced Trees

the difference between the height of the left sub-tree and the height of the right sub-tree is not more than 1.

7

Page 8: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Trees - Example

8

E

R

T

ELPM

EA

SA

root

Leaves or terminal nodes

Child (of root)

Depth of T: 2

Height of T: 1

Level

0

1

3

2

Page 9: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Tree Representation

9

Class TreeNode { Object element; TreeNode firstChild; TreeNode nextSibling; }

Page 10: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Example

10

a

b fe

c d g

a

b e

c d

f

g

Page 11: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Binary Tree

11

S

A

B

N

O

N

P

D

M

I

S Internal node

External node

Page 12: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Height of a Complete Binary Tree

12

L 0

L 1

L 2

L 3

At each level the number of the nodes is doubled. total number of nodes: 1 + 2 + 22 + 23 = 24 - 1 = 15

Page 13: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Nodes and Levels in a Complete Binary Tree

13

Number of the nodes in a tree with M levels:

1 + 2 + 22 + …. 2M = 2 (M+1) - 1 = 2*2M - 1

Let N be the number of the nodes.

N = 2*2M - 1, 2*2M = N + 12M = (N+1)/2M = log( (N+1)/2 )

N nodes : log( (N+1)/2 ) = O(log(N)) levelsM levels: 2 (M+1) - 1 = O(2M ) nodes

Page 14: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Binary Tree Node

14

Class BinaryNode

{

Object Element; // the data in the node

BinaryNode left; // Left child

BinaryNode right; // Right child

}

Page 15: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Binary Tree – Preorder Traversal

15

C

LR

E

T

D

O

N

U

M

P

A

Root Left Right

First letter - at the root

Last letter – at the rightmost node

Page 16: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Preorder Algorithm

16

preorderVisit(tree){ if (current != null) { process (current);

preorderVisit (left_tree);preorderVisit (right_tree);

}}

Page 17: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Binary Tree – Inorder Traversal

17

U

AE

R

T

N

P

D

M

O

C

L

LeftRootRight

First letter - at the leftmost node

Last letter – at the rightmost node

Page 18: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Inorder Algorithm

18

inorderVisit(tree){ if (current != null) {

inorderVisit (left_tree); process (current);

inorderVisit (right_tree); }}

Page 19: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Binary Tree – Postorder Traversal

19

D

LU

A

N

E

P

R

O

M

C

T

LeftRightRoot

First letter - at the leftmost node

Last letter – at the root

Page 20: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Postorder Algorithm

20

postorderVisit(tree){ if (current != null) {

postorderVisit (left_tree);postorderVisit (right_tree);process (current);

}}

Page 21: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Expression Trees

21

1 2

The stack contains references to tree nodes (bottom is to the left)

+

1 2

3*

+

1 2

3

(1+2)*3

Post-fix notation: 1 2 + 3 *

Page 22: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Expression Trees

22

In-order traversal:

(1 + 2) * ( 3)

Post-order traversal:

1 2 + 3 *

*

+

1 2

3

Page 23: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Binary Search Trees

DefinitionsOperations and complexityAdvantages and disadvantagesAVL Trees

Single rotationDouble rotation

Splay TreesMulti-Way Search

23

Page 24: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Definitions

24

Each node – a record with a key

and a valuea left link a right link

All records with smaller keys – left subtreeAll records with larger keys – right subtree

Page 25: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Example

25

Page 26: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Operations

Search - compare the values and proceed either to the left or to the right

Insertion - unsuccessful search - insert the new node at the bottom where the search has stopped

Deletion - replace the value in the node with the smallest value in the right subtree or the largest value in the left subtree.

Retrieval in sorted order – inorder traversal

26

Page 27: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Complexity

27

Logarithmic, depends on the shape of the tree

In the worst case – O(N) comparisons

Page 28: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Advantages of BST

SimpleEfficientDynamic

One of the most fundamental algorithms in CS

The method of choice in manyapplications

28

Page 29: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Disadvantages of BST

The shape of the tree depends on the order of insertions, and it can be degenerated.

When inserting or searching for an element, the key of each visited node has to be compared with the key of the element to be inserted/found.

Keys may be long and the run time may increase much.

29

Page 30: Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Improvements of BST

Keeping the tree balanced:

AVL trees (Adelson - Velskii and Landis)

Balance condition: left and right subtrees of each node can differ by at most one level.

It can be proved that if this condition is observed the depth of the tree is O(logN).

Reducing the time for key comparison: Radix trees - comparing only the leading bits of the keys (not discussed here)

30