Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Tree Data Structures: A Comprehensive Guide with Examples, Schemes and Mind Maps of Computer Science

A comprehensive overview of tree data structures, covering their definition, types, and traversal methods. It delves into the concepts of general trees, binary trees, complete binary trees, binary search trees, avl trees, b trees, and b+ trees. The document also includes detailed explanations of tree traversal algorithms, such as pre-order, in-order, and post-order traversal, along with illustrative examples. It further explores the implementation of tree data structures using linked and sequential representations. Valuable for students and professionals seeking a thorough understanding of tree data structures and their applications in computer science.

Typology: Schemes and Mind Maps

2023/2024

Available from 11/11/2024

dipika-bhosale
dipika-bhosale 🇮🇳

5 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 05
Tree
Definition:
A non-linear data structure called tree. A tree is a structure which is mainly used to store data that is
hierarchical in nature.
01 Tree Terminology:
Root node: The root node R is the topmost node in the tree. If R = NULL, then it means the tree is
empty.
Sub-trees: If the root node R is not NULL, then the trees T1, T2, and T3 are called the sub-trees of
R.
Leaf node: A node that has no children is called the leaf node or the terminal node.
Path: A sequence of consecutive edges is called a path. For example, in Fig. 9.1, the path from
the root node A to node I is given as: A, D, and I.
Ancestor node(Parent Node): An ancestor of a node is any predecessor node on the path from root
to that node. The root node does not have any ancestors. In the tree given in Fig. 9.1, nodes A, C, and
G are the ancestors of node K.
Descendant node(Child Node): A descendant node is any successor node on any path from the node
to a leaf node. Leaf nodes do not have any descendants. In the tree given in Fig. 9.1, nodes C, G, J,
and K are the descendants of node A.
Level number: Every node in the tree is assigned a level number in such a way that the root node is
at level 0, children of the root node are at level number Thus, every node is at one level higher than
its parent. So, all child nodes have a level number given by parent’s level number + 1.
Degree: Degree of a node is equal to the number of children that a node has.
The degree of a leaf node is zero.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Tree Data Structures: A Comprehensive Guide with Examples and more Schemes and Mind Maps Computer Science in PDF only on Docsity!

Chapter 0 5

Tree

Definition:

A non-linear data structure called tree. A tree is a structure which is mainly used to store data that is hierarchical in nature.

01 Tree Terminology:

Root node : The root node R is the topmost node in the tree. If R = NULL, then it means the tree is empty.  Sub-trees: If the root node R is not NULL, then the trees T1, T2, and T3 are called the sub-trees of R.  Leaf node: A node that has no children is called the leaf node or the terminal node.  Path : A sequence of consecutive edges is called a path. For example, in Fig. 9 .1, the path from the root node A to node I is given as: A, D, and I.  Ancestor node(Parent Node) : An ancestor of a node is any predecessor node on the path from root to that node. The root node does not have any ancestors. In the tree given in Fig. 9.1, nodes A, C, and G are the ancestors of node K.  Descendant node(Child Node) : A descendant node is any successor node on any path from the node to a leaf node. Leaf nodes do not have any descendants. In the tree given in Fig. 9.1, nodes C, G, J, and K are the descendants of node A.  Level number: Every node in the tree is assigned a level number in such a way that the root node is at level 0, children of the root node are at level number Thus, every node is at one level higher than its parent. So, all child nodes have a level number given by parent’s level number + 1.  Degree: Degree of a node is equal to the number of children that a node has. The degree of a leaf node is zero.

o In-degree : In-degree of a node is the number of edges arriving at that node. o Out-degree : Out-degree of a node is the number of edges leaving that node.  Depth: A tree is the maximum level of any node in a given tree. This is number of level one can descend the tree from its root node to the terminal nodes (leaves). TYPES OF TREES: Trees are of following 6 types:

  1. General trees
  2. Forests
  3. Binary trees
  4. Binary search trees
  5. Expression trees General Trees: General trees are data structures that store elements hierarchically. The top node of a tree is the root node and each node, except the root, has a parent. A node in a general tree (except the leaf nodes) may have zero or more sub-trees. Forests:

nodes are arranged in an order. Expression Trees Binary trees are widely used to store algebraic expressions. For example, consider the algebraic expression given as: Exp = (a – b) + (c * d) This expression can be represented using a binary tree as shown in Fig. 9.13.

02 Tree Representation:

In the computer’s memory, a binary tree can be maintained either by using a linked representation or by using a sequential representation.

Linked representation of binary trees In the linked representation of a binary tree, every node will have three parts: the data element, a pointer to the left node, and a pointer to the right node. So in C, the binary tree is built with a node type given below. struct node { struct node *left; int data; struct node *right; }; Every binary tree has a pointer ROOT, which points to the root element (topmost element) of the tree. If ROOT = NULL, then the tree is empty. Consider the binary tree given in Fig. 9.3. The schematic diagram of the linked representation of the binary tree is shown in Fig. 9.9. In Fig. 9.9, the left position is used to point to the left child of the node or to store the address of the left child of the node. The middle position is used to store the data. Finally, the right position is used to point to the right child of the node or to store the address of the right child of the node. Empty sub-trees are represented using X (meaning NULL). Look at the tree given in Fig. 9.10. Note how this tree is represented in the main memory using a linked list (Fig. 9.11).

03 Tree Traversal methods:

Traversing a binary tree is the process of visiting each node in the tree exactly once in a systematic way. Unlike linear data structures in which the elements are traversed sequentially, tree is a nonlinear data structure in which the elements can be traversed in many different ways. There are different algorithms for tree traversals. 1 Pre-order Traversal 2 In-order Traversal 3 Post-order Traversal 1 Pre-order Traversal: To traverse a non-empty binary tree in pre-order, the following operations are performed recursively at each node. The algorithm works by:

  1. Visiting the root node,
  2. Traversing the left sub-tree, and finally
  3. Traversing the right sub-tree. Algorithm: PREORDER() Input: ROOT Output: NODE_VISIT_ORDER

Example In Figs (a) and (b), find the sequence of nodes that will be visited using pre-order traversal algorithm. Solution TRAVERSAL ORDER (a) : A, B, D, G, H, L, E, C, F, I, J, and K TRAVERSAL ORDER (b) : A, B, D, C, D, E, F, G, H, and I 2 In-order Traversal: To traverse a non-empty binary tree in in-order, the following operations are performed recursively at each node. The algorithm works by:

  1. Traversing the left sub-tree,
  2. Visiting the root node, and finally
  3. Traversing the right sub-tree. Algorithm: INORDER() Input: ROOT Output: NODE_VISIT_ORDER

Example For the trees given in above, give the sequence of nodes that will be visited using post-order traversal algorithm. TRAVERSAL ORDER (A): G, L, H, D, E, B, I, K, J, F, C, and A TRAVERSAL ORDER (B): D, B, H, I, G, F, E, C, and A

04 AVL search tree:

AVL tree is a self-balancing binary search tree invented by G.M. Adelson-Velsky and E.M. Landis in 1962. The tree is named AVL in honour of its inventors. In an AVL tree, the heights of the two sub-trees of a node may differ by at most one. Due to this property, the AVL tree is also known as a height-balanced tree. In its structure, it stores an additional variable called the BalanceFactor. Thus, every node has a balance factor associated with it. The balance factor of a node is calculated by subtracting the height of its right sub-tree from the height of its left sub-tree. Balance factor = Height (left sub-tree) – Height (right sub-tree)

 If the balance factor of a node is 1, then it means that the left sub-tree of the tree is one level higher than that of the right sub-tree. Such a tree is therefore called as a left-heavy tree.  If the balance factor of a node is 0, then it means that the height of the left sub-tree (longest path in the left sub-tree) is equal to the height of the right sub-tree.  If the balance factor of a node is – 1, then it means that the left sub-tree of the tree is one level lower than that of the right sub-tree. Such a tree is therefore called as a right-heavy tree. Operations on AVL Trees: 01 Inserting a New Node in an AVL Tree: Insertion in an AVL tree is also done in the same way as it is done in a binary search tree. In the AVL tree, the new node is always inserted as the leaf node. But the step of insertion is usually followed by an additional step of rotation. Rotation is done to restore the balance of the tree. However, if insertion of the new node does not disturb the balance factor, that is, if the balance factor of every node is still – 1, 0, or 1, then rotations are not required.

02 Deleting a Node from an AVL Tree: Deletion of a node in an AVL tree is similar to that of binary search trees. But it goes one step ahead. Deletion may disturb the AVLness of the tree, so to rebalance the AVL tree, we need to perform rotations. There are two classes of rotations that can be performed on an AVL tree after deleting a given node. These rotations are R rotation and L rotation.

A B tree is designed to store sorted data and allows search, insertion, and deletion operations to be performed in logarithmic amortized time. A B tree of order m (the maximum number of children that each node can have) is a tree with all the properties of an M-way search tree. In addition it has the following properties:

  1. Every node in the B tree has at most (maximum) m children.
  2. Every node in the B tree except the root node and leaf nodes has at least (minimum) m/2 children. This condition helps to keep the tree bushy so that the path from the root node to the leaf is very short, even in a tree that stores a lot of data.
  3. The root node has at least two children if it is not a terminal (leaf) node.
  4. All leaf nodes are at the same level. B Tree Order 4 (Max size 3) Example Create a B tree of order 6 by inserting the following elements: 3, 14, 7, 1, 8, 5, 11, 17, 13, 6, 23, 12, 20, 26, 4, 16, 18, 24, 25, and 19. Insert 30 Insert 14 Insert 7 Insert 1 Insert 8 Insert 5 Insert 11

Delete 14 06 B+ Tree: A B+ tree is a variant of a B tree which stores sorted data in a way that allows for efficient insertion, retrieval, and removal of records, each of which is identified by a key. While a B tree can store both keys and records in its interior nodes, a B+ tree, in contrast, stores all the records at the leaf level of the tree. B+ Tree Order 4 (Max size 3) Example Create a B+ tree of order 6 by inserting the following elements: 3, 14, 7, 1, 8, 5, 11, 17, 13, 6, 23, 12, 20, 26, 4, 16, 18, 24, 25, and 19. Insert 3 Insert 14 Insert 7 Insert 1

  • Insert
  • Insert
  • Insert
  • Insert
  • Insert
  • Insert
  • Insert
  • Insert
  • Insert
  • Insert
  • Insert
  • Insert
  • Insert
  • Delete
  • Delete
  • Delete
  • Insert
  • Insert
  • Insert
  • Insert
  • Insert
  • Insert
  • Insert
  • Insert
  • Insert