0% found this document useful (0 votes)
4 views

Tree_Data_Structures

Uploaded by

Lito Jose
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Tree_Data_Structures

Uploaded by

Lito Jose
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

BSIS 2

Data
Structure
And
Algorithms
- Trees Data Structure

Prepared by:

Jay-Jay Lim Sapotalo


College Instructor
Why Tree Data Structure?
Other data structures such as arrays, linked list, stack, and queue are linear
data structures that store data sequentially. To perform any operation in a
linear data structure, the time complexity increases with the increase in the
data size. But it is not acceptable in today's computational world.

Different tree data structures allow quicker and easier access to the data as
it is a non-linear data structure.

Tree Terminologies

Node
A node is an entity that contains a key or value and pointers to its child
nodes.
The last nodes of each path are called leaf nodes or external nodes that do
not contain a link/pointer to child nodes.
The node having at least a child node is called an internal node.
Edge
It is the link between any two nodes.

Root
It is the topmost node of a tree.

Height of a Node
The height of a node is the number of edges from the node to the deepest
leaf (ie. the longest path from the node to a leaf node).

Depth of a Node
The depth of a node is the number of edges from the root to the node.

Height of a Tree
The height of a Tree is the height of the root node or the depth of the
deepest node.
Height and depth of each node in a tree

Degree of a Node
The degree of a node is the total number of branches of that node.
Forest
A collection of disjoint trees is called a forest.

Creating forest from a tree


You can create a forest by cutting the root of a tree.
Tree Data Structure and Its Variants

1. Tree Data Structure:


- A tree is a hierarchical data structure consisting of nodes.
- The topmost node is called the root, and each node can have zero or more child nodes.
- Nodes without children are called leaf nodes, and nodes with children are called internal
nodes.
- Connections between nodes are called edges.
- A tree is recursive in nature: each child of a node can itself be the root of a subtree.

Example Visualization:
A (Root)
/\
B C
/\ /\
D E F G (Leaves)

2. Tree Traversal:
Traversal refers to visiting all the nodes in a tree systematically.

a) Preorder Traversal (Root-Left-Right):


- Visit the root first, then traverse the left subtree, followed by the right subtree.
- Example Order: A, B, D, E, C, F, G

b) Inorder Traversal (Left-Root-Right):


- Traverse the left subtree first, visit the root, and then traverse the right subtree.
- Example Order: D, B, E, A, F, C, G

c) Postorder Traversal (Left-Right-Root):


- Traverse the left subtree, then the right subtree, and finally visit the root.
- Example Order: D, E, B, F, G, C, A

d) Level-order Traversal:
- Traverse nodes level by level, starting from the root.
- Example Order: A, B, C, D, E, F, G

3. Binary Tree:
- A binary tree is a tree where each node can have at most two children.
- The two children are usually referred to as the left child and right child.

Example Visualization:
A
/\
B C
/\ /\
D EF G

4. Full Binary Tree:


- A binary tree is full if every node has either 0 or 2 children.
- No node has only one child.

Example Visualization:
A
/\
B C
/\
D E

5. Perfect Binary Tree:


- A binary tree is perfect if all internal nodes have exactly two children and all leaf nodes
are at the same level.
- It is both full and complete.

Example Visualization:
A
/\
B C
/\ /\
D EF G

6. Complete Binary Tree:


- A binary tree is complete if:
A. All levels, except possibly the last, are completely filled.
B. Nodes at the last level are filled from left to right, with no gaps.

Example Visualization:
A
/\
B C
/\ /
D EF

7. Balanced Binary Tree:


- A binary tree is balanced if the height difference between the left and right subtrees of
any node is at most 1.
- This ensures efficient operations like searching, inserting, or deleting.

Example Visualization:
Balanced:
A
/\
B C
/\ /
D EF

Not Balanced:
A
/
B
/
C

You might also like