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

01Trees

Uploaded by

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

01Trees

Uploaded by

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

TREES

TREES


DEFINITION

TERMINOLOGIES

BINARY TREE REPRESENTATIONS

TREE TRAVERSALS

BINARY SEARCH TREE

THREADED BINARY TREE

EXPRESSION TREES
TREES

DEFINITION :


A tree is a data structure that represents
hierarchical relationships between individual data
items.

A tree is non-primitive, non-linear data structure.
Trees

B C D

E F G H I
Trees


Consists of a finite set of elements called nodes, and
a finite set of lines called branches or edges, that
connects the nodes.

The number of branches associated with a node is
the degree of the node.

The number of branches (edges) leaving a node is
called an outdegree of that node.

The number of branches (edges) entering a node is
called an indegree of that node.
Binary Tree


A binary tree is a finite set of elements that is either
empty or is partitioned into three disjoint subsets:
- The first subset contains a single element called
the ROOT of the tree.
- The other two subsets are themselves binary
trees, called the LEFT and RIGHT subtrees of the
original tree.

NODE : Each element of a binary tree is called a node
of the tree.
Binary Tree

B E

C D F

Left subtree Right subtree


TERMINOLOGIES


Parent: A node is a parent if it has successor nodes i.e, if it
has an outdegree greater than zero.

Child: A node with a predecessor is a child. A child node
has an indegree of one.

Root: A node with no predecessor is called as a root node.

Leaf/Terminal Node/External Node: A leaf is any node with
an outdegree of zero, i.e, a node with no successors.

A Node that has no child is called a leaf node.
TERMINOLOGIES


Ancestor: Node n1 is an ancestor of node n2 if n1 is
either the parent of n2 or parent of some ancestor of n2.

Descendants: The nodes in the path below the parent .

Lef Descendant: Node n2 is a lef descendant of node
n1 if n2 is either the lef child of n1 or a descendant of
lef child of n1.

Right Descendant: Node n2 is a right descendant of node
n1 if n2 is either the right child of n1 or a descendant of
right child of n1.
Siblings


Siblings : Two or more nodes having the same
parent.
Ex: A

B C

Here, A is the Parent of B and C, B and C are children


of A, B and C are siblings.
Level

LEVEL 0
A

LEVEL 1 B C

LEVEL 2 D E F

G
LEVEL 3 H I
TERMINOLOGIES


Level of a node: It is the distance of a node from the
root.

The root of a tree has level 0(zero)

Level of any other node in the tree is one more
than level of its parent.

Depth /Height of a Binary tree: The maximum level
of any leaf in the tree.
TYPES OF BINARY TREES


Strictly Binary tree

Complete Binary tree

Almost complete binary tree

Binary Search tree

Expression tree
● Threaded Binary tree

Balanced Binary tree
Strictly Binary tree

If every non-leaf node in a binary tree has non empty


lef and right sub trees, the tree is termed as strictly
binary tree.
A
C
B

D E

F G
Complete Binary tree

Complete binary tree of depth 3.

A completely binary tree of depth d is the stric


binary tree, all of whose leaves are at level d.
Almost Complete Binary Tree

A binary tree of depth d is an almost complete binary tree if all its levels
are full except possibly the last level, where only some rightmost leaves
may be missing. Here every leaf in the tree is either at level d or at level
d – 1.
Balanced Binary Tree

A binary tree is called balanced binary tree if the depths of the subtrees of
all its nodes do not differ by more than 1.
Properties of Trees

1. If a binary tree contains m nodes at level L , then it contains


atmost 2m nodes at level L+1.
2. All levels contain exactly 2L nodes at level L.

3. A strictly binary tree with n leaves always contain 2n-1 nodes.


4. The total number of nodes in a complete binary tree of depth
d contains 2(d+1) -1
5. In a complete binary tree of depth d, there are 2d leaf nodes
and 2d -1 nonleaf nodes.
Array Representation

If P is the parent, then the lef and right child are at the
position 2P+1 and 2P+2 respectively.
LINKED REPRESENTATION

Linked List Representation:


In linked representation of trees, each node N of the tree will contain three fields:
1. Info 2. Lef child 3. Right child
lchild info rchild

Logical Representation of the tree :


struct treenode
{
int data;
struct treenode *rchild;
struct treenode *lchild;
};
LINKED REPRESENTATION
TREE TRAVERSALS


Traversing is a method of visiting each node of a
tree exactly once in a some order.


Three Methods for Binary Tree traversal:
– Inorder Traversal
– Preorder Traversal
– Postorder Traversal
Inorder Traversal

Inorder Traversal (Symmetric order) :


To traverse a non empty binary tree in inorder :
1. Traverse the lef subtree in inorder
2. Visit the root
3. Traverse the right subtree in inorder.
Preorder Traversal

Preorder Traversal :
To traverse a non empty binary tree in Preorder :
1. Visit the root
2. Traverse the lef subtree in Preorder
3. Traverse the right subtree in Preorder.
Postorder Traversal

Postorder Traversal :
To traverse a non empty binary tree in postorder :
1. Traverse the lef subtree in postorder
2. Traverse the right subtree in postorder.
3. Visit the root
Problem

Inorder: ECFAGDH
A
Postorder: EFCGHDA
Obtain the Binary Tree C D

E F G H
Threaded Binary Trees

A A

B C B C

D E F D E F

G G
A binary tree is threaded based on the traversal technique.
In-Threaded Binary Tree
Inorder Traversal
D B A E G C F
Right In-Threaded Binary Tree

B C

D E F

If the right link of a node is NULL and if it is replaced


by the address of the inorder successor then the tree
is said to be Right in-threaded binary tree.
Inorder Traversal D B A E G C F
Lef in-threaded binary tree.

B C

D E F

If the lef link of a node is NULL and if it is replaced by


the address of the inorder predecessor, then the tree
is said to be Lef in-threaded binary tree.
Inorder Traversal D B A E G C F
RIGHT PRE-THREADED BINARY TREE

B C

D E F

G
If the right link of a node is NULL and if it is replaced
by the address of the preorder successor then the
tree is said to be Right Pre-threaded binary tree.
Preorder : ABDCEGF
LEFT PRE-THREADED BINARY TREE

B C

D E F

If the lef link of a node is NULL and if it is replaced by


the address of the preorder predecessor, then the
tree is said to be Lef Pre-threaded binary tree.
Preorder : ABDCEGF

You might also like