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

Trees

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

Trees

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

TREES

TREE
• is a hierarchical data structure composed of nodes
connected by edges.
• It is widely used to represent hierarchical relationships
and is fundamental in various algorithms and applications.
• Trees are characterized by their branching structure,
starting from a root node and extending downwards to
child nodes, which may further branch into more nodes.
TREE

• In computer science, a tree is a widely used hierarchical data


structure that consists of nodes connected by edges.
• It is named "tree" due to its resemblance to a tree in nature, with the
following key properties:
• 1. Nodes : Each node in a tree contains data and may have
references (links or pointers) to other nodes, known as its children.
• 2. Edges : Connections between nodes that define the relationships
and hierarchy within the tree structure.
• 3. Root : The topmost node of the tree, which has no parent. It
serves as the starting point for traversing or accessing other nodes in
the tree.
• 4. Parent and Child Nodes : Nodes in a tree are connected in a
parent-child relationship. A parent node has references to one or more
child nodes, while child nodes have a reference back to their parent.
• 5. Leaf Nodes : Nodes that do not have any children are called
leaf nodes or terminal nodes. They are at the ends of branches in
the tree structure.

• 6. Internal Nodes : Nodes that have at least one child are called
internal nodes. They are neither the root nor leaf nodes.

• 7. Path : A sequence of edges that connect nodes in a tree from


a starting node to a destination node.
Example of a Tree:

• Consider a family tree:

• - Root Node : Grandparents (Paternal Grandfather and


Paternal Grandmother).
• - Parent Nodes : Parents (Father and Mother).

• - Child Nodes : Children (Son and Daughter).

• - Leaf Nodes : Grandchildren (Grandson and


Granddaughter).
Visual Representation:

Grandfather & Grandmother (Root)


/ \
Father Mother
/ \
Son Daughter
/ \ / \
Grandson Granddaughter Grandson Granddaughter
Applications of Trees:

• Trees are used in various applications in computer science and beyond:


• - Data Structures : Implementations like binary trees, AVL trees, and B-
trees for efficient data storage and retrieval.
• - Algorithms : Tree traversal algorithms (e.g., depth-first search, breadth-
first search) for searching and sorting.
•- Hierarchical Data Representation : Representing organizational
structures, file systems, and database indexing.
• Understanding trees and their properties is
fundamental in computer science for:
• designing efficient algorithms
• organizing data hierarchically
• solving complex problems that involve
hierarchical relationships and nested structures.
Binary Trees – Representation

• In computer science, a binary tree is a hierarchical data


structure where each node has at most two children,
referred to as the left child and the right child.
Representing a binary tree involves defining a structure that
efficiently stores and manages nodes and their relationships.
Representation Techniques:

1. Node Structure :
- Each node in a binary tree typically contains:
- Data (value stored in the node)
- References or pointers to its left and right
children (or null if the child does not exist).
2. BinaryTreeNode Class (Example in C ):
public class BinaryTreeNode
{
public int Data; // Data stored in the node
public BinaryTreeNode Left; // Pointer to the left child node
public BinaryTreeNode Right; // Pointer to the right child node

public BinaryTreeNode(int data)


{
Data = data;
Left = null;
Right = null;
}
3. Root Node :
- The topmost node of the binary tree is called
the root node. It serves as the starting point for
accessing other nodes in the tree.
4. Example Representation :
- Consider a simple binary tree with nodes containing integers:

1
/\
2 3
/\
4 5
In this tree:
- Node 1 is the root node.
- Node 2 and Node 3 are children of Node 1.
- Node 4 and Node 5 are children of Node 2.
Key Points:

- Binary Tree : A tree where each node can have at most two children.

- Node Structure : Contains data and pointers to left and right children.

- Representation : Efficiently manages relationships between nodes using pointers


or references.
•Understanding the representation of binary trees is crucial for implementing
tree operations such as insertion, deletion, and traversal, which are fundamental
in algorithm design and data structure optimization.
Properties of Binary Tree

• Binary trees are fundamental data structures in


computer science, characterized by their
hierarchical organization and specific properties
that define their structure and behavior.
Understanding these properties is crucial for
efficient manipulation and traversal of binary
trees.
Key Properties:

1. Root Node :

- The topmost node of a binary tree is called the root node. It serves as the
starting point for accessing all other nodes in the tree.

2. Parent and Child Nodes :

- Each node in a binary tree can have at most two children:

- Left Child : Node to the left of a parent node.

- Right Child : Node to the right of a parent node.

- Nodes that have no children are referred to as leaf nodes.


• 3. Height of a Binary Tree :
• - The height of a binary tree is the number of edges on
the longest path from the root node to a leaf node.
• - It indicates the maximum depth of the tree and is
essential for analyzing the performance of tree
operations.
4. Depth (Level) of a Node :

- The depth (or level) of a node is the number of edges from the root node to that
node.

- The root node is at level 0, its children are at level 1, and so on.

5. Full Binary Tree :

- A binary tree in which every node other than the leaves has two children.

- All leaf nodes are at the same level.


6. Complete Binary Tree :
- A binary tree in which all levels are completely
filled, except possibly for the last level, which is filled
from left to right.
- Useful in heap data structures.
7. Perfect Binary Tree :
- A binary tree where all internal nodes have exactly
two children and all leaf nodes are at the same level.
- The number of nodes at each level doubles as you
move down the tree.
• 8. Balanced Binary Tree :
• - A binary tree is considered balanced if the height difference
between the left and right subtrees of every node is no more
than 1.
• - Ensures efficient operations (searching, inserting, deleting)
with average time complexity of O(log n).
Types of Binary Tree Insertion in Binary Tree - Level Order
Insertion

• Types of Binary Trees


Binary trees can vary in structure and properties based on
how nodes are organized and connected. Here are some common
types of binary trees:
• 1. Full Binary Tree :
• - Every node other than the leaf nodes has exactly two children.

• - All leaf nodes are at the same level.


- Example:
1
/\
2 3
/\
4 5
2. Complete Binary Tree :
- All levels are completely filled, except possibly the
last level, which is filled from left to right.
- Example:
1
/\
2 3
/\/
4 56
3. Perfect Binary Tree :
- All internal nodes have exactly two children.
- All leaf nodes are at the same level.
- Example:
1
/\
2 3
/\/\
4 56 7
4. Balanced Binary Tree :

- The height difference between the left and right subtrees of every node is no more than one.

- Ensures that no subtree is much deeper than any other subtree.

- Example:

/\

2 3

/\ \

4 5 6
5. Degenerate (or Pathological) Binary Tree :
- Each parent node has only one child (left or right).
- Essentially a linked list in structure.
- Example (skewed to the right):
1
\
2
\
3
\
4

You might also like