Dsa Trees
Dsa Trees
A full Binary tree is a special type of binary tree in which every parent
node/internal node has either two or no children.
Binary search tree is a data structure that quickly allows us to maintain a sorted
list of numbers.
It is called a binary tree because each tree node has a maximum of two children.
It is called a search tree because it can be used to search for the presence of a
number in O(log(n)) time.
The properties that separate a binary search tree from a regular binary tree is
1. All nodes of left subtree are less than the root node
2. All nodes of right subtree are more than the root node
3. Both subtrees of each node are also BSTs i.e. they have the above two properties
A tree
having a right subtree with one value smaller than the root is shown to
demonstrate that it is not a valid binary search tree
The binary tree on the right isn't a binary search tree because the right subtree of
the node "3" contains a value smaller than it.
There are two basic operations that you can perform on a binary search tree:
Search Operation
The algorithm depends on the property of BST that if each left subtree has values
below root and each right subtree has values above the root.
If the value is below the root, we can say for sure that the value is not in the right
subtree; we need to only search in the left subtree and if the value is above the
root, we can say for sure that the value is not in the left subtree; we need to only
search in the right subtree.
Algorithm:
If root == NULL
return NULL;
If number == root->data
return root->data;
If number < root->data
return search(root->left)
If number > root->data
return search(root->right)
We have attached the node but we still have to exit from the function without
doing any damage to the rest of the tree. This is where the return node; at the end
comes in handy. In the case of NULL, the newly created node is returned and
attached to the parent node, otherwise the same node is returned without any
change as we go up until we return to the root.
This makes sure that as we move back up the tree, the other node connections
aren't changed.
Image showing
the importance of returning the root element at the end so that the elements don't
lose their position during the upward recursion step.
(important)
Tree Data Structure Terminologies
Terminology Description Example from Diagram
The sub-node of a parent node is the ‘5’ and ‘6’ is the children of
Child Node
child node. ‘2’.
The last node which have any subnode ‘5’, ‘6’, ‘9’ and ‘8’ are leaf
Leaf
is the leaf node. nodes.
Degree of The degree of a node represents the total The degree of ‘2’ is 2 (‘5’ and
Node number of children in it. ‘6’). The degree of ‘4’ is 1.
To avoid the cost of all the shifts in memory that we get from using Arrays, it is
useful to implement Binary Trees with pointers from one element to the next, just
like Binary Trees are implemented before this point, especially when the Binary
Tree is modified often.
But in case we read from the Binary Tree a lot more than we modify it, an Array
implementation of a Binary Tree can make sense as it needs less memory, it can
be easier to implement, and it can be faster for certain operations due to cache
locality.
#include <stdio.h>
int main() {
char binaryTreeArray[ARRAY_SIZE] = {'R', 'A', 'B', 'C', 'D', 'E', 'F', '\0', '\0',
'\0', '\0', '\0', '\0', 'G', '\0'};
if (data != NULL) {
printf("root.right.left.data: %c\n", *data);
} else {
printf("No data found.\n");
}
return 0;
}
A binary tree is a tree data structure in which each parent node can have at most
two children. Each node of a binary tree consists of three items:
data item
There are different variants, or types, of Binary Trees worth discussing to get a
better understanding of how Binary Trees can be structured.
The different kinds of Binary Trees are also worth mentioning now as these words
and concepts will be used later in the tutorial.
Below are short explanations of different types of Binary Tree structures, and
below the explanations are drawings of these kinds of structures to make it as
easy to understand as possible.
A balanced Binary Tree has at most 1 in difference between its left and right
subtree heights, for each node in the tree.
A complete Binary Tree has all levels full of nodes, except the last level, which
is can also be full, or filled from left to right. The properties of a complete Binary
Tree means it is also balanced.
A full Binary Tree is a kind of tree where each node has either 0 or 2 child nodes.
A perfect Binary Tree has all leaf nodes on the same level, which means that all
levels are full of nodes, and all internal nodes have two child nodes.The properties
of a perfect Binary Tree means it is also full, balanced, and complete.
Binary Tree Traversal
Going through a Tree by visiting every node, one node at a time, is called
traversal.
Breadth First Search (BFS) is when the nodes on the same level are visited
before going to the next level in the tree. This means that the tree is explored in a
more sideways direction.
Depth First Search (DFS) is when the traversal moves down the tree all the way
to the leaf nodes, exploring the tree branch by branch in a downwards direction.
pre-order
in-order
post-order
Pre-order Traversal of Binary Trees
#include <stdio.h>
#include <stdlib.h>
root->left = nodeA;
root->right = nodeB;
nodeA->left = nodeC;
nodeA->right = nodeD;
nodeB->left = nodeE;
nodeB->right = nodeF;
nodeF->left = nodeG;
// Traverse
preOrderTraversal(root);
free(nodeG);
free(nodeF);
free(nodeE);
free(nodeB);
free(nodeC);
free(nodeD);
free(nodeA);
free(root);
return 0;
}
IN -order Traversal of Binary Trees