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

Tree

Uploaded by

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

Tree

Uploaded by

Swagoto Some
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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.

Key Concepts of Trees

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

1. Node Structure: The tree nodes are represented by a structure Node,


which contains data and pointers to the left and right children.
2. Insertion: The insert() function adds a new node to the BST by
traversing the tree recursively. If the data is less than the current node's
data, it goes to the left child; if greater, it goes to the right.
3. Searching: The search() function checks if a given key exists in the
BST by comparing the key with the root and then moving left or right
accordingly.
4. Traversal:
o Inorder Traversal: Visits nodes in ascending order.
o Preorder Traversal: Visits nodes starting from the root, then left, and then
right.
o Postorder Traversal: Visits nodes starting from the leftmost child, then right,
and finally the root.
5. Main Function: Demonstrates inserting nodes into the BST, performing
tree traversals, and searching for a specific node.

Important Properties of Trees

• 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

1. Hierarchical Data Representation: File systems, organizational structures.


2. Binary Search Trees: Used in searching algorithms.
3. Expression Trees: Used in compilers for parsing expressions.
4. Heaps: A type of tree used in priority queues.
5. Tries: Used for efficient retrieval of a key in a dataset of strings.
Trees are fundamental structures in computer science due to their versatility and
efficiency in representing hierarchical data and enabling fast search operations.

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 +, -, *, /).

Types of Expression Trees

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

Construction of Expression Trees

• Operands (like numbers or variables) are always leaves.


• Operators (like +, -, *, /) are internal nodes with two children.

Example:

Consider the expression 3 + 5 * (2 - 4). The corresponding expression tree


would be:

+
/\
3 *
/\
5 -
/\
2 4

Traversal of Expression Trees


1. Inorder Traversal: (Left, Root, Right) - Yields the original expression (with
parentheses).
2. Preorder Traversal: (Root, Left, Right) - Yields the prefix expression.
3. Postorder Traversal: (Left, Right, Root) - Yields the postfix expression.

Expression Tree Operations

• Evaluation: Calculate the value of the expression represented by the tree.


• Conversion: Convert between different forms of expressions (infix, prefix, postfix).

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.

Applications of Expression Trees:

• Compilers: Used in the compilation process to evaluate expressions.


• Calculators: They use expression trees to process input and compute results.
• Symbolic Computation: Systems like Mathematica use expression trees for symbolic
algebra.

Expression trees are powerful tools for representing and manipulating


expressions in a structured way, making them essential in many computer
science applications.

You might also like