Docsity

Abstract Data Types (ADTs, Schemes and Mind Maps of Computer Applications

Partial preview of the text

Download Abstract Data Types (ADTs and more Schemes and Mind Maps Computer Applications in PDF only on Docsity!

Data Structures and

Algorithms

Data Structures and

Algorithms

LECTURE 07: BINARY TREE LECTURE 07: BINARY TREE

DefinitionsDefinitions

Exact (Formal) definitionExact (Formal) definition

Empty structure is an empty tree

Non-empty tree consists of a root and its children, where these children are

also trees

Empty structure is an empty tree

Non-empty tree consists of a root and its children, where these children are

also trees

Given a treeGiven a tree

Tree examplesTree examples

Tree examplesTree examples

Binary treeBinary tree

Definition, Types, Traversal Definition, Types, Traversal

Binary TreesBinary Trees

ļ‚š Binary tree: each node has at most two children

Empty tree is a binary tree

Each child may be empty or designated as either left child or right child

ļ‚š Binary tree: each node has at most two children

Empty tree is a binary tree

Each child may be empty or designated as either left child or right child

Tree TraversalTree Traversal

ļ‚š Tree traversal :

Process of visiting each node in the tree exactly one time

ļ‚š Breadth-first traversal :

Visiting each node starting from lowest (or highest) level and moving down

(or up) level by level

Visiting nodes on each level from left to right (or from right to left)

ļ‚š Tree traversal :

Process of visiting each node in the tree exactly one time

ļ‚š Breadth-first traversal :

Visiting each node starting from lowest (or highest) level and moving down

(or up) level by level

Visiting nodes on each level from left to right (or from right to left)

Breadth-first Tree traversal

example

Breadth-first Tree traversal

example

Bread-first traversal:

A, B, C, D, E, F, G, H, I

Tree traversalTree traversal

ļ‚š Depth-first traversal:

Proceeds as far as possible to the left (or right)

Then backs up until the first crossroad

Goes one step to the right (or left)

Again, as far as possible to the left (or right)

ļ‚š There are three tasks to be done:

V: Visiting a node

L: Traversing the left subtree

R: Traversing the right subtree

ļ‚š Depth-first traversal:

Proceeds as far as possible to the left (or right)

Then backs up until the first crossroad

Goes one step to the right (or left)

Again, as far as possible to the left (or right)

ļ‚š There are three tasks to be done:

V: Visiting a node

L: Traversing the left subtree

R: Traversing the right subtree

Ways of depth-first traversalWays of depth-first traversal

ļ‚š Three tasks can be ordered in 3!=6 ways

ļ‚š There are 6 possible ordered depth-first traversals

ļ‚š It can be reduced to 3 traversals

The move is always from left to right and

Attention is focused on the first column.

ļ‚š The 3 traversals are given these standard names

VLR: Preorder traversal (or NLR)

LVR: Inorder traversal (or LNR)

LRV: Postorder (or LRN)

ļ‚š Three tasks can be ordered in 3!=6 ways

ļ‚š There are 6 possible ordered depth-first traversals

ļ‚š It can be reduced to 3 traversals

The move is always from left to right and

Attention is focused on the first column.

ļ‚š The 3 traversals are given these standard names

VLR: Preorder traversal (or NLR)

LVR: Inorder traversal (or LNR)

LRV: Postorder (or LRN)

Tree Traversal ExampleTree Traversal Example

Preorder traversal: F, B, A, D, C, E, G, I, H

(root, left, right)

Inorder traversal: A, B, C, D, E, F, G, H, I (left,

root, right)

Postorder traversal: A, C, E, D, B, H, I, G, F

(left, right, root)

Level-order traversal (breadth-first): F, B, G,

A, D, I, C, E, H

Preorder traversal: F, B, A, D, C, E, G, I, H

(root, left, right)

Inorder traversal: A, B, C, D, E, F, G, H, I (left,

root, right)

Postorder traversal: A, C, E, D, B, H, I, G, F

(left, right, root)

Level-order traversal (breadth-first): F, B, G,

A, D, I, C, E, H

Binary tree implementationBinary tree implementation