Tree
Tree
A tree is a widely used abstract data type in computer science that represents
hierarchical structures. It is composed of nodes connected by edges. A tree is a
non-linear data structure, unlike arrays, linked lists, stacks, and queues, which
are linear data structures.
1. Node: Each element in a tree is called a node. A node contains data and
references (pointers) to its child nodes.
2. Root: The top node in a tree, with no parent, is called the root.
3. Edge: A connection between two nodes.
4. Parent: A node that has one or more child nodes.
5. Child: A node that is a descendant of another node.
6. Leaf: A node with no children.
7. Subtree: A tree formed by a node and its descendants.
8. Height of a Node: The number of edges on the longest path from the
node to a leaf.
9. Depth of a Node: The number of edges from the root to the node.
10.Height of a Tree: The height of the root node.
11.Degree of a Node: The number of children of a node.
Types of Trees
1. Binary Tree: Each node has at most two children, referred to as the left
child and the right child.
2. Binary Search Tree (BST): A binary tree where for every node, all
elements in the left subtree are less than the node, and all elements in the
right subtree are greater than the node.
3. Balanced Tree: A tree where the height of the left and right subtrees of
any node differ by at most one.
4. Complete Binary Tree: A binary tree in which all levels are completely
filled except possibly the last level, which is filled from left to right.
5. AVL Tree: A self-balancing binary search tree where the difference
between the heights of left and right subtrees cannot be more than one for
all nodes.
6. Red-Black Tree: A self-balancing binary search tree where nodes are
colored red or black and the tree is balanced using color properties.
Tree Operations
1. Insertion: Adding a node to the tree.
2. Deletion: Removing a node from the tree.
3. Traversal: Visiting all nodes in a tree in a specific order (e.g., Inorder,
Preorder, Postorder).
4. Searching: Finding a node in the tree.
Explanation
• Balanced Trees: Ensuring the tree remains balanced (i.e., the heights of
the left and right subtrees of any node differ by no more than one) is
important for maintaining efficient operations (insertion, deletion, and
search).
• Binary Search Tree (BST) Property**: The left subtree contains only
nodes with keys less than the node’s key, and the right subtree only nodes
with keys greater.
Applications of Trees
EXPRESSION OF TREE
Expression Trees are binary trees used to represent mathematical expressions.
Each node in an expression tree represents either an operator or an operand. The
leaves of the tree are operands (constants or variables), and the internal nodes
are operators (like +, -, *, /).
1. Binary Expression Trees: A specific type of expression tree where each internal node
has exactly two children. This is the most common type.
2. Unary/Binary Mixed Trees: These trees may have some internal nodes with one
child (unary operators like - in -a).
Example:
+
/\
3 *
/\
5 -
/\
2 4
Explanation:
1. Data Structure:
o Node: Represents each node in the tree with data (either an operand or
operator), left, and right pointers.
2. Create Node:
o createNode(): Allocates memory for a new node and initializes it.
3. Construct Tree:
o constructTree(): Uses a stack-based approach to construct the tree from
a postfix expression. For each character:
▪ If it’s an operand, create a node and push it onto the stack.
▪ If it’s an operator, pop the top two nodes, make them the operator's
children, and push the resulting subtree back onto the stack.
4. Traversal:
o inorder(): Recursively traverses the tree in an inorder manner to print the
expression.
5. Evaluation:
o evaluate(): Recursively evaluates the expression represented by the tree.
If the node is an operator, it applies it to the results of the left and right
subtrees.