Trees
Trees
TREE
• is a hierarchical data structure composed of nodes
connected by edges.
• It is widely used to represent hierarchical relationships
and is fundamental in various algorithms and applications.
• Trees are characterized by their branching structure,
starting from a root node and extending downwards to
child nodes, which may further branch into more nodes.
TREE
• 6. Internal Nodes : Nodes that have at least one child are called
internal nodes. They are neither the root nor leaf nodes.
1. Node Structure :
- Each node in a binary tree typically contains:
- Data (value stored in the node)
- References or pointers to its left and right
children (or null if the child does not exist).
2. BinaryTreeNode Class (Example in C ):
public class BinaryTreeNode
{
public int Data; // Data stored in the node
public BinaryTreeNode Left; // Pointer to the left child node
public BinaryTreeNode Right; // Pointer to the right child node
1
/\
2 3
/\
4 5
In this tree:
- Node 1 is the root node.
- Node 2 and Node 3 are children of Node 1.
- Node 4 and Node 5 are children of Node 2.
Key Points:
- Binary Tree : A tree where each node can have at most two children.
- Node Structure : Contains data and pointers to left and right children.
1. Root Node :
- The topmost node of a binary tree is called the root node. It serves as the
starting point for accessing all other nodes in the tree.
- The depth (or level) of a node is the number of edges from the root node to that
node.
- The root node is at level 0, its children are at level 1, and so on.
- A binary tree in which every node other than the leaves has two children.
- The height difference between the left and right subtrees of every node is no more than one.
- Example:
/\
2 3
/\ \
4 5 6
5. Degenerate (or Pathological) Binary Tree :
- Each parent node has only one child (left or right).
- Essentially a linked list in structure.
- Example (skewed to the right):
1
\
2
\
3
\
4