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

Binary Trees and Multitrees Final Topic 2

The document provides an overview of tree data structures, explaining their definitions, properties, and various types, including binary trees and their specific classifications. It highlights the advantages of trees over linear data structures in terms of data access efficiency. Additionally, it describes key terminologies related to trees, such as nodes, edges, and traversal methods.

Uploaded by

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

Binary Trees and Multitrees Final Topic 2

The document provides an overview of tree data structures, explaining their definitions, properties, and various types, including binary trees and their specific classifications. It highlights the advantages of trees over linear data structures in terms of data access efficiency. Additionally, it describes key terminologies related to trees, such as nodes, edges, and traversal methods.

Uploaded by

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

Binary Trees

Multiway Trees
What is a TREE?
What is a TREE?
Tree represents the nodes connected by
edges.

A tree is where each node may have zero or


more children (a binary tree is a specialized
case of a general tree). Trees are used to
model applications such as file systems.

A tree is a nonlinear hierarchical data structure


that consists of nodes connected by edges.
Why Tree Data Structure?
Other data structures such as arrays, linked list,
stack, and queue are linear data structures that
store data sequentially. In order 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 of a Tree
The height of a Tree is the height of the root node or the
depth of the deepest node.
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.
Parent Node: The node which is a predecessor
of a node is called the parent node of that node.
{B} is the parent node of {D, E}.

Child Node: The node which is the immediate


successor of a node is called the child node of
that node. Examples: {D, E} are the child nodes
of {B}.
Root Node: The topmost node of a tree or the
node which does not have any parent node is
called the root node. {A} is the root node of the
tree. A non-empty tree must contain exactly one
root node and exactly one path from the root to
all other nodes of the tree.

Leaf Node or External Node: The nodes which


do not have any child nodes are called leaf
nodes. {K, L, M, N, O, P} are the leaf nodes of
the tree.
Ancestor of a Node: Any predecessor nodes on the
path of the root to that node are called Ancestors of
that node. {A,B} are the ancestor nodes of the node
{E}

Descendant: Any successor node on the path from


the leaf node to that node. {E,I} are the descendants
of the node {B}.

Sibling: Children of the same parent node are called


siblings. {D,E} are called siblings.
Properties of a Tree
1. Number of edges: An edge can be defined as the
connection between two nodes. If a tree has N
nodes then it will have (N-1) edges. There is only
one path from each node to any other node of the
tree.

2. Depth of a node: The depth of a node is defined as


the length of the path from the root to that node.
Each edge adds 1 unit of length to the path. So, it
can also be defined as the number of edges in the
path from the root of the tree to the node.
3. Height of a node: The height of a node can be defined as
the length of the longest path from the node to a leaf node
of the tree.

4. Height of the Tree: The height of a tree is the length of


the longest path from the root of the tree to a leaf node of
the tree.

5. Degree of a Node: The total count of subtrees attached


to that node is called the degree of the node. The degree of
a leaf node must be 0. The degree of a tree is the
maximum degree of a node among all the nodes in the
tree.
Binary Tree
A binary tree is a tree data structure in which each parent
node can have at most two children. Each node of a binary
tree consists of three items:

data item

address of left child

address of right child


Types of Binary Tree

1. Full Binary Tree


A full Binary tree is a special type of binary
tree in which every parent node/internal
node has either two or no children.
2. Perfect Binary Tree
A perfect binary tree is a type of binary tree
in which every internal node has exactly two
child nodes and all the leaf nodes are at the
same level.
3. Complete Binary Tree

A complete binary tree is just like a full binary


tree, but with two major differences

Every level must be completely filled


All the leaf elements must lean towards the
left.

The last leaf element might not have a right


sibling i.e. a complete binary tree doesn't have
to be a full binary tree.
4. Degenerate or Pathological Tree

A degenerate or pathological tree is the tree


having a single child either left or right.
5. Skewed Binary Tree

A skewed binary tree is a


pathological/degenerate tree in which the tree
is either dominated by the left nodes or the
right nodes. Thus, there are two types of
skewed binary tree: left-skewed binary tree
and right-skewed binary tree.
6. Balanced Binary Tree

It is a type of binary tree in which the difference


between the height of the left and the right
subtree for each node is either 0 or 1.
Binary Tree Representation

A node of a binary tree is represented by a


structure containing a data part and two
pointers to other structures of the same type.

struct node
{
int data;
struct node *left;
struct node *right;
};
Tree Traversals (Inorder, Preorder and Postorder)

Unlike linear data


structures (Array,
Linked List, Queues,
Stacks, etc) which have
only one logical way to
traverse them, trees
can be traversed in
different ways. The
following are the
generally used methods
for traversing trees:
Postorder Traversal

1. Traverse the left


subtree, i.e., call
Postorder(left-
>subtree)
2. Traverse the right
subtree, i.e., call
Postorder(right-
>subtree)
3. Visit the root
Preorder Traversal

1. Visit the root.


2. Traverse the left
subtree, i.e., call
Preorder(left-
>subtree)
3. Traverse the right
subtree, i.e., call
Preorder(right-
>subtree)

You might also like