Docsity

Tree Traversals: A Comprehensive Guide with Examples and Applications, Exams of Data Structures and Algorithms

Partial preview of the text

Download Tree Traversals: A Comprehensive Guide with Examples and Applications and more Exams Data Structures and Algorithms in PDF only on Docsity!

A. Sam Silva

J. Jeya Praise

TREE TRAVERSALS

Synopsis

Introduction

Tree Terminologies

Binary Tree

Tree Traversal

Application

Conclusion

Tree Collection of nodes  (^) The collection can be empty  (^) Consists of finite set of elements, called nodes, and a finite set of directed lines called branches, that connect the nodes.

Structure of Tree H

G
I
F
D E
B
J
A
C

Level 0 Level 1 Level 2 Level 3

ROOT

LEAF

NODE

Basic Tree Concepts  (^) Root

  • (^) Top most element of the tree Root = A  (^) Node
  • (^) Each data item in the tree Node = {A,B,C,D,E,F,G,H,I,J} H
G
I
F
D E
B
J
A
C

Basic Tree Concepts  (^) Degree of node

  • (^) Maximum number of sub-tree of a node Deg {A,B,C,G} = 2 Deg {E} = 1 Deg {D,F,H,I,J} = 0 H
G
I
F
D E
B
J
A
C

Basic Tree Concepts

Terminal Node

  • (^) The node which is not having any sub-tree.
  • (^) It is also called as Leaf {D,H,F,I,J} H
G
I
F
D E
B
J
A
C

Basic Tree Concepts

Non-Terminal Node

  • (^) The node which are having sub-trees {A,B,C,E,G} H
G
I
F
D E
B
J
A
C

Basic Tree Concepts

Level

  • (^) The entire tree structure is leveled in such a way that the root node is always at level 0 {A} - Level 0 {B,C} - Level 1 {D,E,F,G} - Level 2 {H,I,J} - Level 3 H
G
I
F
D E
B
J
A
C

Basic Tree Concepts

Edge

  • (^) The line drawn from one node to another node is called edge B
A
C

EDGE

Basic Tree Concepts

Depth

  • (^) The maximum level of any node is called depth
  • (^) i.e. The longest path in the tree  (^) LEVEL 3 H
G
I
F
D E
B
J
A
C

Basic Tree Concepts

Forest

  • (^) It is a set of disjoint trees
  • (^) If the root is removed, then any tree becomes a forest H
G
I
F
D E
B
J
C

Binary Search Tree

  • (^) A binary search tree is a special binary tree, which may be empty or not
  • (^) It should have the following characteristics  (^) Every node will have a value  (^) No node can have same value  (^) Value (left) < value (parent)  (^) Value (right) > value (parent)

Operations

  1. Node Insertion
  2. Node Deletion
  3. Searching a Node
  4. Traversing