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

Unit-3: Non-Linear Data Structure

The document discusses different types of tree traversals including preorder, inorder, and postorder traversals. It defines each traversal method with steps for processing the root node and traversing left and right subtrees. Algorithms with recursive procedures are provided to perform each type of traversal on a binary tree. The document also describes how to construct a binary tree from given inorder and postorder traversals and discusses threaded binary trees which use threads instead of null pointers to link nodes traversed consecutively in a given order.

Uploaded by

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

Unit-3: Non-Linear Data Structure

The document discusses different types of tree traversals including preorder, inorder, and postorder traversals. It defines each traversal method with steps for processing the root node and traversing left and right subtrees. Algorithms with recursive procedures are provided to perform each type of traversal on a binary tree. The document also describes how to construct a binary tree from given inorder and postorder traversals and discusses threaded binary trees which use threads instead of null pointers to link nodes traversed consecutively in a given order.

Uploaded by

DHRUVIL BHANDERI
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 40

Data Structure

Unit-3
Non-Linear Data
Structure
Tree By : Twisa Mehta
Tree Traversal
 The most common operations performed on tree structure is that of traversal.
 This is a procedure by which each node in the tree is processed exactly once in a systematic
manner.
 There are three ways of traversing a binary tree. A

1. Preorder Traversal
2. Inorder Traversal B D
3. Postorder Traversal

C E G

F
Preorder Traversal
 Preorder traversal of a binary tree is defined as follow
1. Process the root node A ✓

2. Traverse the left subtree in preorder


3. Traverse the right subtree in preorder B ✓ D ✓

 If particular subtree is empty (i.e., node has no left or


C ✓ E ✓ G ✓
right descendant) the traversal is performed by doing
nothing.
F ✓
 In other words, a null subtree is considered to be fully
traversed when it is encountered. Preorder traversal of a given tree as

A B C D E F G
Inorder Traversal
 Inorder traversal of a binary tree is defined as follow
A ✓
1. Traverse the left subtree in Inorder
2. Process the root node
B ✓ D ✓
3. Traverse the right subtree in Inorder

C ✓ E ✓ G ✓

F ✓

Inorder traversal of a given tree as

C B A E F D G
Postorder Traversal
 Postorder traversal of a binary tree is defined as follow
A ✓
1. Traverse the left subtree in Postorder
2. Traverse the right subtree in Postorder
B ✓ D ✓
3. Process the root node

C ✓ E ✓ G ✓

F ✓

Postorder traversal of a given tree as


C B F E G D A
Converse Traversal
 If we interchange left and right words in the preceding definitions, we obtain three new
traversal orders which are called
 Converse Preorder Traversal: A D G E F B C
 Converse Inorder Traversal: G D F E A B C
 Converse Postorder Traversal: G F E D C B A
Write Pre/In/Post Order Traversal
1 50 15

2 3 25 75
3 1

6 22
4 22 40 60 80

5 45
5
15 30 90

23 65

34 78
Linked Representation of Binary Tree
T
LPTR DATA RPTR

Typical node of Binary Tree A

A
B D

B D

C E G
C E G

F
F
Algorithm of Binary Tree Traversal
 Preorder Traversal - Procedure: RPREORDER(T)
 Inorder Traversal - Procedure: RINORDER(T)
 Postorder Traversal - Procedure: RPOSTORDER(T)
Procedure: RPREORDER(T)
 This procedure traverses the tree in preorder, in a recursive manner.
 T is root node address of given binary tree LPTR DATA RPTR

 Node structure of binary tree is described as below Typical node of Binary Tree

1. [Check for Empty Tree] 3. [Process the Right Sub Tree]


IF T = NULL IF RPTR (T) ≠ NULL
THEN write (‘Empty Tree’) THEN RPREORDER (RPTR (T))
return 4. [Finished]
ELSE write (DATA(T)) Return
2. [Process the Left Sub Tree]
IF LPTR (T) ≠ NULL
THEN RPREORDER (LPTR (T))
Procedure: RINORDER(T)
 This procedure traverses the tree in InOrder, in a recursive manner.
 T is root node address of given binary tree. LPTR DATA RPTR

 Node structure of binary tree is described as below. Typical node of Binary Tree

1. [Check for Empty Tree] 4. [Process the Right Sub Tree]


IF T = NULL IF RPTR (T) ≠ NULL
THEN write (‘Empty Tree’) THEN RINORDER (RPTR (T))
return 5. [Finished]
2. [Process the Left Sub Tree] Return
IF LPTR (T) ≠ NULL
THEN RINORDER (LPTR (T))
3. [Process the Root Node]
write (DATA(T))
Procedure: RPOSTORDER(T)
 This procedure traverses the tree in PostOrder, in a recursive manner.
 T is root node address of given binary tree. LPTR DATA RPTR

 Node structure of binary tree is described as below. Typical node of Binary Tree

1. [Check for Empty Tree] 4. [Process the Root Node]


IF T = NULL write (DATA(T))
THEN write (‘Empty Tree’) 5. [Finished]
return Return
2. [Process the Left Sub Tree]
IF LPTR (T) ≠ NULL
THEN RPOSTORDER (LPTR (T))
3. [Process the Right Sub Tree]
IF RPTR (T) ≠ NULL
THEN RPOSTORDER (RPTR (T))
Construct Binary Tree from Traversal
Construct a Binary tree from the given Inorder and Postorder traversals
• Step 1: Find the root node
• Preoder Traversal – first node is root node
Inorder : D G B A H E I C F • Postoder Traversal last node is root node
Postorder : G D B H I E F C A • Step 2: Find Left & Right Sub Tree
• Inorder traversal gives Left and right sub tree

Postorder : G D B H I E F C AA
A A
Inorder : D G B AA H E I C F

B C B C
A

D,G H,E,I F D E F
D,G,B H,E,I,C,F

G H I
Construct Binary Tree from Traversal
Preorder :GG B Q A C K F P D E R H Inorder : Q B K C F AG G P E D H R
Root left right left Root right
G G G

B P
QBKCFA PED HR B P

D Q A D
Q A
G
C E R
KCF E HR
B P

K F H
Q KCFA ED HR
Linked Representation of Binary Tree
T
LPTR DATA RPTR

Typical node of Binary Tree A

A
B D

B D

C E G
C E G

F
F
Threaded Binary Tree
 The wasted NULL links in the binary tree storage representation can be replaced by threads
 A binary tree is threaded according to particular traversal order. e.g.: Threads for the
inorder traversals of tree are pointers to its higher nodes, for this traversal order
 In-Threaded Binary Tree
 If left link of node P is null, then this link is replaced by the address of its predecessor
 If right link of node P is null, then this link is replaced by the address of its successor

 Because the left or right link of a node can denote either structural link or a thread, we
must somehow be able to distinguish them
Threaded Binary Tree
 Method 1:- Represent thread a Negative address
 Method 2:- To have a separate Boolean flag for each of left and right pointers, node
structure for this is given below
LPTR LTHREAD DATA RTHREAD RPTR
Typical node of Threaded Binary Tree

• LTHREAD = true = Denotes leaf thread link


HEAD
• LTHREAD = false = Denotes leaf structural link
• RTHREAD = true = Denotes right threaded link
• RTHREAD = false = Denotes right structural link

Head node is simply another node which serves as the predecessor


and successor of first and last tree nodes.
Tree is attached to the left branch of the head node.
Threaded Binary Tree
HEAD
A

B D A

C E G
B D

C E G
Inorder Traversal

C B A E F D G
F

Fully In-Threaded Binary Tree


Threaded Binary Tree
Construct Right In-Threaded Binary Tree of given Tree
A
HEAD

B E

A
C D F H

B E
G

Inorder Traversal
C D F H
CBDAFGEH
G
Advantages of Threaded Binary Tree
 Inorder traversal is faster than unthreaded version as stack is not required.
 Effectively determines the predecessor and successor for inorder traversal, for unthreaded
tree this task is more difficult.
 A stack is required to provide upward pointing information in binary tree which threading
provides without stack.
 It is possible to generate successor or predecessor of any node without having over head of
stack with the help of threading.
Disadvantages of Threaded Binary Tree
 Threaded trees are unable to share common sub trees.
 If Negative addressing is not permitted in programming language, two additional fields are
required.
 Insertion into and deletion from threaded binary tree are more time consuming because
both thread and structural link must be maintained.
Binary Search Tree : property (keys(left) < key(root) < keys(right))
 A binary search tree is a binary tree in which each node possessed a key that satisfy the
following conditions
1. All key (if any) in the left sub tree of the root precedes the key in the root
2. The key in the root precedes all key (if any) in the right sub tree
3. The left and right sub trees of the root are again search trees
Construct Binary Search Tree (BST)
Construct binary search tree for the following data
50 , 25 , 75 , 22 , 40 , 60 , 80 , 90 , 15 , 30

50

25 75

22 40 60 80

15 30 90

Construct binary search tree for the following data


10, 3, 15, 22, 6, 45, 65, 23, 78, 34, 5
Search a node in Binary Search Tree
 To search for target value.
 We first compare it with the key at root of the tree.
 If it is not same, we go to either Left sub tree or Right sub tree as appropriate and repeat the
search in sub tree.
 If we have In-Order List & we want to search for specific node it requires O(n) time.
 In case of Binary tree it requires O(Log2n) time to search a node.
Delete node from Binary Search Tree

Delete node a

a
Delete from Leaf Node

a Delete node a b

Delete from Non Terminal (Empty Left Sub Tree)


Delete node from BST

Delete node a

a C

b C

Delete from Non Terminal (Neither Sub Tree is Empty)


Conversion of general tree into a binary tree
 The root of the Binary Tree is the Root of the Generic Tree.
 The left child of a node in the Generic Tree is the Left child of that node in the Binary Tree.
 The right sibling of any node in the Generic Tree is the Right child of that node in the Binary
Tree.

#3130702 (DS)  Unit 3 – Non-Linear Data Structure (Tree


Dr. Pradyumansinh U. Jadeja 27
Generic tree

The root of the Binary Tree is the Root of the Generic Tree.
The left child of a node in the Generic Tree is the Left child of that
node in the Binary Tree.
The right sibling of any node in the Generic Tree is the Right child
of that node in the Binary Tree.

#3130702 (DS)  Unit 3 – Non-Linear Data Structure (Tree


Dr. Pradyumansinh U. Jadeja 28
Heap
 A Heap is a special Tree-based data structure in which the tree is a complete binary tree.
 Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys
present at all of it’s children. The same property must be recursively true for all sub-trees in
that Binary Tree.
 Min-Heap: In a Min-Heap the key present at the root node must be minimum among the
keys present at all of it’s children. The same property must be recursively true for all sub-
trees in that Binary Tree.

#3130702 (DS)  Unit 3 – Non-Linear Data Structure (Tree


Dr. Pradyumansinh U. Jadeja 29
Applications of tree :
 Trees are used to store information that naturally forms a hierarchy. For example, the file
system on a computer : file system.
 If we organize keys in form of a tree (with some ordering e.g., BST), we can search for a given
key in moderate time.
 Insert/delete keys in moderate time (quicker than Arrays and slower than Unordered Linked
Lists). 
 Like Linked Lists and unlike Arrays, Pointer implementation of trees don’t have an upper limit
on number of nodes as nodes are linked using pointers.

#3130702 (DS)  Unit 3 – Non-Linear Data Structure (Tree


Dr. Pradyumansinh U. Jadeja 30
Tree applications :
 Store hierarchical data, like folder structure, organization structure, XML/HTML data.
 Binary Search Tree is a tree that allows fast search, insert, delete on a sorted data. It also allows finding
closest item
 Heap is a tree data structure which is implemented using arrays and used to implement priority queues.
 B-Tree and B+ Tree : They are used to implement indexing in databases.
 Syntax Tree: Used in Compilers.
 K-D Tree: A space partitioning tree used to organize points in K dimensional space.
 Trie : Used to implement dictionaries with prefix lookup.
 Suffix Tree : For quick pattern searching in a fixed text.
 Spanning Trees and shortest path trees are used in routers and bridges respectively in computer networks
 As a workflow for compositing digital images for visual effects.

#3130702 (DS)  Unit 3 – Non-Linear Data Structure (Tree


Dr. Pradyumansinh U. Jadeja 31
AVL ( Adelson-Velsky and Landis ) Tree : -
 AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of
left and right subtrees cannot be more than one for all nodes. 
 An Example Tree that is an AVL Tree  An Example Tree that is NOT an AVL Tree

#3130702 (DS)  Unit 3 – Non-Linear Data Structure (Tree


Dr. Pradyumansinh U. Jadeja 32
AVL Tree
RR Rotation

33
33
Example

34
34
LL Rotation

35
35
Example

36
36
LR Rotation

37
37
2-3 trees : use all of them to prevent having an unbalanced tree
and improves average access time.

In a 2-3 tree, each tree node contains


either one or two keys, and all leaves are
at the same level.

The 2-3 tree is not a binary tree, A


node contains one or two keys. Every
internal node has either two children
(if it contains one key) or three
38
38
children (if it contains two keys).
Height balanced tree
• Height balance tree (self balancing tree) is a binary tree which
automatically maintain height of tree, and its sub tree on each
insertion and deletion of node. And tree is always complete tree. AVL
tree, red-black tree are example of height balanced tree. . AVL tree
uses balance factor to balance the height of tree. Balance factor is
nothing but difference between height of left subtree and right
subtree.
• A tree whose subtrees differ in height
by no more than one and the subtrees
are height-balanced, too

39
39
Weight balanced tree
• weight-balanced binary trees (WBTs) are a type of self-balancing binary search trees
that can be used to implement dictionaries (maps) and sequences.
• A weight-balanced binary tree is a binary tree which is balanced based on knowledge
of the probabilities of searching for each individual node. Within each subtree, the
node with the highest weight appears at the root. This can result in more efficient
searching performance. Construction of such a tree is similar to that of a Treap, but
node weights are chosen randomly in the latter.
•  The letters represent node values and the numbers represent node weights.
• The weight may be thought of as a probability or activity count associated with the
node. In the diagram, the root is G because its weight is the greatest in the tree. The
left subtree begins with A because, out of all nodes with values that come before G, A
has the highest weight. Similarly, N is the highest-weighted node that comes after G.

40
40

You might also like