Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Tree Data Structure

 A tree is a hierarchical data structure that consists of nodes connected by edges. It resembles the
hierarchical structure of natural trees.
 Key properties:
o Each node has zero or more child nodes.
o The topmost node is called the root.
o Nodes can have their own subtrees (which are also trees).
 Trees are used for organizing and representing data in a way that facilitates efficient navigation
and search.

Basic Terminologies in Tree Data Structure:

1. Parent Node: The predecessor of a node.


2. Child Node: The immediate successor of a node.
3. Root Node: The topmost node (no parent).
4. Leaf Node (External Node): Nodes with no children.
5. Ancestor: Predecessor nodes on the path from the root to a node.
6. Descendant: A node is a descendant of another if the latter is an ancestor of the former.
7. Sibling: Children of the same parent.
8. Level of a Node: The count of edges from the root to that node.
9. Internal Node: A node with at least one child.
10. Neighbour: Parent or child nodes of a node.

Binary Tree

 A binary tree is a special type of tree where each node has at most two children: a left child and
a right child.
 Common operations: insertion, deletion, and traversal.
 Balanced Binary Tree: Ensures that the difference in heights between left and right subtrees is at
most one.

Conversion of General Tree to Binary Tree

 Rules:
1. The root of the binary tree is the root of the general tree.
2. Left child of a node in the general tree becomes the left child in the binary tree.
3. Right sibling of any node in the general tree becomes the right child in the binary tree.
 Example:

o Convert the following Generic Tree to Binary Tree: !Generic Tree


o Binary Tree: !Binary Tree

Tree Traversal Techniques

 Different ways to traverse a tree:


1. Inorder Traversal: Left subtree, root, right subtree.
2. Preorder Traversal: Root, left subtree, right subtree.
3. Postorder Traversal: Left subtree, right subtree, root.
4. Level Order Traversal (BFS): Visit nodes level by level.

Rotation of Tree

 Rotations are used to balance trees (e.g., AVL trees).


 Types: left rotation, right rotation, left-right rotation, right-left rotation.

AVL Tree

 A self-balancing binary search tree.


 Ensures that the difference in heights between left and right subtrees is at most one.
 Used for efficient storage and retrieval of data.

Graphs
 Graphs consist of nodes (vertices) and edges connecting them.
 Types: directed, undirected, weighted, unweighted.
 Applications: social networks, transportation networks, etc.

Connected Components & Spanning Tree


 Connected components: Subgraphs where every pair of nodes is connected by a path.
 Spanning tree: A subgraph that includes all nodes and is a tree (no cycles).

Shortest Path & Transitive Closure


 Shortest path: Finding the shortest path between two nodes in a graph.
 Transitive closure: Determining whether there is a path between every pair of nodes

You might also like