Lecture 6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Graph Theory

Dr. Irfan Yousuf


Department of Computer Science (New Campus)
UET, Lahore
(Lecture # 6: February 03, 2023 )
Outline
• Binary Trees
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:
Binary Tree Types
1. Complete Binary Tree
2. Full Binary Tree
3. Skewed Binary Tree
4. Perfect Binary Tree
5. Expression Binary tree
Complete Binary Tree
• A complete binary tree is a tree in which
• All leaf nodes are at n or n-1 level
• Levels are filled from left to right
Full Binary Tree (Proper Binary Tree)

A full binary tree is a tree in which every node other than


the leaves has two children or no children. Every level is
completely filled up.
No of nodes= 2h+1 -1
• The number of leaves is = internal nodes +1
• The total number of nodes is = 2*internal nodes + 1
• The total number of nodes= 2h+1 -1
• The number of leaves is = (n+1)/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.
• A perfect binary tree with n nodes has height
= log(n+1)-1

• A perfect binary tree of height h has 2h leaf nodes


Skewed Binary Tree

• A binary tree is said to be Skewed Binary Tree if every node


in the tree contains either only left or only right sub tree.
• If the node contains only left sub tree then it is called left-
skewed binary tree and if the tree contains only right sub tree
then it is called right-skewed binary tree.
Expression Binary tree

• Expression trees are a special kind of binary tree used to


evaluate certain expressions.
• Two common types of expressions that a binary expression
tree can represent are algebraic and boolean.
• These trees can represent expressions that contain both unary
and binary operators.
• The leaves of a binary expression tree are operands, such as
constants or variable names, and the other nodes contain
operators.
• Expression tree are used in most compilers.
Preorder Tree Traversal

In Preorder traversal method first process root element, then


left sub tree and then right sub tree.
Procedure:-
Step 1: Visit root node
Step 2: Visit left sub tree in preorder
Step 3: Visit right sub tree in preorder
Preorder Tree Traversal
Inorder Tree Traversal

In Inorder traversal method first process left element, then


root element and then the right element.
Procedure:-
Step 1: Visit left sub tree in inorder
Step 2: Visit root node
Step 3: Visit right sub tree in inorder
Inorder Tree Traversal
Postorder Tree Traversal

In Postorder traversal first visit left sub tree, then right sub tree and then the
root element.
Procedure:-
Step 1: Visit left sub tree in postorder
Step 2: Visit right sub tree in postorder
Step 3: Visit root node
Postorder Tree Traversal
Binary Search Tree
A binary search tree (BST) or "ordered binary tree" is a
tree in which each node contains a key that satisfies
following conditions:
1. All keys are distinct.’
2. all elements in its left subtree are less to the node
(<), and all the elements in its right subtree are
greater than the node (>).
Binary search tree
Binary search tree property
For every node X
All the keys in its left
subtree are smaller than
the key value in X
All the keys in its right
subtree are larger than the
key value in X
Binary Search Trees

A binary search tree Not a binary search tree


Binary Search Trees
Binary Search Trees
Summary

Binary Tree

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy