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

Binarty Tree and Graph

Uploaded by

hanishkadabas21
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Binarty Tree and Graph

Uploaded by

hanishkadabas21
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

ARYAN

WHAT IS BINARY TREE DATA STRUCTURE?


A binary tree is a hierarchical data structure in computer science composed of nodes, where each node has
at most two children, referred to as the left child and the right child. The topmost node in a binary tree is
called the root, and nodes with no children are called leaves. The nodes in a binary tree are connected by
edges, and the edges represent the relationships between the nodes.

Here are some key terms associated with binary trees:

1. **Root:** The topmost node in a binary tree.

2. **Parent:** A node in a binary tree that has one or more child nodes.

3. **Child:** Nodes that are descendants of a parent node.

4. **Leaf:** A node in a binary tree that has no children.

5. **Subtree:** A tree formed by a node and its descendants.

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:

TYPES OF BINARY TREE

1. **Full Binary Tree:**

- In a full binary tree, each node has either 0 or 2 children.

- All leaf nodes are at the same level.

2. **Complete Binary Tree:**

- In a complete binary tree, all levels are completely filled except possibly for the last level, which is filled
from left to right.

- Complete binary trees are useful in representing heaps.

3. **Perfect Binary Tree:**

- In a perfect binary tree, all internal nodes have exactly two children, and all leaf nodes are at the same
level.

- The number of nodes at each level is a power of 2.

4. **Balanced Binary Tree:**


ARYAN

- 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.

5. **Degenerate (or pathological) Tree:**

- 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.

6. **Binary Search Tree (BST):**

- 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.

- BSTs provide efficient search, insert, and delete operations.

7. **Threaded Binary Tree:**

- In a threaded binary tree, additional pointers are added to each node to represent an in-order or pre-
order traversal without using recursion.

- Threaded trees can be used to optimize certain types of tree traversals.

8. **AVL Tree (Adelson-Velsky and Landis Tree):**

- 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.

EXPLAIN BINARY SEARCH TREE


Certainly! A Binary Search Tree (BST) is a binary tree data structure in which each node has
at most two children, referred to as the left child and the right child. The key property of a
BST is that for every node, all values in its left subtree are less than its own value, and all
values in its right subtree are greater than its own value. This ordering property allows for
efficient searching, insertion, and deletion of elements within the tree.
1. **Binary Tree Structure:**
- Each node in a BST has at most two children: a left child and a right child.
- Nodes with no children are called leaves.
2. **Ordering Property:**
- For every node `N`, all nodes in its left subtree have values less than `N`, and all nodes
in its right subtree have values greater than `N`.
- This ordering property ensures that the BST can be used for efficient searching.
ARYAN

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

TREE : A tree data structure is a hierarchical structure that is


used to represent and organize data in a way that is easy to
navigate and search. It is a collection of nodes that are connected
by edges and has a hierarchical relationship between the nodes.

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.

THREADED BINARY TREE, STUDY REPRESENTION OF GRAPH IN MEMORY FROM


NOTES, DIFFERENCE B /B+TREE
ARYAN

BFS AND DFS

You might also like