0% found this document useful (0 votes)
9 views6 pages

Chapter -5 Tree

Chapter Five discusses tree structures as non-linear data structures that represent hierarchical relationships between nodes. It defines key terminologies related to trees, such as root, leaf node, and internal node, and explains basic tree operations and traversal methods including in-order, pre-order, and post-order traversal. The chapter also provides examples and exercises for constructing and traversing binary search trees.

Uploaded by

nafyjabesa1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Chapter -5 Tree

Chapter Five discusses tree structures as non-linear data structures that represent hierarchical relationships between nodes. It defines key terminologies related to trees, such as root, leaf node, and internal node, and explains basic tree operations and traversal methods including in-order, pre-order, and post-order traversal. The chapter also provides examples and exercises for constructing and traversing binary search trees.

Uploaded by

nafyjabesa1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Chapter Five: Tree Structures

Introduction

 Linked lists provide greater flexibility than arrays but they are linear structures so that it
is difficult to use them to organize a hierarchical representation of objects.
 Stacks and queues reflect some hierarchy but limited to one dimension
 Unlike stacks, queues, linked lists and arrays, Trees are a non linear data structures.
 In linked list each node has a link which points to another node but in tree each node may
point to several other nodes.
 Trees are very flexible, versatile and powerful non-liner data structure that can be
used to represent data items possessing hierarchical relationship between the grand father
and his children and grand children as so on.
 A tree is a set of nodes and edges that connect pairs of nodes. It is an abstract model of
a hierarchical structure.
Terminologies
 Root: node with no parent
 Leaf node (external node): node with no child
 Internal node: a node with at least one child.
 Degree of a node: is the number of sub trees of a node in a given tree.
 Degree of a tree: the highest number of sub tree in the tree
 Ancestors of a node: all parents and Grand.parents of a node
 Descendants of a node: all children and Grand. children of a node
 Depth of a node: number of ancestors or length of the path from the root to the node.
 Height of a tree: depth of the deepest node
 Sub tree: a tree consisting of a node and its descendants.

Example:-
Binary tree: a tree in which each node has at most two children called left child and right child.
Binary search tree (ordered binary tree): a binary tree that may be empty, but if it is not
empty it satisfies the following.
o Every node has a key and no two elements have the same key.
o The keys in the right sub tree are larger than the keys in the root.
o The keys in the left sub tree are smaller than the keys in the root.
o The left and the right sub trees are also binary search trees.
Example:-

Exercise (construct a binary search tree using the given nodes)

1. 10,15,7,3,8,20,25 4. 14,15,4,9,7,18,3,5,16,4,20,17,9,14,5
2. 85,80,90,82,70,100,87 5. 90,95,100
3. 20,10,30,15,25,50,40,1,5,79
Basic Tree Operations (binary tree)
 Consider the following definition of binary search tree.
struct Node
{
int Num;
Node * Left, *Right;
};
Node *RootNodePtr=NULL;
Basic Operations
Following are the basic operations of a tree −

 Search − Searches an element in a tree.

 Insert − Inserts an element in a tree.

 Pre-order Traversal − Traverses a tree in a pre-order manner.

 In-order Traversal − Traverses a tree in an in-order manner.

 Post-order Traversal − Traverses a tree in a post-order manner.

Tree Traversing

Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all
nodes are connected via edges (links) we always start from the root (head) node. That is, we
cannot randomly access a node in a tree. There are three ways which we use to traverse a tree −

 In-order Traversal
 Pre-order Traversal
 Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree or to print all the
values it contains.
In-order Traversal
In this traversal method, the
 left sub tree is visited first, then
 the root and later
 the right sub-tree.
We should always remember that every node may represent a subtree itself. If a binary tree is
traversed in-order, the output will produce sorted key values in an ascending order. Used to
generate mathematical expression in infix notation.

We start from A, and following in-order traversal, we move to its left subtree B. B is also
traversed in-order. The process goes on until all the nodes are visited. The output of inorder
traversal of this tree will be −
D→B→E→A→F→C→G
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.

Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree and finally the right
subtree. From the above tree diagram
We start from A, and following pre-order traversal, we first visit A itself and then move to its
left subtree B. B is also traversed pre-order. The process goes on until all the nodes are visited.
Used to generate mathematical expression in prefix notation.

The output of pre-order traversal of this tree will be −

A→B→D→E→C→F→G

Algorithm
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.
Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we traverse the left
subtree, then the right subtree and finally the root node. From the above given tree diagram
We start from A, and following pre-order traversal, we first visit the left subtree B. B is also
traversed post-order. The process goes on until all the nodes are visited. Used to generate
mathematical expression in postfix notation.

The output of post-order traversal of this tree will be −

D→E→B→F→G→C→A

Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
Exercise

Find the following from the given tree


 In-order Traversal
 Pre-order Traversal
 Post-order Traversal
Convert into tree diagram
Preorder traversal - + – A * B C + D / E F ------>Prefix notation
Inorder traversal - A – B * C + D + E / F ------>Infix notation
Postorder traversal - A B C * – D E F / + + ------>Postfix notation

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