01Trees
01Trees
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
●
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
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
D E
F G
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
If P is the parent, then the lef and right child are at the
position 2P+1 and 2P+2 respectively.
LINKED REPRESENTATION
●
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
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
B C
D E F
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