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

Tree

Hi

Uploaded by

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

Tree

Hi

Uploaded by

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

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.

Key Terminologies in a Tree:

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:

Let’s take a simple example of a company structure:

CEO

/ \

Manager1 Manager2

/ \ \

Emp1 Emp2 Emp3

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:

1. Parent-child representation (Adjacency List):


o Each node points to its children. This is a simple way to represent trees where
each node has references to its child nodes.
o Example:

struct Node {

int data;

Node* left; // Pointer to the left child

Node* right; // Pointer to the right child

};

 

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.

 Binary Tree Representation:

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

 Array Representation of Trees:

 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

 Array representation: [1, 2, 3, 4, 5, 6, 7]


 For node 1 (index 0):

 Left child: 2*0 + 1 = 1 (value 2)


 Right child: 2*0 + 2 = 2 (value 3)

Pointer Representation (Linked Representation):

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

Binary Tree Data Structure :

A Binary Tree Data Structure is a hierarchical data structure in which each


node has at most two children, referred to as the left child and the right
child. It is commonly used in computer science for efficient storage and
retrieval of data, with various operations such as insertion, deletion, and
traversal.

Types of Binary Trees

 Full Binary Tree: Every node has either 0 or 2 children.


 Perfect Binary Tree: All interior nodes have two children, and all leaves are at the
same level.
 Complete Binary Tree: All levels are filled except possibly the last, and nodes are
filled from left to right.
 Skewed Binary Tree: All nodes have only one child, making the tree linear (like a
linked list). It can be left-skewed or right-skewed.
 Balanced Binary Tree: A tree where the height of the left and right subtrees of any
node differ by no more than one.
Insert into a Binary Search Tree:

To insert a node into a binary search tree:


 Check the value to be inserted (say X) with the value of the current
node (say val) we are in:
o If X is less than val move to the left subtree.
o Otherwise, move to the right subtree.
 Once the leaf node is reached, insert X to its right or left based on the
relation between X and the leaf node’s value.
Follow the below illustration for a better understanding:

Searching in Binary Search Tree (BST)


Algorithm to search for a key in a given Binary Search Tree:
Let’s say we want to search for the number X, We start at the root. Then:
 We compare the value to be searched with the value of the root.
o If it’s equal we are done with the search if it’s smaller we know
that we need to go to the left subtree because in a binary
search tree all the elements in the left subtree are smaller and
all the elements in the right subtree are larger.
 Repeat the above step till no more traversal is possible
 If at any iteration, key is found, return True. Else False.
Tree Traversals:

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:

Preorder inorder postorder


1 1 1
/ \ / \ / \
2 3 2 3 2 3
/\ /\ /\ /\ /\ /\
4 56 7 4 56 7 4 56 7
 Visit the root node first,  Recursively visit the left  Recursively visit the left
then recursively visit the left subtree, then the root, and subtree, then the right
subtree, followed by the right finally the right subtree. subtree, and finally the root.
subtree.  Order: Left -> Root ->  Order: Left -> Right ->
 Order: Root -> Left -> Right. Root.
Right.

The preorder traversal would The inorder traversal for the The postorder traversal would
be: 1, 2, 4, 5, 3, 6, 7 same tree: 4, 2, 5, 1, 6, be: 4, 5, 2, 6, 7, 3, 1
3, 7

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.

Method - 1: Deletion of A Node Having No Child (Leaf Node)


Method - 2: Deletion of A Node Having Only One Child Node
Method - 3: Deletion of A Node Having Two Children Nodes

Method - 1: Deletion of A Node Having No Child


(Leaf Node)
 It is the simplest method when deleting a node is the leaf node.
 In this, the leaf node with is replaced the NULL and simply free the allocated space.
 Consider the following example where leaf node with value = 30 is deleted from the
BST:

Method - 2: Deletion of A Node Having Only One


Child Node
 In this, replace the node that is going to be deleted with its only child node and then
delete the child node, which now contains the value which is to be deleted.
 Replace it with the NULL and free the allocated space.
 Consider the following example where the node with one child node whose value
= 25 is deleted from the BST:
Method - 3: Deletion of A Node Having Two Children
Nodes
 It is a quite complex method case compared to the other two methods.
 In this, the node which is to be deleted is replaced with its in-order successor or
predecessor recursively until the node to be deleted is replaced on the leaf of the
tree.
 After this, replace the node with NULL and free the allocated space. Consider the
following example where the node with two child nodes whose value = 60 is
deleted from the BST:

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:

 Visit the right subtree of the deleting node.


 Take the least value node called the in-order successor node.
 Replace the deleting node with its in-order successor node.
 In this way take the node with value 65 which is the least valued in-order successor
node of node 60 that is to be deleted.

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.

Binary Search Tree (BST) for the following sequence of numbers-


47, 12, 75, 88, 90, 73, 57, 1, 85, 50, 62

Draw binary search tree by using the given element:


14,15,4,9,7,18,3,5,16,4,20,17,9,14,5.

You might also like