Sei sulla pagina 1di 5


Tree Structures

Tree ADT - Terminologies Tree traversals a. Preorder Traversal b. Inorder Traversal c. Post order Traversal Left child right sibling data structures for general trees Binary Tree ADT Types of Binary Tree 1. Left Skewed Binary Tree 2. Right Skewed Binary Tree 3. Complete Binary Tree 4. Full Binary Tree 5. Extended Binary Tree Representation or Implementation of Binary Tree 1. Linear Implementation or Sequential Implementation (using Array) 2. Linked Implementation (using Pointers i.e Linked List) Applications of trees Expression trees Binary search tree ADT a. Insert b. Delete c. Search AVL trees Four Rotations Single Rotation LL & RR Double Rotation LR & RL Binary heaps Priority Queue

Define tree. Trees are non linear data structure, which is used to store data items in a shorted sequence. It represents any hierarchical relationship between any data item. It is a collection of nodes, Which has a distinguish node called the root and zero or more nonempty subtrees T1,T2, Tk. each of which are connected by a directed edge from the root. Define Height of the tree?

The height of n is the length of the longest path from root to a leaf. Thus all leaves have height zero. The height of a tree is equal to a height of a root. Define Depth of tree? For any node n, the depth of n is the length of the unique path from the root to node n. Thus for a root the depth is always zero. Define Degree of a node It is the number of sub trees of a node in a given tree. Define Degree of a tree It is the maximum degree of a node in a given tree. Define Terminal node or leaf Nodes with no children are known as leaves. A leaf will always have degree zero and is also called as terminal node. Define Non-terminal node Any node except the root node whose degree is a non-zero value is called as a nonterminal node. Non-terminal nodes are the intermediate nodes in traversing the given tree from its root node to the terminal node. Define sibling Nodes with the same parent are called siblings. Define binary tree A Binary tree is a finite set of data items which is either empty or consists of a single item called root and two disjoin binary trees called left sub tree max degree of any node is two. Define expression tree Expression tree is also a binary tree in which the leafs terminal nodes or operands and non-terminal intermediate nodes are operators used for traversal. Construction of expression trees 1. Convert the given infix expression into postfix notation 2. Create a stack and read each character of the expression and push into the stack, if operands are encountered. 3. When an operator is encountered pop 2 values from the stack. From a tree using the operator. Define AVL tree AVL tree also called as height balanced tree .It is a height balanced tree in which every node will have a balancing factor of 1,0,1 Balancing factor Balancing factor of a node is given by the difference between the height of the left sub tree and the height of the right sub tree.

What are the various operation performed in the binary search tree ? 1.insertion 2.deletion 3.find 4.find min 5.find max What are the types of rotation? There are four types of rotations, in which two of them are the mirror images of the other two rotations. The four rotations are. Single right rotation or R-rotation Single left rotation or L-rotation Double left-right rotation or LR-rotation Double right-left rotation or RL-rotation What is priority queue? A priority queue is a data structure that allows at least the following two operations: insert which does the obvious thing; and Deletemin, which finds, returns, and removes the minimum element in the priority queue. The Insert operation is the equivalent of Enqueue Application of priority queues 1. for scheduling purpose in operating system 2. used for external sorting 3. important for the implementation of greedy algorithm, which operate by repeatedly finding a minimum. What are the main properties of a binary heap? 1.structure property 2.heaporder property Define tree traversal and mention the type of traversals? Visiting of each and every node in the tree exactly is called as tree traversal. Three types of tree traversal 1.inorder traversal 2.preoder traversal 3.postorder traversal. Define AVL trees and who was it invented by? An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of the nodes left and right subtrees, is either 0 or +1 or 1. the height of an empty subtree is defined as 1. AVL trees were invented in 1962 by two Russian scientists, G.M.Adelson-Velsky and E.M.Landis, after whom the data structure is named.

What are binary search trees and what is it mainly used for? Binary search tree is one of the principal data structures for implementing dictionaries. It is a binary tree whose nodes contain elements of a set of orderable items, one element per node, so that all elements in the left subtree are smaller than the element in the subtrees root and all elements in the right subtree are greater than it. What is a rotation in AVL tree used for? If an insertion of a new node makes an AVL tree unbalanced, the tree is transformed by a rotation. A rotation in an AVL tree is a local transformation of its subtree rooted at a node whose balance has become either +2 or 2; if there are several such nodes, then the tree rooted at the unbalanced node that is closest to the newly inserted leaf is rotated. What are the drawbacks of AVL trees? The drawbacks of AVL trees are . Frequent rotations .The need to maintain balances for the trees nodes. Overall complexity, especially of the deletion operation. What is a heap? A heap is a partially ordered data structure, and can be defined as a binary tree assigned to its nodes, one key per node, provided the following two conditions are met .The trees shape requirement-The binary tree is essentially complete, that is all the leaves are full except possibly the last level, where only some rightmost leaves will be missing. The parental dominance requirement-The key at each node is greater that or equal to the keys of its children What is the main use of heap? Heaps are especially suitable for implementing priority queues. Priority queue is a set of items with orderable characteristic called an items priority, with the following operations. Finding an item with the highest priority Deleting an item with highest priority Adding a new item to the set Give three properties of heaps? The properties of heap are There exists exactly one essentially complete binary tree with n nodes. Its height is equal to log2n . The root of the heap is always the largest element What is a min-heap? A min-heap is a mirror image of the heap structure. It is a complete binary tree in which every element is less than or equal to its children. So the root of the min-heap contains the smallest element.

What is a binary tree extension and what is its use? The binary tree extension can be drawn by replacing the empty subtrees by special nodes in a binary tree. The extra nodes shown as little squares are called external & the original nodes shown as little circles called internal. The extension of a empty binary tree is a single external node. The binary tree extension helps in analysis of tree algorithms. A full binary tree (sometimes proper binary tree or 2-tree) is a tree in which every node other than the leaves has two children A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. Differentiate Complete binary tree vs full binary tree? A full tree is a tree where all nodes except the leaves have the maximum number of children. For a BST, that would be two children per node. A complete tree is the same thing, except that the bottom level does not need to be full. It can be missing leaf nodes, however the ones present must be shifted to the left. Skewed tree search The worst possible binary tree structure is one that is completely skewed either to the left (no node has a right child) or to the right (no node has a left child). This happens when the values to be inserted arrive in descending or ascending order, respectively. Searching in such a tree degenerates to a sequential linear search of O(N).