Binarty Tree and Graph
Binarty Tree and Graph
2. **Parent:** A node in a binary tree that has one or more child nodes.
6. **Depth:** The depth of a node in a binary tree is the number of edges on the path from the root to
that node.
7. **Height (or depth of the tree):** The length of the longest path from the root to a leaf. The height of a
tree is the maximum depth of any node in the tree.
8. **Binary Search Tree (BST):** A binary tree where the left subtree of a node contains only nodes with
values less than the node's value, and the right subtree contains only nodes with values greater than the
node's value. This property ensures efficient searching, insertion, and deletion operations.
Binary trees can be traversed in different ways. The three main types of binary tree traversals are:
1. **Inorder Traversal:** Traverse the left subtree, visit the root, and then traverse the right subtree.
2. **Preorder Traversal:** Visit the root, traverse the left subtree, and then traverse the right subtree.
3. **Postorder Traversal:** Traverse the left subtree, traverse the right subtree, and then visit the root.
There are several types of binary trees, each with specific properties and use cases. Here are some
common types:
- In a complete binary tree, all levels are completely filled except possibly for the last level, which is filled
from left to right.
- In a perfect binary tree, all internal nodes have exactly two children, and all leaf nodes are at the same
level.
- A balanced binary tree is a tree in which the height of the left and right subtrees of any node differs by
no more than one.
- Balancing ensures that the tree remains relatively shallow, leading to efficient search and insertion
operations.
- In a degenerate tree, each parent node has only one associated child node.
- Also known as a pathological tree or a skewed tree, it essentially becomes a linked list.
- In a binary search tree, for each node, all elements in its left subtree are smaller, and all elements in its
right subtree are greater.
- In a threaded binary tree, additional pointers are added to each node to represent an in-order or pre-
order traversal without using recursion.
- An AVL tree is a self-balancing binary search tree. It maintains balance by performing rotations after
insertions and deletions to ensure that the height difference between left and right subtrees is at most 1.
- AVL trees guarantee logarithmic height and, consequently, efficient search operations.
These are just a few examples of binary trees, and there are other specialized types and variations based on
specific requirements and applications. Each type has its own advantages and use cases depending on the
problem at hand.They provide a flexible and efficient way to organize and represent hierarchical
relationships among data.
The ordering property of a BST makes it useful for a variety of applications, especially for
search, insertion, and deletion operations. Here's how these operations work in a Binary
Search Tree:
1. **Searching in a BST:**
- To search for a value in a BST, start at the root.
- Compare the target value with the value at the current node.
- If the target value is smaller, move to the left subtree; if larger, move to the right
subtree.
- Repeat the process until the target value is found or the current node is null (indicating
the value is not in the tree).
2. **Insertion in a BST:**
- To insert a new value, perform a search to find the appropriate position for the new
node based on the ordering property.
- Once the appropriate position is found (i.e., a null child where the new node should
be), insert the new node with the given value.
3. **Deletion in a BST:**
- To delete a node with a specific value, first, find the node to be deleted using a search.
- There are three cases to consider:
- If the node to be deleted is a leaf, simply remove it.
- If the node has one child, replace the node with its child.
- If the node has two children, find the node's in-order successor (the smallest value in
its right subtree), replace the node's value with the in-order successor's value, and then
recursively delete the in-order successor.
The time complexity of search, insertion, and deletion in a balanced BST is O(log n), where
n is the number of nodes. However, in the worst case (when the tree is degenerate or
unbalanced), the time complexity can degrade to O(n), where n is the number of nodes,
making it similar to a linked list.
To address this issue, self-balancing binary search trees, such as AVL trees or red-black
trees, are used. These trees automatically maintain balance during insertions and
deletions, ensuring efficient performance in the worst case as well.
ARYAN
The topmost node of the tree is called the root, and the nodes
below it are called the child nodes. Each node can have multiple
child nodes, and these child nodes can also have their own child
nodes, forming a recursive structure.
This data structure is a specialized method to organize and store
data in the computer to be used more effectively. It consists of a
central node, structural nodes, and sub-nodes, which are connected
via edges. We can also say that tree data structure has roots,
branches, and leaves connected with one another.