0% found this document useful (0 votes)
32 views42 pages

Fpga PDF

Fpga

Uploaded by

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

Fpga PDF

Fpga

Uploaded by

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

Data Structures using C

Trees
Trees are used to
represent /
organize data in
hierarchical
manner Memory

Volatile Non-Volatile

DRAM SRAM Solid State Magnetic

Flash EPROM Masked ROM Hard Disk DVD

NAND NOR
Definition of a Tree
• A tree is a finite set of one or more nodes such that,
i. There is specially designated node called the root
ii. The remaining nodes are partitioned into n ≥0 disjoint sets T 1, T2, …, Tn
where each of these sets is a tree.
iii. T1, T2, …, Tn are called sub-trees of that root.
A node stands for an item of information

B C D

E F G H I J

K L M
Trees – Terminology (1 of 2)
• The number of sub-trees of a node is called its “Degree”.
Accordingly degree of A is 3, C is 1 and F is 0 (zero) for the tree
below.
• Nodes with degree zero are called “Leaf Nodes” or “Terminal
Nodes”. Accordingly, {K, L, F, G, M, I, J} is the set of “Leaf Nodes” or
“Terminal Nodes” for the tree below. Other nodes are called “Non-
terminal Nodes”.
• The roots of the sub-tree
A
of a Node ‘x’ are the
children of ‘x’. Thus, H, I
and J are the “Children” B C D
of ‘D’. The “Parent of D is
A.
E F G H I J
• Children of the same
parent are called
“Siblings”. Thus, H, I and K L M
J are siblings.
Trees – Terminology (2 of 2)
• The “Degree” of Tree is the Max of the degree of the Nodes of the
tree. Thus the degree of the tree below is 3.
• The ancestors of a Node are all the Nodes along the path from the
root to that node. Thus, for the tree below ancestors of M are A, D
and H.
• The “Level” of a Node Level
is defined by letting
the root be at Level A 1
One. If a node is at
level ‘l’, then its 2
children are at level B C D
‘l+1’. The “Height” or
“Depth” of a tree is
E F G H I J 3
defined to be the Max
level of any node in
that tree. Thus, depth
K L M
of the tree on RHS is 4 4
List Representation of a Tree
• The root comes first, followed by a list of sub-trees
• The List Representation of the tree below is
( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )

B C D

E F G H I J

K L M
Memory Representation A

of a Tree
• Following is the memory B C D
representation of the tree
on the Right Hand Side.
E F G H I J
• With this kind of
representation it is now
easier to implement the K L M
List Handling Functions
A 0

B F 0 C G 0 D I J 0

E K L 0 H M 0
• Each node can have the fields for the data and a pointer each to it’s
children. The number of pointer fields can be as many as the “degree” of
the particular node, as shown below: But, considering that degree of each
node is different in a tree of the kind
DATA CHILD1 CHILD1 -------- CHILDk shown above, it is difficult to implement
algorithms as the nodes vary in size!!
Left Child - Right Sibling Representation
• To convert into this representation, we note that every node has at most
one leftmost child and at most one closest right sibling. Ref. the tree we so
far have been using as example, the leftmost child of A is B, and the
leftmost child of D is H. Further, the closest right sibling of B is C and that
of H is I. Further considering that order of children and closeness of a
sibling, for a node, in a tree is not important, we could redraw the
example tree as below
A
data
left child right sibling
B C D
The Memory
representation for a
node based on the
redrawn tree could H I J
E F G
then be as above

K L M
Degree – Two Representation A
• When the Left Child – Right Sibling
Tree is rotated by 450 clockwise,
B
we get the Degree – Two Tree.
• The two children of a Node in a
E C
Degree – Two representation are
called – Left Child and Right Child.
• Right Child field of “Root” Node in K F G D
the Degree – Two Tree is empty,
since the “Root” Node of the Left L H
Child – Right Sibling Tree that is
being transformed can never have M I
a sibling.
J
Left Child – Right Child Trees
are also known as Binary Trees data
The Memory representation for a node then is, left child right child
Binary Trees
• A Binary Tree is a finite set of nodes that are either empty or consist
of a root and two disjoint binary trees called the left sub-tree and
the right sub-tree.

• The Max number of Nodes on


level ‘i’ of a Binary Tree is 2i-1,
i≥1
• The Max number of Nodes in a
Binary Tree of depth k is 2k-1,
K≥1
Few Example Binary Trees
A A Full 1
Skewed Complete Binary Tree of
Binary Tree Binary Tree depth 4 2 3
B B C
4 5 6 7

C D E F G 8 9 10 11 12 13 14 15

H I
D
More Binary Trees A

E A A
B

B C B C
A Binary Tree (BT) is a Tree in which no Node can have more than two children
Array / Sequential Representation of
Binary Trees A [1] A
[2] B
A [1] A
B [3] --
[2] B [4] C
B C [3] C [5] --
[4] D C
[6] --
[5] E
D E F G [7] --
[6] F D [8] D
[7] G
H I
[9] --
[8] H
E
. .
[9] I [16] E
Note1: The Zeroth’s position of the array is left empty!!
Note2: For the complete Binary Tree such as the one on the Left Hand Side this
representation is ideal, but for a Skewed Binary Tree such as the one on the Right
Hand Side, less than half of the array space would be utilized!!
Array / Sequential Representation of
Binary Trees: More Examples
A H

B C I J

D E K L

F G M

0 1 2 3 4 5 6 7 8 9 10 11 12 13 0 1 2 3 4 5 6 7 8 9 10
- AB C D -E F G - HI J K L - M

Note: The Zeroth’s position of the array is left empty!!


Array / Sequential Representation of
Binary Trees
• If a complete Binary Tree with ‘n’ nodes is
represented sequentially, then for any Node with
index ’i’, 1 ≤ i ≤ n, we have
1) ‘parent(i)’ is at [i/2] if i ≠ 1. if i = 1, ‘i’ is at the root and
has no parent
2) ‘leftChild(i)’ is at ‘2i’ if 2i ≤ n. if 2i ≥ n, then ‘i’ has no
Left Child.
3) ‘rightChild(i)’ is at 2i + 1. If 2i + 1 ≤ n, then ‘i’ has no
Right Child.
Issues with Array / Sequential representation are: 1. Wastage of memory
space, 2. Insertion / Deletion of new Nodes in the tree are problematic
The alternative therefore is Linked Representation!!
Binary Trees

30 60
20

5 40 70
1 25
5

65 80
1 1 22 2
4 0

Binary search
Not binary trees
search
tree
Linked Representation – Node
Structure and Definition
data

left_child data right_child


left_child right_child
Link Representation of a Binary Tree
A

B C

root
D E F G
A

H I B C

D 0 E 0 0 F 0 0 G 0

0 I 0 0 J 0

The Root of Tree is stored in the data member “root” of the class BinaryTree. This
data member serves as the access pointer to the tree. (Max no of nodes of height h
Recursive function to create a BST
Nodeptr CreateBST(Nodeptr root, int item){
if (root==NULL)
{ root =
getnode();
root->data=
item;
root->lchild=root-
>rchild = NULL;
return root;
}
else
if (item<root->data)
root->lchild = CreateBST(root->lchild,
item); else
if (item>root->data)
root->rchild = CreateBST(root-
>rchild, item); It creates a node using malloc
Getnode():
else
printf("Duplicates are not allowed\
n");
• C code for the same: • // Main function to test the BST creation
• #include <stdio.h>
• #include <stdlib.h>
• int main() {
• • Nodeptr root = NULL;
• // Define the structure for a tree node
• int choice, item;
• typedef struct Node* Nodeptr;
• typedef struct Node { •
• int data; • printf("Binary Search Tree Implementation\
• struct Node *lchild, *rchild;

n");
};
• • root = CreateBST(root, item);
• // Function to create a new node • return 0;
• Nodeptr getnode() {
• Nodeptr newNode = (Nodeptr)malloc(sizeof(Node));
• }
• if (newNode == NULL) {
• printf("Memory allocation failed\n");
• exit(1);
• }
• return newNode;
• }

• // Function to create/insert into the BST
• Nodeptr CreateBST(Nodeptr root, int item) {
• if (root == NULL) {
• root = getnode();
• root->data = item;
• root->lchild = root->rchild = NULL;
• return root;
• } else if (item < root->data) {
• root->lchild = CreateBST(root->lchild, item);
• } else if (item > root->data) {
• root->rchild = CreateBST(root->rchild, item);
• } else {
• printf("Duplicates are not allowed\n");
• }
• return root; }
Searching in A Binary Search Tree

• If the root is NULL, then this is an


empty tree. No search is needed.
• If the root is not NULL, compare the
x with the key of root.
– If x equals to the key of the root, then
it’s
done.
– If x is less than the key of the root, then
no elements in the right subtree can
have key value x. We only need to search
the left tree.
– If x larger than the key of the root, only
the right subtree is to be searched.
Searching a BST
1) Recursive Searching Algorithm:

typedef struct node *Nodeptr;


struct node{
int data;
Nodeptr rchild;
Nodeptr
lchild;
};
Nodeptr
search(Nodeptr
root,int key)
{
/* return a pointer
to the node that
contains key.
If there is no
such node,
return NULL */
2)Iterative Searching Algorithm

• Nodeptr itersearch(Nodeptr root, int key)


• {
• while (root) {
• if (key == root->data) return root;
• if (key < root->data)
• root = root->lchild;
• else root = root->rchild;
• }
• return NULL;
• }

CHAPTER 5 6
Other operations:
1. Finding the maximum element in BST: maximum
element will always be the last right child in a BST.
Move to the rightmost node in a BST and you will
end up in the maximum element.
2. Finding the minimum element: Move to the leftmost
child and you will reach the least element in BST.
3. Finding the height of a tree: height is nothing but
maximum level in the tree plus one. It can be
easily found using recursion.
Insertion To A Binary Search Tree
• Before insertion is performed, a search must
be done to make sure that the value to be
inserted is not already in the tree.
• If the search fails, then we know the value is
not
in the tree. So it can be inserted into the tree.
• If item is lesser than the root item, move to
left or else move to the right of root node.
• This process is repeated until the correct
position is found.
10 10

8 15 8 15

5 9 12 17 5 9 12 17

13
To insert item 13 to above BST
• Compare with the root item. 13> 10, hence move
to right and reach 15.
• Now 13<15, So go to left and reach 12.
• 13>12, hence move right.
• Now the correct position is found and hence insert
the new node to the right of 12.
10
10

7 15
7 15

5 9 12 17
5 9 12 17

To insert 8 into the above tree


• Compare with root item. 8<10, hence move left
and reach 7.
• Now 8>7. So move right and reach 9.
• 8<9. Move left and the correct position is obtained.
• Insert 8 to the left of 9.
Insertion Into A Binary Search
Tree
typedef struct node *Nodeptr; /*traverse until correct position is
found*/
struct node{
int data; parent=NUL
Nodeptr
L;
cur=*root;
rchild; while(cur){
Nodeptr parent=
lchild; cur;
}; if (item==cur->data )
void { printf("Duplicates Not
Insert(Nodeptr allowed"); free(temp);
return;
*root, int item) }
{ else if (item<cur->data)
cur=cur->lchild;
Nodeptr temp= else
getnode(); temp- cur=cur->rchild;
>data = item; }
temp->lchild = if (item<parent-
NULL; temp- >data) parent-
>lchild = temp;
>rchild = NULL; if else
Deletion From A Binary Search Tree
• Delete a leaf node
– A leaf node which is a right child of its
parent
– A leaf node which is a left child of its parent
• Delete a non-leaf node
– A node that has one child
– A node that has two children
• Replaced by the largest element in its left subtree,
or
• Replaced by the smallest element in its
right subtree
Deleting From A Binary Search Tree

30

5 40

2 35 80
Deleting From A Binary Search Tree
non-leaf
node

3
0

2
40
5

2 80
When the node to be deleted has non empty left
subtree and non-empty right subtree
Deletion from a Binary Search
Tree
non-leaf
40 45
node

20 60 20 60

10 30 50 70 10 30 50 70

45 55 55

52 52
Before deleting 40 After deleting 40
CHAPTER 5 1
6
Binary Traversals and Tree Iterators
• One notion that arises frequently is the idea of traversing
a Tree, visiting each Node EXACTLY once; visit of the Node
meaning accessing of the data field of the Node
• When we traverse a Binary Tree we want to treat each
Node and sub-trees in the same fashion
• Let “L” Stand for “Moving Left”, Let “R” for “Moving Right”
and “V” for “Visiting the Node”, Then there are SIX
possible combinations for traversals: LVR, LRV, VLR, VRL,
RVL, RLV.
• If we adopt the convention that we travel left before right,
then we have 3 traversals: LVR, LRV and VLR.
• To these we assign the names: “INORDER”, “POSTORDER”,
and “PREORDER”.
Binary Traversals and Tree Iterators
• There is a natural
correspondence between +
the 3 traversals and
producing the “Infix”,
* E
“Postfix” and “Prefix”
formats of Expressions.
• For our subsequent study * D

of the 3 traversal methods


of Binary Trees we refer to / C
the expression
represented in the form of
A B
a Binary Tree on the Right
Hand Side
Inorder Traversal (LVR)
Traversing a binary tree in inorder (or symmetric order)
1. Traverse the left subtree in inorder.
2. Visit the root.
3. Traverse the right subtree in inorder.

void inorder(struct node *root) {


if(root == 0)
Return 0;
else { Recursive!!
inorder(root->left);
Printf(“%d”, root->data);
inorder(root->right);
}

The Inorder Traversal Sequence of the Example Tree therefore is: A/B*C*D+E
Home work for 12/12/24
• #include <stdio.h>
• #include <stdlib.h> • if (data < root->data) {
• root->left = insert(root->left, data); // Insert in the left subtree
• // Define the structure for a node in the binary tree • } else if (data > root->data) {
• struct node { • root->right = insert(root->right, data); // Insert in the right
• int data; subtree
• struct node *left, *right; • }
• };
• // Function to create a new node • return root;
• struct node* newNode(int data) { • }
• struct node* temp = (struct node*)malloc(sizeof(struct node));
• temp->data = data; • int main() {
• temp->left = temp->right = NULL; • struct node* root = NULL;
• return temp;
• } • // Insert nodes into the binary search tree
• // Inorder traversal function • root = insert(root, 50);
• void inorder(struct node *root) { • insert(root, 30);
• if (root == NULL) { • insert(root, 70);
• return; // Base case: if the node is NULL, do nothing • insert(root, 20);
• } • insert(root, 40);
• inorder(root->left); // Traverse the left subtree • insert(root, 60);
• printf("%d ", root->data); // Visit the root node • insert(root, 80);
• inorder(root->right); // Traverse the right subtree
• } • // Perform inorder traversal
• printf("Inorder traversal of the binary tree: \\n");
• // Function to insert a node in a Binary Search Tree (BST) • inorder(root);
• struct node* insert(struct node* root, int data) {
• if (root == NULL) { • return 0;
• return newNode(data); // Create a new node if the tree is • }
empty
• }
Preorder Traversal (VLR)
• Traversing a binary tree in preorder (depth-first order)
1. Visit the root.
2. Traverse the left subtree in preorder.
3. Traverse the right subtree in preorder.
void preorder(struct node *root) {
if(root == 0)
Return 0;
else { Recursive!!
Printf(“%d”, root->data);
preorder(root->left);
preorder(root->right);
}

The Preorder Traversal Sequence of the Example Tree therefore is: +**/ABCDE
Postorder Traversal (LRV)
• Traversing a binary tree in postorder
1. Traverse the left subtree in postorder.
2. Traverse the right subtree in postorder.
3. Visit the root.
void postorder(struct node *root) {
if(root == 0)
Return 0; Recursive!!
else {
postorder(root->left);
postorder(root->right);
Printf(“%d”, root->data);

The Postorder Traversal Sequence of the Example Tree therefore is: AB/C*D*E+
Level-Order Traversal
• In Level-Order Traversal, you process / visit the nodes by depth, first
the root, then the children of the root etc.
• This traversal is also known as “Breadth-First” Traversal
• In this traversal all nodes present at the same level are visited one
by one from left to right.
• Queues are generally used for Level-Order Traversal

Whether written iteratively or recursively, the


inorder, preorder and post order traversals all
require a stack. Level-order traversal can be
done using queues

<ref. https://www.geeksforgeeks.org/print-level-order-traversal-line-line/>

The Level-Order Traversal Sequence of the Example Tree therefore is: +*E*D/CAB
Example: Binary Tree Traversals

Inorder: DGBAHEICF
Preorder: ABDGCEHIF
Postorder: GDBHIEFCA
Level order: ABCDEFGHI
More Examples: Binary Tree Traversals
Few Applications of Tree Data Structure
• File systems are almost always implemented as a tree structure
with, two types of nodes: folders (or directories), and files
• Trees are also used in implementing games, such as board games
where, A node represents a position on the board and the children
of the node representing all the possible moves from that position
• Trees are also used in the evaluation of expressions and also the
representation of control statements:

You might also like