


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 26
This page cannot be seen from the preview
Don't miss anything!
A non-linear data structure called tree. A tree is a structure which is mainly used to store data that is hierarchical in nature.
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:
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.
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).
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:
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:
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
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:
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