Data Structures Fakhar lodhi Chapter 5: Trees
|
|
- Stewart Jennings
- 6 years ago
- Views:
Transcription
1 Chapter 5 Trees Introduction Linked-lists are essentially used to store and organize data where the relationship among data is linear or one-dimensional (a circular structure can be considered as a special case of a linear structure). A lot of data is however non-linear in nature. Examples of such data include hierarchical data organizations such as lineal charts, file directories, and table of contents of a book. Figure shows one such hierarchy which presents a section of the family tree of perhaps the most famous Arab tribe Banu- Quresh, highlighting lineage of the Holy Prophet Muhammad (PBUH). Such hierarchical organizations are very common and cannot be expressed with linear lists. We therefore need other data structures to model such organizations. Tree is perhaps the most important non-linear data structure with numerous applications in computing and other fields. This chapter is dedicated to the discussion of tree data structure. 5.1 Tree some definitions The elements of a tree are called nodes. A tree consists of a finite set of nodes and links. A link connects two nodes of a tree. Two nodes are said to be adjacent if they are directly connected by a link. A path is a sequence of adjacent nodes. Every node is connected to every other node in the tree through a unique path. A tree which does not have any node is called an empty tree. Our focus of interest is a special kind of tree called the ed tree. A ed tree is a non-empty tree which has a specially designated node called the node. By convention, ed trees are shown to grow downwards with the node shown at the top and serves as start or the entry point in to the tree. All other nodes are said to be descendents of the node. The direct descendents (nodes that have a direct link) of a node are called its children and the node itself is called the parent of those nodes. Children of the same node are called siblings. The node does not have any parent. Every other node has exactly one parent. A node may have 0 or Page 1 of 16
2 Haris Zubair Abu Talib Auf Husais Sehm Taiem Mugheera Taiem Zahra Mutlib Abdus Shams Naufil Muhammad (PBUH) Asad Nuzlah Luayy Qusayy Quresh Ghalib Muharib Ka'ab Amir Hirs Murrah Jamha Adi Kilab Makhzoom Abd Munaf Abdud Dar Abdul Uza Hashim Abu Amr Abu Ubaida Abdul Mutlib Aba' Saifi Abdullah Musa'ab Abu Lahab Maqoom Hajl Abbas Mugheera Hamza Zarrar Data Structures Fakhar lodhi Chapter 5: Trees Page 2 of 16
3 more children. A node with zero children is called a leaf node. Nodes other than the leaf nodes and the node are called internal or intermediate nodes. That is, an intermediate node has exactly one parent and at least one child. A subset of a tree that can be viewed as a complete tree in itself is called a subtree. That is, a node with all its descendents is a subtree. In other words, every node in a tree can be seen as the node of the subtree ed at that node. The subtree corresponding to the node is the entire tree and hence a tree is defined and identified by its node. Figure shows a ed tree with A with nodes B, F, and K as its children/subtrees. Nodes B, F, K, C, D, and X are internal nodes whereas H, L, Q, G, M, I, N, and P are leaf nodes. Degree of a node is defined as the number of children of that node. In Figure, nodes H, L, Q, G, M, I, N, and P have degree 0, nodes F and X have degree 1, nodes B, K, and D have degree 2, and A and C have degree 3. Level of a node is defined as the distance of the node from the node. In Figure A is at level 0, B, F, and K are at level 1, C, H, D, L and X are level 2, and the rest are at level 3. Height of a tree is defined as the maximum level of any node in the tree 1. Height of tree in Figure is 3. 1 Definition of height and level is not standardized and may slightly vary from one text to the other. Page 3 of 16
4 5.2 Binary tree A binary tree is a special kind of ed tree with the property that all nodes in a binary tree have at most two children. That is, the maximum degree of a node in a binary tree is 2. The two children of a node in a binary tree are usually designated as the left and right child. A binary tree can be defined recursively as the following: a) A binary tree is either empty or non-empty b) If the binary tree is non-empty then 1. There is a node 2. The node has two children, the left child and the right child. These children correspond to two subtrees the left subtree and the right subtree. 3. Each subtree is a binary tree. Figure (a) and (b) are examples of binary trees. In Figure (a), B is the left child of A and H is the right child of B. It can be very easily seen that the maximum number of nodes on level i of a binary tree is 2 i and the maximum number of nodes in a binary tree of height k is 2 k+1 1. A binary tree of height k having 2 k+1 1 nodes is called a full binary tree. A binary tree is a full binary tree if and only if all leaf nodes are at the same level and all other nodes have degree 2. Figure shows a full binary tree of height 4 with 15 nodes in it. Page 4 of 16
5 A complete binary tree is a binary tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right. Another way to determine whether a tree is complete or not is to assign numbers to all its nodes as follows: 1. node is assigned number for a node numbered i, assign 2*i and 2*i + 1 to its left and right children respectively. then the tree is a complete binary tree iff the maximum number thus assigned is equal to the number of nodes in the tree. Note that full binary tree is also a complete binary tree. Figure (a) is an example of a complete binary tree whereas the tree in Figure (b) is not complete. Page 5 of 16
6 5.3 Binary tree implementation using linked structures Binary trees can be easily implemented using dynamically created nodes which are linked through pointers. As shown in Figure, a binary tree node structure is quite similar to that of a doubly linked-list instead of having pointers to the next and previous nodes, in this case, we have pointers to the left and right children of the node. Some implementations also add a pointer to the parent node but in most cases this is not required. struct TreeNode { T data; TreeNode<T> *left, *right; ; We can now use this node structure to define our binary tree whose specification is shown in Figure class BinaryTree { public: BinaryTree(){ = NULL; // create an empty binary tree void buildbinarytree( T data, BinaryTree<T> &leftsubtree, BinaryTree<T> &rightsubtree); /************************************************************* This method can be used to build a binary tree bottoms up. The modified tree is ed at a node with the input data and its left and right children are made-up of the left and right subtrees passed as input parameters. The left and right subtrees passed as input lose their contents and their s are set to NULL. **************************************************************/ ~BinaryTree(){clear(); void inorder(){inorder(); // in-order traversal void preorder(){inorder(); // pre-order traversal void postorder(){inorder(); // post-order traversal BinaryTree(const BinaryTree & bt); // copy constructor const BinaryTree & operator=(const BinaryTree & rhs); // assignment operator private: TreeNode <T> *; friend class LNRIterator<T>; // in-order iterator TreeNode<T> * createtreenode(t data); void clear(treenode<t> *t); // cleanup tree void inorder(treenode<t> *t); // in-order workhorse void preorder(treenode<t> *t); // pre-order workhorse void postorder(treenode<t> *t); // post-order workhorse ; left data right Page 6 of 16
7 5.3.1 Building a binary tree Figure shows the methods to build the binary from bottom to top. TreeNode<T> * BinaryTree<T> :: createtreenode(t data) { TreeNode<T> *t = new TreeNode<T>; if (t!= NULL) { t->data = data; t->left = t->right = NULL; return t; else throw OUT_OF_MEMORY; void BinaryTree<T> :: buildbinarytree(t Data,BinaryTree<T> &leftsubtree, BinaryTree<T> &rightsubtree) { // create a new tree node and store data in it TreeNode<T> *t = createtreenode(data); // make the left and right of the new node point to the s // of the left and right sub-trees respectively t->left = leftsubtree->; t->right = rightsubtree->; // assign NULL to s of the input trees so that the nodes // that have now been shifted to the target tree are not // destroyed when the destructor is called for these trees. leftsubtree-> = NULL; rightsubtree-> = NULL; clear(); = t; // return any existing nodes in the tree to heap // make the new node the of the target tree There are a couple of points to be noted. 1. This method detaches the nodes present in the trees passed as input parameters and attaches those nodes to the tree which we are building from these sub-trees. Hence these trees are effectively destroyed by assigning NULL value to their s. 2. The second last statement in the method, clear(), is very important. It essentially destroys the target tree by returning any nodes attached to back to heap. Failing to do so could create garbage. Page 7 of 16
8 Figure (b to f) demonstrates how this method could be used to build the tree of Figure.(a) (a) t1 (b) NULL NULL NULL t2 nulltree BinaryTree<int> t1, t2, nulltree; (c) t1 1 3 t2 t1.buildtree(1,nulltree, nulltree); t2.buildtree(3,nulltree, nulltree); (d) t NULL t2 t1.buildtree(2,t1, t2); t1 4 t2 NULL 2 5 (e) t2 5 t2.buildtree(5, nulltree, nulltree); (f) 1 3 t1.buildtree(4,t1, t2); Page 8 of 16
9 5.3.2 Binary tree traversal It is often required to visit (access) each node in the tree and process data present in there. For that purpose some tree traversal algorithm is used which provides a systematic approach to access all the nodes in the tree. A complete traversal generates a linear order of all the nodes in the tree. As described in section 5.2, a non-empty tree is defined by the node pointed to by its and its two children the left subtree and the right subtree. These two subtrees are also trees ed at the left and right child of the parent node. A traversal algorithm can be developed easily by making use of this recursive definition. L Left subtree N - Node R Right subtree NLR process the node, then traverse the left subtree, then traverse the right subtree NRL process the node, then traverse the right subtree, then traverse the left subtree LNR traverse the left subtree, then process the node, then traverse the right subtree LRN traverse the left subtree, then traverse the right subtree, then process the node RNL traverse the right subtree, process the node, then then traverse the left subtree RLN traverse the right subtree, then traverse the left subtree, then process the node Let N, L, and R denote node, left subtree, and right subtree of a tree. As shown in Figure, these three letters (N, L, and R) can be arranged in six different permutations NLR, NRL, LNR, LRN, RNL, and RLN. These six permutations correspond to six different traversals of the binary tree. For example, LNR corresponds to a traversal in which we complete the tree traversal by first traversing the left subtree, then visiting the node, and then traversing the right subtree. If we always traverse left before right then we are left with three possible traversal NLR, LNR, and LRN. These three are called pre-order, in-order, and post-order respectively. C++ code for in-order and pre-order workhorse functions is given in Figure void BinaryTree<T> :: inorder(treenode<t> *t) { if (t!= NULL) { inorder(t->left); process(t); inorder(t->right); void BinaryTree<T> :: preorder(treenode<t> *t) { if (t!= NULL) { process(t); preorder(t->left); preorder(t->right); Page 9 of 16
10 The in-order, pre-order, and post-order traversals can be generated by drawing the tree and just walking around it starting from the left of the and ending at the right of it. For each node we note when we are on its left, under it, and on its right. As shown in Figure, the pre-order, inorder, and post-order traversals are generated by visiting each node when we are on the left, under, and right of it respectively. A B K Q C H L X M P NLR visit when at the left of the Node LNR visit when under the Node LRN visit when at the right of the Node A B C Q M H K LX P Q C M B H A L KX P Q M C H B L P XK A Figure shows the three tree traversals for an expression tree. Note that the in-order, pre-order, and post-order traversals generate the expression in infix, prefix, and postfix respectively. - + / A * D * B C E F LNR: A + B * C D / E * F NLR: + A * B C / D * E F LRN: A B C * + D E F * / Page 10 of 16
11 The destructor As shown is Figure, the binary tree destructor uses clear function. As can be seen easily, clear requires a post-order traversal of the tree. The resulting code is given in Figure void BinaryTree<T> :: clear(treenode<t> *t) { if (t!= NULL) { clear(t->left); clear(t->right); delete t; Exercise 1. write the copy constructor 2. write the assignment operator 3. write a member function to count the number of nodes in the tree 4. write a member function to count the leaf nodes 5. write a member function to count the nodes with degree 1 6. write a member function to count nodes with degree 2 7. write a member function to calculate the height of the tree 8. write a member function to make a mirror image of the tree 9. write a member function to determine the presence of a key in the tree. 10. write a function to determine the level of the node containing a given key. Return -1 if the key is not present. 11. write a member function == which returns true if the two trees are equal. 12. write a member function that takes a key and prints the entire subtree ed at the node that contains the key. 13. write a member function that takes a key and prints the entire subtree ed at the sibling of the node that contains the key. 14. write a member function that deletes all the leaf nodes from the tree. Page 11 of 16
12 15. write a member function that deletes all the nodes with degree 1 from the tree. 16. write a member function that takes two keys as input and inserts a node with second key as the left most descendent of the node with the first key if it the first key present in the tree making the new node the first node to be visited in in-order in the tree ed at the node that contains the first key. 17. write a member function that takes two keys as input and inserts a node with second key as the right most descendent of the node with the first key if it the first key present in the tree making the new node the last node to be visited in in-order in the tree ed at the node that contains the first key. Page 12 of 16
13 5.4 Tree iterators Just like the linked-list class, we need tree iterators that would allow us to access each element of the tree in some order so that we can process data according to our specific need. Since, for each node reached through the iterator we process the data in the node and then request for the next node, we cannot use recursion because once we return from that function, we cannot restart the recursion from that node the context (addresses of the all the ancestors stored on to the implicit stack) would have been lost. We would therefore need an iterative solution where, in order to remember the context, an explicit stack needs to be employed. Following this line of thought, an in-order iterator for the binary tree class is developed. The iterator supports the four basic functions create, isdone, begin, next, and getdata and is presented in Figure. class LNRIterator { private: const BinaryTree<T> &tree; TreeNode<T> *current; Stack<TreeNode<T> *> stk; // the associated tree // pointer to the current node // used for storing those // ancestors of current that are // yet to be visited TreeNode<T> * gototheleftmostdescendent(treenode<t> *t); // given a node, go its leftmost descendent // to be used in begin() and next() public: LNRIterator(const BinaryTree<T> &bt):tree(bt), current(null){ bool isdone() {return current == NULL; T getdata() { if (!isdone()) return current->data; else throw ILLEGAL_REFERNCE_EXCEPTION; ; void begin(); // takes current to the first node to be visited void next(); // moves current to the next node in order The constructor, isdone, and getdata are trivial and do not need elaboration. We will therefore only focus on begin and next which are given in Figure. Page 13 of 16
14 TreeNode<T> * LNRIterator<T> :: gototheleftmostdescendent(treenode<t> *t) { TreeNode<T> *temp = t; while(temp->left!= NULL) { stk.push(temp); temp = temp->left; return temp; void LNRIterator<T> :: begin() { stk.clear(); stk.push(null); current = ; if (!= NULL) // if the tree is not empty current = gototheleftmostdescendent(); Begin sets the value of the current to the first node to be visited in order. Starting from the, it gets to the left-most child of the by keeping moving to the left until by a node is reached whose left child is not present. Before moving to the left, address of the node is saved so that it can be used later for visiting the node and then traversing its right subtree. It may be noted that begin also initializes the stack by clearing it of its contents and puts NULL on to it. This will be used to set the value of current to NULL when the iterator goes beyond the last node. After visiting a node, the in-order traversal of the right subtree by moving to its right child and then to the leftmost descendent of the right child and saving the address of the nodes encountered in the process. This is like repeating the process similar to begin for the tree ed at the right child of a node. Once the traversal on the right is complete, this means the entire subtree ed at that node is complete and hence current moves back one step in the hierarchy by popping the address of its ancestor. The process continues until NULL is popped from the stack and assigned to current, indicating end of traversal. The code for the next() operation is given in Figure void LNRIterator<T> :: next() { if (current == NULL) throw ILLEGAL_REFERENCE_EXCEPTION; if (current->right!= NULL) { current = gototheleftmostdescendent(current->right); else current = stk.pop(); Page 14 of 16
15 We can now use this iterator to for our specific purpose as shown below: BinaryTree<int> bintree; // add data to the tree LNRIterator<int> myiterator(bintree); for(myiterator.begin(); myiterator.isdone(); myiterator.next()) cout << getdata(); Exercise: 1. Write pre-order iterator. 2. write post-order iterator. 3. write iterative stack-based in-order traversal 4. write iterative stack-based pre-order traversal 5. write iterative stack-based post-order traversal 6. write a member function of the in-order iterator class to add a new node as the leftmost descendent of the current node. 7. Write a function to delete the leftmost descendent of the current class. Warning: the node to be deleted may have a right child which must be preserved. 8. write a member function of the in-order iterator class to add a new node as the rightmost descendent of the current node. 9. Write a function to delete the rightmost descendent of the current class. Warning: the node to be deleted may have a left child which must be preserved. Page 15 of 16
16 5.5 Level-order traversal Level-order traversal traverses the tree level by level. That is, it traverses all the nodes at level i before moving to nodes at level i+1. Such a traversal is also called depth-first traversal. Traditionally, level-order traversal is performed top-to-bottom and left-to-tight. For example, level-order traversal of the tree of Figure will visit nodes in the following order: A B K C H L X Q M P Level-order traversal can be implemented by using a queue. A node at level i would have its children at level i+1. Therefore, after visiting a node, if its children were added to the queue, then they could be retrieved from the queue and visited in their order of insertion, resulting in levelorder traversal. We start by inserting the node to the queue. Then we remove a node from the queue, visit it and put its children, if any, on the queue. The process continues until there is nothing left on the queue. The code for level-order traversal is presented in Figure void BinaryTree<T> :: levelorder() { if (!= NULL) { TreeNode<T> *t; Queue<TreeNode<T> *> que; que.add(); while(!que.isempty()) { t = que.remove(); visit(t); if (t->left!= NULL) que.add(t->left); if (t->right!= NULL) que.add(t->right); Exercise: 1. Write a function that takes a number as input and prints all the nodes present at that level. 2. Write level-order iterator. 3. Write a recursive function to achieve the task specified in Q. 1 Page 16 of 16
Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology
Trees : Part Section. () (2) Preorder, Postorder and Levelorder Traversals Definition: A tree is a connected graph with no cycles Consequences: Between any two vertices, there is exactly one unique path
More informationBinary Tree Implementation
Binary Tree Implementation Lecture 31 Sections 12.2-12.3 Robb T. Koether Hampden-Sydney College Mon, Apr 5, 2010 Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 1 /
More informationBinary Trees
Binary Trees 4-7-2005 Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? What is a Tree? You are all familiar with what
More informationCOMP : Trees. COMP20012 Trees 219
COMP20012 3: Trees COMP20012 Trees 219 Trees Seen lots of examples. Parse Trees Decision Trees Search Trees Family Trees Hierarchical Structures Management Directories COMP20012 Trees 220 Trees have natural
More informationComputer Science 210 Data Structures Siena College Fall Topic Notes: Trees
Computer Science 0 Data Structures Siena College Fall 08 Topic Notes: Trees We ve spent a lot of time looking at a variety of structures where there is a natural linear ordering of the elements in arrays,
More informationBinary Trees. Height 1
Binary Trees Definitions A tree is a finite set of one or more nodes that shows parent-child relationship such that There is a special node called root Remaining nodes are portioned into subsets T1,T2,T3.
More informationTopic 14. The BinaryTree ADT
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary tree implementation Examine a binary tree
More informationAlgorithms and Data Structures
Lesson 3: trees and visits Luciano Bononi http://www.cs.unibo.it/~bononi/ (slide credits: these slides are a revised version of slides created by Dr. Gabriele D Angelo) International
More informationBinary Trees. Reading: Lewis & Chase 12.1, 12.3 Eck Programming Course CL I
inary Trees Reading: Lewis & hase 12.1, 12.3 Eck 9.4.1 Objectives Tree basics Learn the terminology used when talking about trees iscuss methods for traversing trees iscuss a possible implementation of
More informationTrees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.
Trees Q: Why study trees? : Many advance DTs are implemented using tree-based data structures. Recursive Definition of (Rooted) Tree: Let T be a set with n 0 elements. (i) If n = 0, T is an empty tree,
More information[ DATA STRUCTURES ] Fig. (1) : A Tree
[ DATA STRUCTURES ] Chapter - 07 : Trees A Tree is a non-linear data structure in which items are arranged in a sorted sequence. It is used to represent hierarchical relationship existing amongst several
More informationCSI33 Data Structures
Outline Department of Mathematics and Computer Science Bronx Community College November 13, 2017 Outline Outline 1 C++ Supplement.1: Trees Outline C++ Supplement.1: Trees 1 C++ Supplement.1: Trees Uses
More informationData Structures and Algorithms
Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk Tree? In computer science, a tree is an abstract model of a hierarchical structure A tree is a finite set of one or more nodes
More informationData Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD
Data Structures Trees By Dr. Mohammad Ali H. Eljinini Trees Are collections of items arranged in a tree like data structure (none linear). Items are stored inside units called nodes. However: We can use
More informationTrees, Binary Trees, and Binary Search Trees
COMP171 Trees, Binary Trees, and Binary Search Trees 2 Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
More information8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))
DHANALAKSHMI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING EC6301 OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES UNIT IV NONLINEAR DATA STRUCTURES Part A 1. Define Tree [N/D 08]
More informationBinary Tree Implementation
Binary Tree Implementation Lecture 32 Section 19.1 Robb T. Koether Hampden-Sydney College Mon, Apr 16, 2018 Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 16, 2018 1 / 24
More informationCE 221 Data Structures and Algorithms
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, 4.1 4.2 Izmir University of Economics 1 Preliminaries - I (Recursive) Definition: A tree is a collection of nodes. The
More informationGarbage Collection: recycling unused memory
Outline backtracking garbage collection trees binary search trees tree traversal binary search tree algorithms: add, remove, traverse binary node class 1 Backtracking finding a path through a maze is an
More informationData Structure. IBPS SO (IT- Officer) Exam 2017
Data Structure IBPS SO (IT- Officer) Exam 2017 Data Structure: In computer science, a data structure is a way of storing and organizing data in a computer s memory so that it can be used efficiently. Data
More informationBinary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals
Binary Trees 1 Binary Tree Node Relationships 2 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the
More informationBinary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management
Binary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from
More informationTopic Binary Trees (Non-Linear Data Structures)
Topic Binary Trees (Non-Linear Data Structures) CIS210 1 Linear Data Structures Arrays Linked lists Skip lists Self-organizing lists CIS210 2 Non-Linear Data Structures Hierarchical representation? Trees
More informationWhy Do We Need Trees?
CSE 373 Lecture 6: Trees Today s agenda: Trees: Definition and terminology Traversing trees Binary search trees Inserting into and deleting from trees Covered in Chapter 4 of the text Why Do We Need Trees?
More informationtree nonlinear Examples
The Tree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary tree implementation Examine a binary tree example 10-2
More information1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.
Unit 6: Binary Trees Part 1 Engineering 4892: Data Structures Faculty of Engineering & Applied Science Memorial University of Newfoundland July 11, 2011 1 Binary trees 1 Binary search trees Analysis of
More informationTrees. A tree is a directed graph with the property
2: Trees Trees A tree is a directed graph with the property There is one node (the root) from which all other nodes can be reached by exactly one path. Seen lots of examples. Parse Trees Decision Trees
More informationTREES Lecture 10 CS2110 Spring2014
TREES Lecture 10 CS2110 Spring2014 Readings and Homework 2 Textbook, Chapter 23, 24 Homework: A thought problem (draw pictures!) Suppose you use trees to represent student schedules. For each student there
More informationBinary Trees, Binary Search Trees
Binary Trees, Binary Search Trees Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search, insert, delete)
More informationMID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.
MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the elements may locate at far positions
More informationData Structure - Binary Tree 1 -
Data Structure - Binary Tree 1 - Hanyang University Jong-Il Park Basic Tree Concepts Logical structures Chap. 2~4 Chap. 5 Chap. 6 Linear list Tree Graph Linear structures Non-linear structures Linear Lists
More informationMIDTERM EXAMINATION Spring 2010 CS301- Data Structures
MIDTERM EXAMINATION Spring 2010 CS301- Data Structures Question No: 1 Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the
More informationTrees: examples (Family trees)
Ch 4: Trees it s a jungle out there... I think that I will never see a linked list useful as a tree; Linked lists are used by everybody, but it takes real smarts to do a tree Trees: examples (Family trees)
More informationChapter 20: Binary Trees
Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other
More informationMULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR
STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM
More informationAbout This Lecture. Trees. Outline. Recursive List Definition slide 1. Recursive Tree Definition. Recursive List Definition slide 2
Revised 21-Mar-05 About This Lecture 2 Trees In this lecture we study a non-linear container called a Tree and a special kind of Tree called a Binary Tree. CMPUT 115 - Lecture 18 Department of Computing
More informationDefinitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees
Topic 17 Introduction ti to Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb Definitions A A tree is an abstract t data t d internal type nodes one
More informationMarch 20/2003 Jayakanth Srinivasan,
Definition : A simple graph G = (V, E) consists of V, a nonempty set of vertices, and E, a set of unordered pairs of distinct elements of V called edges. Definition : In a multigraph G = (V, E) two or
More informationPrinciples of Computer Science
Principles of Computer Science Binary Trees 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 1 Today s Topics Extending LinkedList with Fast Search Sorted Binary Trees Tree Concepts Traversals of a Binary
More informationTrees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech
Trees Dr. Ronaldo Menezes Hugo Serrano (hbarbosafilh2011@my.fit.edu) Introduction to Trees Trees are very common in computer science They come in different variations They are used as data representation
More informationTree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.
Trees, Binary Search Trees, and Heaps CS 5301 Fall 2013 Jill Seaman Tree: non-recursive definition Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every node (except
More informationLec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)
Lec 17 April 8 Topics: binary Trees expression trees Binary Search Trees (Chapter 5 of text) Trees Linear access time of linked lists is prohibitive Heap can t support search in O(log N) time. (takes O(N)
More informationTREES. Trees - Introduction
TREES Chapter 6 Trees - Introduction All previous data organizations we've studied are linear each element can have only one predecessor and successor Accessing all elements in a linear sequence is O(n)
More informationTrees. CSE 373 Data Structures
Trees CSE 373 Data Structures Readings Reading Chapter 7 Trees 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical relationships File directories
More informationPESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication
USN 1 P E PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -0 Department of Electronics and Communication INTERNAL ASSESSMENT TEST 2 Date : 4//2017 Marks: 50 Subject & Code
More informationThere are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.
Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 49 Module 09 Other applications: expression tree
More informationDiscussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:
Discussion 2C Notes (Week 8, February 25) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Trees Definitions Yet another data structure -- trees. Just like a linked
More informationAlgorithms and Data Structures
Algorithms and Data Structures PD Dr. rer. nat. habil. Ralf Peter Mundani Computation in Engineering / BGU Scientific Computing in Computer Science / INF Summer Term 2018 Part 2: Data Structures PD Dr.
More informationTrees, Heaps, and Priority Queues
Chapter 17 Trees, Heaps, and Priority Queues Objectives To know how to represent and access the elements in a binary tree ( 17.2.1-17.2.2). To insert an element to a binary tree ( 17.2.3). To traverse
More informationUNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.
UNIT III TREES A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items. Tree: A tree is a finite set of one or more nodes such that, there
More informationTrees! Ellen Walker! CPSC 201 Data Structures! Hiram College!
Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College! ADTʼs Weʼve Studied! Position-oriented ADT! List! Stack! Queue! Value-oriented ADT! Sorted list! All of these are linear! One previous item;
More informationCMPT 225. Binary Search Trees
CMPT 225 Binary Search Trees Trees A set of nodes with a single starting point called the root Each node is connected by an edge to some other node A tree is a connected graph There is a path to every
More informationLecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson
Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Reading: Carrano, Chapter 15 Introduction to trees The data structures we have seen so far to implement
More informationCISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees Outline General Trees Terminology, Representation, Properties Binary Trees Representations, Properties, Traversals Recursive Algorithms
More informationDATA STRUCTURES USING C
DATA STRUCTURES USING C The British Constitution Crown Church of England Cabinet House of Commons House of Lords Supreme Court Ministers County Metropolitan Council police Rural District County Borough
More informationOutline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures
Outline Chapter 7: Trees 1 Chapter 7: Trees Approaching BST Making a decision We discussed the trade-offs between linked and array-based implementations of sequences (back in Section 4.7). Linked lists
More informationData Structures Question Bank Multiple Choice
Section 1. Fundamentals: Complexity, Algorthm Analysis 1. An algorithm solves A single problem or function Multiple problems or functions Has a single programming language implementation 2. A solution
More informationTopic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb
Topic 18 "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb Definitions A tree is an abstract data type one entry point, the root Each node is either a leaf
More informationCOSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)
COSC 2007 Data Structures II Final Exam Thursday, April 13 th, 2006 This is a closed book and closed notes exam. There are total 3 parts. Please answer the questions in the provided space and use back
More informationTree Structures. A hierarchical data structure whose point of entry is the root node
Binary Trees 1 Tree Structures A tree is A hierarchical data structure whose point of entry is the root node This structure can be partitioned into disjoint subsets These subsets are themselves trees and
More informationCS200: Trees. Rosen Ch & 11.3 Prichard Ch. 11. CS200 - Trees
CS200: Trees Rosen Ch. 11.1 & 11.3 Prichard Ch. 11 1 Trees A A node has only one parent! Except the root: zero parents B C D E F Tree grows top to bottom! 2 Tree Terminology Node interior node path root
More informationCSC148 Week 6. Larry Zhang
CSC148 Week 6 Larry Zhang 1 Announcements Test 1 coverage: trees (topic of today and Wednesday) are not covered Assignment 1 slides posted on the course website. 2 Data Structures 3 Data Structures A data
More informationTree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest
Tree Structures Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest o A tree is a connected digraph with these properties: There is exactly one node (Root)
More informationTrees, Part 1: Unbalanced Trees
Trees, Part 1: Unbalanced Trees The first part of this chapter takes a look at trees in general and unbalanced binary trees. The second part looks at various schemes to balance trees and/or make them more
More informationSample Question Paper
Scheme - I Sample Question Paper Marks : 70 Time: 3 Hrs. Q.1) Attempt any FIVE of the following. 10 Marks a. Write any four applications of data structure. b. Sketch the diagram of circular queue. c. State
More informationChapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: o There is a unique simple path between any 2 of its vertices. o No loops. o No multiple edges. Example
More informationTree Data Structures CSC 221
Tree Data Structures CSC 221 Specialized Trees Binary Tree: A restriction of trees such that the maximum degree of a node is 2. Order of nodes is now relevant May have zero nodes (emtpy tree) Formal Definition:
More information6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node
6-TREE Data Structure Management (330701) Tree: A tree is defined as a finite set of one or more nodes such that There is a special node called the root node R. The remaining nodes are divided into n 0
More informationBBM 201 Data structures
BBM 201 Data structures Lecture 11: Trees 2018-2019 Fall Content Terminology The Binary Tree The Binary Search Tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013
More informationTrees. (Trees) Data Structures and Programming Spring / 28
Trees (Trees) Data Structures and Programming Spring 2018 1 / 28 Trees A tree is a collection of nodes, which can be empty (recursive definition) If not empty, a tree consists of a distinguished node r
More informationBST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams
Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr mahmoud.sakr@cis.asu.edu.eg Cairo, Egypt, October 2012 Binary Search Trees (BST) 1. Hierarchical data structure with a single reference to root
More informationBinary Trees and Binary Search Trees
Binary Trees and Binary Search Trees Learning Goals After this unit, you should be able to... Determine if a given tree is an instance of a particular type (e.g. binary, and later heap, etc.) Describe
More informationTREES Lecture 12 CS2110 Fall 2016
TREES Lecture 12 CS2110 Fall 2016 Prelim 1 tonight! 2 5:30 prelim is very crowded. You HAVE to follow these directions: 1. Students taking the normal 5:30 prelim (not the quiet room) and whose last names
More informationSection Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees
Chapter 11 Copyright McGraw-Hill Education. All rights reserved. No reproduction or distribution without the prior written consent of McGraw-Hill Education. Chapter Summary Introduction to Trees Applications
More informationTrees. Estruturas de Dados / Programação 2 Árvores. Márcio Ribeiro twitter.com/marciomribeiro. Introduc)on. Hierarchical structure
Introduc)on Linear structures Removing this idea, treasure of applicaons Estruturas de Dados / Programação 2 Árvores Márcio Ribeiro marcio@ic.ufal.br twitter.com/marciomribeiro Hierarchical structure Companies
More informationCSI33 Data Structures
Outline Department of Mathematics and Computer Science Bronx Community College October 19, 2016 Outline Outline 1 Chapter 7: Trees Outline Chapter 7: Trees 1 Chapter 7: Trees Uses Of Trees Chapter 7: Trees
More informationLargest Online Community of VU Students
WWW.VUPages.com http://forum.vupages.com WWW.VUTUBE.EDU.PK Largest Online Community of VU Students MIDTERM EXAMINATION SEMESTER FALL 2003 CS301-DATA STRUCTURE Total Marks:86 Duration: 60min Instructions
More informationTrees. Tree Structure Binary Tree Tree Traversals
Trees Tree Structure Binary Tree Tree Traversals The Tree Structure Consists of nodes and edges that organize data in a hierarchical fashion. nodes store the data elements. edges connect the nodes. The
More informationCSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.
Name: Class: Date: CSCI-401 Examlet #5 True/False Indicate whether the sentence or statement is true or false. 1. The root node of the standard binary tree can be drawn anywhere in the tree diagram. 2.
More information4.1 Ordered Lists Revisited
Chapter 4 Linked-lists 4.1 Ordered Lists Revisited Let us revisit our ordered-list. Let us assume that we use it for storing roll numbers of students in a class in ascending order. We can make the assumption
More information12 Abstract Data Types
12 Abstract Data Types 12.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT). Define
More informationTrees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1
Trees Introduction & Terminology Cinda Heeren / Geoffrey Tien 1 Review: linked lists Linked lists are constructed out of nodes, consisting of a data element a pointer to another node Lists are constructed
More information! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge
Trees & Heaps Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Fall 2018 Jill Seaman!1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every
More informationUNIT IV -NON-LINEAR DATA STRUCTURES 4.1 Trees TREE: A tree is a finite set of one or more nodes such that there is a specially designated node called the Root, and zero or more non empty sub trees T1,
More informationTREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!
//16 Prelim 1 tonight! :3 prelim is very crowded. You HAVE to follow these directions: 1. Students taking the normal :3 prelim (not the quiet room) and whose last names begin with A through Da MUST go
More informationUnit III - Tree TREES
TREES Unit III - Tree Consider a scenario where you are required to represent the directory structure of your operating system. The directory structure contains various folders and files. A folder may
More informationa graph is a data structure made up of nodes in graph theory the links are normally called edges
1 Trees Graphs a graph is a data structure made up of nodes each node stores data each node has links to zero or more nodes in graph theory the links are normally called edges graphs occur frequently in
More informationReferences and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)
9// References and Homework Text: Chapters, and ABSTRACT DATA TYPES; LISTS & TREES Homework: Learn these List methods, from http://docs.oracle.com/javase/7/docs/api/java/util/list.html add, addall, contains,
More informationTree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.
Tree A tree consists of a set of nodes and a set of edges connecting pairs of nodes. A tree has the property that there is exactly one path (no more, no less) between any pair of nodes. A path is a connected
More informationIntroduction to Binary Trees
Introduction to inary Trees 1 ackground ll data structures examined so far are linear data structures. Each element in a linear data structure has a clear predecessor and a clear successor. Precessors
More informationTrees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.
Chapter 6 Trees In a hash table, the items are not stored in any particular order in the table. This is fine for implementing Sets and Maps, since for those abstract data types, the only thing that matters
More informationAn Introduction to Trees
An Introduction to Trees Alice E. Fischer Spring 2017 Alice E. Fischer An Introduction to Trees... 1/34 Spring 2017 1 / 34 Outline 1 Trees the Abstraction Definitions 2 Expression Trees 3 Binary Search
More informationChapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Trees Chapter 11 Chapter Summary Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees Introduction to Trees Section 11.1 Section Summary Introduction to Trees
More informationCSCI 104 Binary Trees / Priority Queues / Heaps. Mark Redekopp Michael Crowley
CSCI 04 Binary Trees / Priority Queues / Heaps Mark Redekopp Michael Crowley Trees Definition: A connected, acyclic (no cycles) graph with: A root node, r, that has 0 or more subtrees Exactly one path
More informationCS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1
CS61B Lecture #20: Trees Last modified: Mon Oct 8 21:21:22 2018 CS61B: Lecture #20 1 A Recursive Structure Trees naturally represent recursively defined, hierarchical objects with more than one recursive
More informationTrees. Truong Tuan Anh CSE-HCMUT
Trees Truong Tuan Anh CSE-HCMUT Outline Basic concepts Trees Trees A tree consists of a finite set of elements, called nodes, and a finite set of directed lines, called branches, that connect the nodes
More informationTree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010
SE-286: Data Structures t and Programming Tree Virendra Singh Indian Institute of Science Bangalore Lecture 11 Courtesy: Prof. Sartaj Sahni 1 Trees Nature Lover sviewofatree leaves branches root 3 Computer
More informationData and File Structures Laboratory
Binary Trees Assistant Professor Machine Intelligence Unit Indian Statistical Institute, Kolkata September, 2018 1 Basics 2 Implementation 3 Traversal Basics of a tree A tree is recursively defined as
More informationCSCI2100B Data Structures Trees
CSCI2100B Data Structures Trees Irwin King king@cse.cuhk.edu.hk http://www.cse.cuhk.edu.hk/~king Department of Computer Science & Engineering The Chinese University of Hong Kong Introduction General Tree
More informationCMSC 341 Leftist Heaps
CMSC 341 Leftist Heaps Based on slides from previous iterations of this course Today s Topics Review of Min Heaps Introduction of Left-ist Heaps Merge Operation Heap Operations Review of Heaps Min Binary
More information