Tree
Tree
A tree is a widely used data structure in computer science that simulates a hierarchical tree
structure with nodes. It consists of a set of nodes and edges, where each node contains a value
or data, and the edges represent the relationships (or connections) between nodes. A tree is a
non-linear data structure because it doesn’t store data in a sequential manner.
1. Node: Each element in a tree is called a node. It contains data and may have child
nodes.
o Example: In a family tree, each person is a node.
2. Root: The topmost node of the tree. It has no parent.
o Example: In a company’s hierarchy, the CEO can be the root of the tree.
3. Edge: The connection between two nodes.
o Example: A manager and their subordinates are connected by an edge.
4. Parent: A node that has a child node.
o Example: In a family tree, a parent node might represent a parent with
children nodes.
5. Child: A node that descends from another node.
o Example: A person’s children in a family tree are child nodes.
6. Leaf: A node that has no children (i.e., the bottommost node in a tree).
o Example: A junior employee with no direct reports in an organizational tree.
7. Sibling: Nodes that share the same parent.
o Example: Two employees reporting to the same manager.
8. Subtree: A tree consisting of a node and all its descendants.
o Example: A manager and all employees reporting to them directly or
indirectly.
9. Depth: The number of edges from the root to a particular node.
o Example: If the root is at depth 0, a node that is two levels below the root is at
depth 2.
10. Height: The number of edges on the longest path from a node to a leaf.
o Example: The CEO might have a height of 3 if the longest chain of
management below them is 3 levels deep.
11. Degree: The number of children a node has.
o Example: A manager with three direct reports has a degree of 3.
12. Path: A sequence of nodes and edges connecting a node to another node.
o Example: The path from the CEO to a junior employee would trace through
all intermediate managers.
13. Ancestor: A node that is higher up in the tree hierarchy.
o Example: A grandparent is an ancestor of a child in a family tree.
14. Descendant: A node that is lower in the tree hierarchy.
o Example: A child or grandchild is a descendant of the root node in a family
tree.
Example of a Tree:
CEO
/ \
Manager1 Manager2
/ \ \
In data structures, a tree is a hierarchical representation consisting of nodes and edges that
reflect parent-child relationships. Trees are a foundational structure for organizing data, and
they are used in various algorithms, databases, and file systems due to their efficient
representation of hierarchical relationships.
Types of Tree Representation in Data Structures:
struct Node {
int data;
};
In this structure, each node stores data, and the two pointers store the left and right
children. For general trees, you can use a list to hold multiple children.
In a binary tree, each node has at most two children (commonly referred to as left
and right children). A binary tree is often implemented using a structure (or class)
where each node contains:
o Data: The value stored in the node.
o Left child: A reference to the left subtree.
o Right child: A reference to the right subtree.
Example
struct Node {
int data;
Node* left;
Node* right;
};
Binary Search Tree (BST) is a specialized form of a binary tree where the left
child contains smaller values than the parent, and the right child contains larger
values.
A tree, especially a binary tree, can also be represented as an array where for a node
at index i:
o The left child is at 2*i + 1
o The right child is at 2*i + 2
o The parent is at (i-1) / 2
This method is particularly useful for representing heaps, which are complete binary
trees.
Example: Consider this binary tree:
/ \
2 3
/\ /\
4 56 7
In this representation, each node contains a pointer to its child node(s). It’s most
commonly used in dynamic tree structures (like binary trees) where the size of the tree
isn’t fixed.
Example (for binary tree):
struct Node {
int data;
Node* left;
Node* right;
};
For general trees with any number of children, a node can have an array or list of
pointers to children.
Child-Sibling Representation (for general trees):
This is a representation for general trees where each node has a pointer to its first
child and a pointer to its next sibling. This allows for trees with any number of
children and provides an efficient way to traverse all children of a node.
Example:
struct Node {
int data;
Node* firstChild;
Node* nextSibling;
};
1.
o In this representation, the first child pointer moves down the tree, and the
sibling pointer moves sideways across nodes of the same level.
In addition to representing trees, it’s important to understand how to traverse them. Tree
traversal means visiting each node in a specific order. There are different ways to traverse a
tree:
Level-order Traversal:
Visit nodes level by level, starting from the root and moving to each subsequent level
from left to right.
Example: The level-order traversal would be: 1, 2, 3, 4, 5, 6, 7
Delete Operation
In a binary search tree, the Delete function is used to delete the specified node.
But, delete a node from a binary search tree in such a way, that the property of the
binary search tree doesn't violate.
To achieve this there are three methods for deleting a node from a binary search tree
are used.
In-order Traversal Sequence of the following Tree is 15, 25, 30, 60, 65, 75, 85,
90, 95.
A node with two children can be deleted from the BST in the following two ways:
Way 1:
Way 2:
Visit the left subtree of the deleting node.
Take the greatest value node called the in-order predecessor node.
Replace the deleting node with its in-order predecessor node.
In this way take the node with value 30 which is the greatest valued in-order
predecessor node of the node 60 that is to be deleted.