Loading...

PPT – Binary Tree Traversals PowerPoint presentation | free to download - id: 7302d6-MjRiN

The Adobe Flash plugin is needed to view this content

Binary Tree Traversals

- Emma Price

Objectives

- To be able to
- Explain what a binary tree is.
- To traverse a binary tree using the three

different methods.

Binary Trees

- We all should know that a Binary Tree is not a

type of tree!

Binary Trees

- In computer science, a tree is a widely-used data

structure that emulates a tree structure with a

set of linked nodes. - In computer science, a binary tree is a tree data

structure in which each node has at most two

children. Typically the child nodes are called

left and right. One common use of binary trees is

binary search trees another is binary heaps - Each node on the tree contains two links to two

other trees, both referred to as subtrees. The

two subtrees are often called the left and right

subtree.

Revision

- Binary
- Composed of two parts
- Represents numeric values using two symbols

typically 0 and 1. - What is recursion?
- A function that calls itself with a similar

subset of numbers with which it began. - The function must be aware that an end state

exists which terminates the recursive process.

Sub-tree

Traversing a Binary Tree

- Traversing a tree means visiting all the nodes of

a tree in order. - There are three methods of traversing a Binary

tree - Preorder traversal
- Inorder traversal
- Postorder traversal
- In each case, the algorithms are recursive-they

call themselves.

Binary Tree Traversals

- The reason we transverse a binary tree is to

examine each of its nodes. - Many different binary tree algorithms involve

traversals. - For example if we wish o find the largest value

in each node, we must examine the value contained

in each mode.

Preorder

1.Start at the root node 2.Traverse the left and

subtree 3.Traverse the righthand subtree

Preorder

10

10

12

5

5

12

11

15

3

7

3

7

11

15

Preordered for the above tree will be 10, 5, 3,

7, 12, 11, 15

Inorder

1.Traverse the lefthand tree 2.Visit the

node 3.Traverse the righthand subtree

Inorder

10

10

12

5

5

12

11

15

3

7

3

7

11

15

Preordered for the above tree will be 3, 5, 7,

10, 11,12,15

Postorder

1.Traverse the left hand subtree 2.Traverse the

righthand subtree 3. Return to the root node.

Postorder

10

10

12

5

5

12

11

15

3

7

3

7

11

15

Preordered for the above tree will be 3, 7, 5,

11, 15,12, 10

Conclusion

- The major advantage of binary search trees is

that the related sorting algorithms and search

algorithms such as in-order traversal can be very

efficient.

Reminder In computer science and mathematics, a

sorting algorithm is an algorithm that puts

elements of a list in a certain order