0% found this document useful (0 votes)
74 views33 pages

Understanding Binary Trees and Their Types

Binary trees are fundamental data structures that combine advantages of ordered arrays and linked lists by allowing fast searching similar to arrays and fast insertion/deletion similar to lists. They consist of nodes connected by edges to represent relationships. Trees have properties like paths, root, parent and child nodes, subtrees, and can be traversed in preorder, inorder and postorder manners. Binary trees restrict each node to have at most two children. They can be represented sequentially or via links and have applications in compilers, databases, file systems and more.

Uploaded by

Ramanpreet Kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Topics covered

  • Levels,
  • Information Storage,
  • Applications of Trees,
  • Keys,
  • Height of Binary Tree,
  • Traversing,
  • In-degree,
  • Extended Binary Tree,
  • Node,
  • Postorder Traversal
0% found this document useful (0 votes)
74 views33 pages

Understanding Binary Trees and Their Types

Binary trees are fundamental data structures that combine advantages of ordered arrays and linked lists by allowing fast searching similar to arrays and fast insertion/deletion similar to lists. They consist of nodes connected by edges to represent relationships. Trees have properties like paths, root, parent and child nodes, subtrees, and can be traversed in preorder, inorder and postorder manners. Binary trees restrict each node to have at most two children. They can be represented sequentially or via links and have applications in compilers, databases, file systems and more.

Uploaded by

Ramanpreet Kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Topics covered

  • Levels,
  • Information Storage,
  • Applications of Trees,
  • Keys,
  • Height of Binary Tree,
  • Traversing,
  • In-degree,
  • Extended Binary Tree,
  • Node,
  • Postorder Traversal

Binary Trees

Unit-5
Introduction to Tree

• Fundamental data storage structures used


in programming.
• Combines advantages of an ordered array
and a linked list.
• Searching as fast as in ordered array.
• Insertion and deletion as fast as in linked
list.
Tree (example)

node

d ge
e

Draw a parse tree


Tree characteristics

• Consists of nodes connected by edges.


• Nodes often represent entities (complex
objects) such as people, car parts etc.
• Edges between the nodes represent the way
the nodes are related.
• Its easy for a program to get from one node
to another if there is a line connecting them.
• The only way to get from node to node is to
follow a path along the edges.
Tree Terminology

• Path: Traversal from node to node along the edges results


in a sequence called path.
• Root: Node at the top of the tree.
• Parent: Any node, except root has exactly one edge running
upward to another node. The node above it is called parent.
• Child: Any node may have one or more lines running
downward to other nodes. Nodes below are children.
• Leaf: A node that has no children.
• Subtree: Any node can be considered to be the root of a
subtree, which consists of its children and its children’s
children and so on.
Tree Terminology

• Visiting: A node is visited when program control


arrives at the node, usually for processing.
• Traversing: To traverse a tree means to visit all
the nodes in some specified order.
• Levels: The level of a particular node refers to
how many generations the node is from the root.
Root is assumed to be level 0.
• Keys: Key value is used to search for the item or
perform other operations on it.
Binary-Tree
Binary Trees

• Every node in a binary tree


can have at most two
children.
• The two children of each
node are called the left child
and right child corresponding
to their positions.
• A node can have only a left
child or only a right child or it
can have no children at all.
• Left child is always less that
its parent, while right child is
greater than its parent.
Unbalanced Trees

• Some trees can be unbalanced.


• They have most of their nodes on one side of the
root or the other. Individual subtrees may also be
unbalanced.
• Trees become unbalanced because of the order
in which the data items are inserted.
• If the key values are inserted in ascending or
descending order the tree will be unbalanced.
• For search-centric application (Binary tree), an
unbalanced tree must be re-balanced.
Complete Binary tree

A full binary tree (sometimes proper binary tree or


2-tree) is a tree in which every node other than
the leaves has two children.

A complete binary tree is a binary tree in which


every level, except possibly the last, is
completely filled, and all nodes are as far left as
possible.
Complete Binary tree

Complete binary tree


Representing Binary Tree in
memory

Let T be a Binary Tree. There are two ways of


representing T in the memory as follow :

1. Sequential Representation of Binary Tree.


2. Link Representation of Binary Tree.
Linked Representation of
Binary Tree

Consider a Binary Tree T. T will be maintained in memory


by means of a linked list representation which uses three
parallel arrays; INFO, LEFT, and RIGHT pointer variable
ROOT as follows. In Binary Tree each node N of T will
correspond to a location k such that

• LEFT [k] contains the location of the left child of node N.


• INFO [k] contains the data at the node N.
• RIGHT [k] contains the location of right child of node N.
Representation of a node:

In this representation of binary tree root will contain the


location of the root R of T. If any one of the subtree is
empty, then the corresponding pointer will contain the
null value if the tree T itself is empty, the ROOT will
contain the null value.
Sequential representation of
Binary Tree

• Let us consider that we have a tree T. let our tree T is a


binary tree that us complete binary tree. Then there is an
efficient way of representing T in the memory called the
sequential representation or array representation of T.
This representation uses only a linear array TREE as
follows:
• The root N of T is stored in TREE [1].
• If a node occupies TREE [k] then its left child is stored in
TREE [2 * k] and its right child is stored into TREE [2 * k
+ 1].
Tree traversal

• Preorder(NLR)
• Inorder(LNR)
• Postorder(LRN)
Pre-order Traversal

To traverse a non-empty binary tree in pre-


order, the following operations are
performed recursively at each node. The
algorithm works by:
1. Visiting the root node,
2. Traversing the left sub-tree, and finally
3. Traversing the right sub-tree.
A. TRAVERSAL ORDER: A, B, D, G, H, L, E, C, F, I, J,and K

B. TRAVERSAL ORDER: A, B, D, C, D, E, F, G, H, and I


In-order Traversal
To traverse a non-empty binary tree in in-order,
the following operations are performed
recursively at each node. The algorithm works
by:
1. Traversing the left sub-tree,
2. Visiting the root node, and finally
3. Traversing the right sub-tree.
A. TRAVERSAL ORDER: G, D, H, L, B, E, A, C, I, F, K, and
J

B. TRAVERSAL ORDER: B, D, A, E, H, G, I, F, and C


Post-order Traversal
To traverse a non-empty binary tree in post-
order, the following operations are performed
recursively at each node. The algorithm works
by:
1. Traversing the left sub-tree,
2. Traversing the right sub-tree, and finally
3. Visiting the root node.
A. TRAVERSAL ORDER: G, L, H, D, E, B, I, K, J, F, C, and A

B. TRAVERSAL ORDER: D, B, H, I, G, F, E, C, and A


Given an expression, Exp = ((a + b) – (c * d)) % ((e ^f) / (g – h)), construct
the corresponding binary tree
Constructing a Binary Tree
from Traversal Results

•We can construct a binary tree if we are given at


least two traversal results.
•The first traversal must be the in-order traversal and
the second can be either pre-order or post-order
traversal.
•The in-order traversal result will be used to
determine the left and the right child nodes, and the
•pre-order/post-order can be used to determine the
root node.
Step 1 Use the pre-order sequence to determine the root
node of the tree. The first element would be the root node.

Step 2 Elements on the left side of the root node in the


in-order traversal sequence form the left sub-tree of the
root node. Similarly, elements on the right side of the root
node in the in-order traversal sequence form the right sub-
tree of the root node.

Step 3 Recursively select each element from pre-order


traversal sequence and create its left and
right sub-trees from the in-order traversal sequence
In–order Traversal: D B E A F C G
Pre–order Traversal: A B D E C F G
In–order Traversal: D B H E I A F J C G
Post order Traversal: D H I E B J F G C A
APPLICATIONs OF TREES

1. Trees are used to store simple as well as complex data. Here


simple means an integer value, character value and complex
data means a structure or a record.
2. Trees are often used for implementing other types of data
structures like hash tables, sets, and maps.
3. Trees are an important data structure used for compiler
construction.
4. Trees are also used in database design.
5. Trees are used in file system directories.
6. Trees are also widely used for information storage and retrieval
in symbol tables.
Important facts

A binary tree of n nodes has exactly n – 1 edges. The depth


of a node N is given as the length of the path from the root R
to the node N. The depth of the root node is zero.

A binary tree of height h has at least h nodes and at most


(2^h) – 1 nodes.

The height of a binary tree with n nodes is at least log2(n+1)


and at most n. In-degree of a node is the number of edges
arriving at that node. The root node is the only node that has
an in-degree equal to zero. Similarly, out-degree of a node is
the number of edges leaving that node
In a complete binary tree, every level (except possibly the
last) is completely filled and nodes appear as far left as
possibly.
A binary tree T is said to be an extended binary tree(or a 2-
tree) if each node in the tree has either no children or exactly
two children.

You might also like