You are on page 1of 12

Lovely Professional University, Punjab

Data Structures
Lecture: Tree
Contents
• Introduction
• Binary Tree
• Basic Terminology
• Complete Binary Tree
• Extended Binary Tree
• Traversing Binary Tree
•Pre-order
• In-order
• Post-order
Introduction
• Trees are non-linear data structures.

• Used to represent a hierarchical relationship


between the elements.
Binary Tree
• A Binary Tree T is defined as a finite set of elements,
called nodes, such that:

(a) T is empty (called the null tree or empty tree), or


(b) T contains a distinguished node R, called the root
of T, and the remaining nodes of T form an
ordered pair of disjoint binary trees T1 and T2.

• T1 and T2 are called left sub-tree and right subtrees of R.


Binary Tree
• T1 and T2 are called left sub-tree and right subtrees of R.

• If T1 is nonempty, then its root is called the left successor


of R; similarly, if T2 is nonempty, then its root is called the
right successor of R.
S. Basis of
BINARY TREE BINARY SEARCH TREE
No. Comparison
BINARY SEARCH TREE is a node based binary tree that
BINARY TREE is a nonlinear data structure where
1. Definition further has right and left subtree that too are binary
each node can have at most two child nodes.
search tree.

•Full binary tree •AVL tree


2. Types •Complete binary tree •Splay Tree
•Extended Binary tree and more •T-trees and more

In BINARY SEARCH TREE the left subtree has elements


In BINARY TREE there is no ordering in terms of
3. Structure less than the nodes element and the right subtree has
how the nodes are arranged
elements greater than the nodes element.

Data
Data Representation is carried out in a hierarchical Data Representation is carried out in the ordered
4. Representati
format. format.
on
Duplicate
5. Binary trees allow duplicate values. Binary Search Tree does not allow duplicate values.
Values

The speed of deletion, insertion, and searching Because the Binary Search Tree has ordered
6. Speed operations in Binary Tree is slower as compared to properties, it conducts element deletion, insertion,
Binary Search Tree because it is unordered. and searching faster.

7. Complexity Time complexity is usually O(n). Time complexity is usually O(logn).


It is used for retrieval of fast and quick information It works well at element deletion, insertion, and
8. Application
and data lookup. searching.

It is utilized in the implementation of Balanced Binary


It serves as the foundation for implementing Full
9. Usage Search Trees such as AVL Trees, Red Black Trees, and
Binary Tree, BSTs, Perfect Binary Tree, and others.
so on.
Basic Terminology
• Terminal Nodes: The node with no successor is called
terminal node or leaf.

• Similar Trees: Two binary trees T1 and T2 are said to be


similar if they have the same shape or structure.

• Copy of Trees: Two binary trees T1 and T2 are said to be


copies if they are similar and if they have the same contents at
corresponding nodes.
Basic Terminology
• Parent:
• Left Child:
• Right Child:
• Siblings:
• Level:

• A line drawn from the node N to its successor is called edge.


• The sequence of consecutive edges is called Path.
• Height or Depth of a Tree is the maximum number of nodes in a
branch of T.
Height = Largest Level + 1
Complete Binary Tree
• A Binary tree T is said to be complete if all its levels,
except possibly the last, have the maximum number of
possible nodes and all the nodes at the last level appear
as far left as possible.

• The depth of a complete Binary Tree T is:


D = floor value (log2n + 1)
Extended Binary Tree
• A Binary tree T is said to be extended binary tree or 2-
Tree or Full Binary Tree if each node N has either 0 or 2
children.

• Nodes with 0 child are called external nodes.

• Nodes with 2 children are called internal nodes.


Perfect Binary Tree
• A Binary tree is Perfect Binary Tree in which all
internal nodes have two children and all leaves are at
same level.
Questions

You might also like