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

Chapter 6 Tree

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

Chapter 6 Tree

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

1

7/16/2024
Definition of Tree Structures
Binary Trees and Binary Search Trees
Basic Tree Operations
Traversing in a Binary Tree
General Trees and Their Implementations

7/16/2024 2
A tree structure is a way of representing
the hierarchical nature of a structure in a
graphical form.

Tree is an example of non-linear data


structures.

7/16/2024 3
7/16/2024 4
The root of a tree is the node with no parents.
There can be at most one root node in a tree
(node A in the above example
An edge refers to the link from parent to child
(all links in the figure).
A node with no children is called leaf node
(E,J,K,H and I).
Children of same parent are called siblings (B,
C, D are siblings of A and E, F are the siblings
of B). 7/16/2024 5
A node p is an ancestor of a node q if there
exists a path from root to q and p appears on
the path. For example, A, C and G are the
ancestors for K
The node q is called a descendant of p.
Set of all nodes at a given depth is called
level of the tree (B, C and D are same level).
The root node is at level zero.
7/16/2024 6
7/16/2024 7
The depth of a node is the length of the path from the root to the node

(depth of G is 2, A - C - G).

The height of a node is the length of the path from that node to the

deepest node.

The height of a tree is the length of the path from the root to the

deepest node in the tree.

A (rooted) tree with only one node (the root) has a height of zero.

In the previous example, height of B is 2 (B - F - J).


7/16/2024 8
Size of a node is the number of descendants
it has including itself (size of the sub tree C is
3).
If every node in a tree has only one child
(except leaf nodes) then we call such trees as
skew trees.
If every node has only left child then we call
them as left skew trees.
Similarly, if every node has only right child
then we call them as the right skew trees.
7/16/2024 9
7/16/2024 10
IMPLEMENTATION OF TREE
The tree data structure can be created by creating the
nodes dynamically with the help of the pointers. The
tree in the memory can be represented as shown
below:

The above figure shows the representation of the


tree data structure in the memory. In the above
structure, the node contains three fields. The second
field stores the data; the first field stores the address of
the left child, and the third field stores the address of
the right child.
11
7/16/2024
A tree is called binary tree if each node has
zero child, one child or two children.
We can visualize a binary tree as
consisting of a root and two disjoint binary
trees, called the left and right subtrees of
the root.

7/16/2024 12
7/16/2024 13
Strict Binary Tree: A binary tree is
called strict binary tree if each node has
exactly two children or no children.

7/16/2024 14
Full Binary Tree: A binary tree is called full binary tree if each node has
exactly two children and all leaf nodes are at same level.

7/16/2024 15
Complete binary tree
A binary tree is called complete binary tree if all leaf nodes are at height h
or h - 1 and also without any missing number in the sequence.

7/16/2024 16
7/16/2024 17
Binary Search Tree is a node-based binary tree
data structure which has the following
properties:
The left subtree of a node contains only nodes
with keys lesser than the node’s key.
The right subtree of a node contains only
nodes with keys greater than the node’s key.
The left and right subtree each must also be a
binary search tree.
7/16/2024 18
7/16/2024 19
Searching a key
To search a given key in Binary Search Tree,
we first compare it with root, if the key is
present at root, we return root.
If key is greater than root’s key, we recur for
right subtree of root node.
Otherwise we recur for left subtree.

7/16/2024 20
Illustration to search 6 in above tree:

1. Start from root.


2. Compare the search element with root, if
less than root, then recurse for left, else
recurse for right.
3. If element to search is found anywhere,
return true, else return false.

7/16/2024 21
1.int SearchBST (
2.Node *RootNodePtr, int X)
3.{
4.if(RootNodePtr == NULL)
5.return 0; // 0 means (false, not found).
6.else
7.if(RootNodePtr ->Num == X)
8.return 1; // 1 means (true, found).
9.else if(RootNodePtr ->Num > X)
10.return(SearchBST(RootNodePtr ->Left, X));
11.else
12.return(SearchBST(RootNodePtr ->Right, X));
13.}

7/16/2024 22
Insertion of a key
A new key is always inserted at leaf.
We start searching a key from root till we
hit a leaf node.
Once a leaf node is found, the new node is
added as a child of the leaf node.

7/16/2024 23
7/16/2024 24
 void InsertBST( )  NP->Left = InsNodePtr;

 {  Inserted=1;

 Node *InsNodePtr;  }

 InsNodePtr = new Node;  else

 cout << "Enter The Number" << endl;  NP = NP->Left;

 cin>>InsNodePtr->Num;  }

 InsNodePtr->Left=NULL;  else {

 InsNodePtr->Right=NULL;  if(NP->Right == NULL)

 if(RootNodePtr== NULL)
 {

 RootNodePtr=InsNodePtr;
 NP->Right = InsNodePtr;

 else
 Inserted=1;
 }
 {
 else
 Node *NP=RootNodePtr;
 NP = NP->Right;
 int Inserted=0;
 }
 while(Inserted ==0)
 }
 {
 }
 if(NP->Num > InsNodePtr->Num)
 }
 {

 if(NP->Left == NULL) 7/16/2024 25

 {
Binary search tree can be traversed in three
ways.
PreOrder traversal:- traversing binary tree
in the order of parent, left and right.
InOrder traversal:- traversing binary tree in
the order of left, parent and right.
PostOrder traversal:- traversing binary tree
in the order of left, right and parent.
Example:
7/16/2024 26
7/16/2024 27
Preorder traversal:
10, 6, 4, 8, 7, 15, 14, 12, 11, 13, 18, 16, 17, 19
Inorder traversal:
4, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
Used to display nodes in ascending order.
PostOrder traversal:
4, 7, 8, 6, 11, 13, 12, 14, 17, 16, 19, 18, 15, 10
7/16/2024 28
Store values on leaf nodes and operators on internal nodes:

Preorder traversal: - used to generate mathematical expression in


prefix notation.

Inorder traversal: - used to generate mathematical expression in


infix notation.

Postorder traversal: - used to generate mathematical expression in


postfix notation.

7/16/2024 29
7/16/2024 30
 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

7/16/2024 31
Preorder traversal
1. Process the value in the root (e.g. print the root value).
2. Traverse the left subtree with a preorder traversal.
3. Traverse the right subtree with a preorder traversal.
Inorder traversal :-
prints the node values in ascending order:
1. Traverse the left subtree with an inorder traversal.
2. Process the value in the root (e.g. print the root value).
3. Traverse the right subtree with an inorder traversal.
Postorder traversal
1. Traverse the left subtree with a postorder traversal.
2. Traverse the right subtree with a postorder traversal.
7/16/2024 32
3. Process the value in the root (e.g. print the root value).
7/16/2024 33
7/16/2024 34

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