Module - 4
Module - 4
Trees
In computer science, trees are extensively employed and essential to many
algorithms and data storage uses. The terms that are essential to understanding
tree data structures are listed below.
Tree
Nodes are 1, 2, 3, 4, 5, 6, 7, and 8. A root node is 1. 2 and 3 are parents of 1.
2, 3, are brothers and sisters. The leaf nodes are 4, 6, 7, and 8. The internal, or
non-leaf, nodes are 2, 5. The tree's height or depth is three.
A tree with two root nodes is called the left subtree. There is just one node in the
Right-Sub-Tree, which is node 3.
1. Tree: An edge-connected hierarchical data structure with nodes that are each
connected by at least one child node.
2. Node: A single link in a tree data structure that is composed of linkages to its
child nodes as well as data.
3. Root: The highest point in a tree, which is where a tree journey begins. It is
parentless.
4. Parent Node: In a tree data structure, a parent link is a node that is connected
to one or more child nodes. In the hierarchy, it is positioned above its progeny.
5. Child Node: In a tree, nodes that are related to a parent node. In the hierarchy,
they are beneath the parent node.
6. Leaf Node (Terminal Node): An unbroken link in a tree or a node with a zero
out-degree.
7. An internal or non leaf link in a tree is one that has one or more child nodes.
8. Siblings: In a tree, siblings are nodes that have the same parent node.
9. Depth: A node's elevation or separation from the root node. The root node has
a height of zero.
10. Height or Depth of Tree: The maximum distance that a tree's root can go to
reach a leaf node. It stands for the tree's deepest point.
11. Subtree: A tiny tree contained within a bigger tree. It is made up of a node
and every child that it has.
Full-Binary Tree :
Perfect Binary-Tree:
The prerequisites for a height-balanced binary tree are as follows:
1. For each node, there is only one difference between the left and right sub trees.
2. The balance of the left-sub-tree.
3. A balanced sub tree is selected.
1. There are nodes on every level, possibly with the exception of the final one.
2. Nodes are added from left to right if the final level is not entirely occupied.
A complete binary tree has the following essential features:
1. It is a binary tree.
2. Because it is perfectly balanced, the tree's height is kept to a minimum relative
to the number of nodes it has.
3. Because the structure is complete, adding and removing nodes from a binary
tree is usually efficient.
1. The Array Diagram
Linked List Illustration No. 2
Take a look at the binary tree below.
A node in this representation of a linked list has the structure shown below.
The binary tree example above that uses a linked-list internal representation is
displayed as follows.
C Tree Traversal Algorithms:
A binary-tree must be traversed by visiting each node in a certain order. In-order
traversal, pre-order traversal, and post-order traversal are the three common
techniques for navigating a binary tree. Every method specifies a distinct order
for visiting nodes.
}
1. Recursion-Based In Order Traversal Implementation
a. First visit the left node.
b. Then visit the root node.
c. Then visit the right node.
In the above order specified when performing an in order traversal.
2. Using Recursion to Implement Pre Order Traversal
a. First traverse the root node.
b. Then visit the left node.
c. Then visit the right node.
In the above order specified when performing a pre order traversal.
3. Using Recursion to Implement Post-Order Traversal
a. First visit the left node.
b. Then visit the right node.
c. Then traverse the current node.
In the above order specified when performing a post-order traversal.
In order to find a particular value in a Binary Search Tree (BST), one must
traverse the tree using its ordering property as a guide. Here is an explanation
and a C algorithm for searching in a BST:
Performing a Binary-Search-Tree Search:
In order to find a particular value in a Binary Search Tree (BST), one must
traverse the tree using its ordering property as a guide. This is an explanation and
a C algorithm for searching in a BST.
1. The search function requires two inputs: the value we wish to search for (key)
and a pointer to the BST root (root).
2. The function first determines whether the data of the root node matches the key
or whether the tree is empty (root is NULL). The root node is returned, signifying
that the key has been located, if either of these circumstances holds true.
3. Because the key needs to be on the left side according to the BST's ordering
property, it recursively calls search on the left subtree if the key is smaller than
the root's data.
4. Because the key must be on the right subtree, it recursively calls search on the
right subtree if the key value is larger than the roots data.
In a Binary-Search-Tree (BST), adding a new node is as follows:
7. After inserting the key, the function returns the updated root.
Create an empty BST, use the insert function to add multiple values to it, and then
run an in order traversal to copy the contents of the Tree in the main function.
In a Binary-Search-Tree (BST), deletion:
1. The delete function requires two inputs: the value to be deleted (key) and a
pointer to the BST root (root).
Node without children: To update the parent's pointer, we just release the node
and return NULL.
When deleting a node with two children, it is necessary to locate the minimum
value node in the appropriate subtree, which is done using the findMin function.
Create a sample BST in the main function, use the delete function to remove a
particular value (key), and then print the tree before and after the deletion using
an in-order traversal to show how the deletion operation was carried out.
AVL Tree:
AVL tree:The imbalanced node turns to the right and becomes the left child's
right child, as seen.
right first. A left-right rotation is produced by combining the rotations of the left
and right.
The fundamental functions of AVL trees are as follows: 1. Locate a specific node;
2. Add a node; and 3. Remove a node.
Looking around
1. Begin at the root node in order to search for a node with a specific value x.
2. Examine this root node's value in relation to x.
7. Come to the conclusion that there are no nodes in the AVL tree with values
equal to x if you come across a NULL at any stage of the search.
8. Return NULL
Insertion Algorithm:
Removal of a Node from an AVL Tree:
1. A node's two offspring are black if it is red. It means that red nodes cannot
be next to each other on any path that leads from the root to a leaf.
Nonetheless, a sequence of black nodes could contain any number of them.
We refer to this as red condition.
Advantages:
Splay Trees :
Research on node access has demonstrated that once a node or piece of
information is accessed, it is likely to be accessed again. Binary-search-trees with
a self adjusting mechanism are called Splay Trees. The Splay Tree data structure
drastically alters its shape when a node is accessed, pushing that link in the
direction of the base node.
This modification improves the efficiency of subsequent accesses to the node. As
a result, the most recent node that is accessed—whether for insertion or search—
is moved closer to the root each time. Methodically, splay rotations—which are
essentially AVL tree rotations—are followed in order to pushing the node
upwards toward the base.
The other nodes would be forced to move away from the root as a result.
This modification improves the efficiency of subsequent accesses to the node. As
a result, the most recent node that is accessed—whether for insertion or search—
is moved closer to the root each time. Methodically, splay rotations—which are
essentially AVL tree rotations—are followed in order to pushing the node
upwards toward the base.
The other nodes would be forced to move away from the root as a result.
Below are the different rotations that need to be made in the splay tree.