Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.

Slides:



Advertisements
Similar presentations
Introduction to Trees Chapter 6 Objectives
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees Chapter 8.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
10. Binary Trees A. Introduction: Searching a linked list.
12 Abstract Data Types Foundations of Computer Science ã Cengage Learning.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Trees, Binary Trees, and Binary Search Trees COMP171.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
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,
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
1 10. Binary Trees Read Sec A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
1 CMSC 341 Introduction to Trees Textbook sections:
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Trees Chapter 15.
Week 6 - Wednesday CS221.
Lecture 18. Basics and types of Trees
Binary Trees, Binary Search Trees
Introduction to Trees IT12112 Lecture 05.
Binary Trees, Binary Search Trees
Trees.
Binary Trees, Binary Search Trees
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

TREES A tree consists of a finite set of elements, called nodes (or vertices) and a finite set of directed lines, called arcs, that connect pairs of the nodes. Figure Tree representation

12.3 As We can divided the vertices in a tree into three categories: the root, leaves and the internal nodes. Table 12.1 shows the number of outgoing and incoming arcs allowed for each type of node.

12.4 Each node in a tree may have a subtree. The subtree of each node includes one of its children and all descendents of that child. Figure shows all subtrees for the tree in Figure Figure Subtrees

lambda munu lambda nu mu Figure: Two distinct rooted trees

a b c d a b cdcd Figure. Two distinct binary trees The two binary trees as shown in figure are not the same: in the first case b is the left child of a and the right child is missing, whereas in the second case b is the right child of a and the left child is missing

Trees A tree is an acyclic, connected and undirected graph. Equivalently, a tree may be defined as an undirected graph in which there exists exactly one path between any given pair of nodes. Since a tree is a kind of graph, the same representations used to implement graphs can be used to implement trees. Figure (a) Trees with 4 nodes Trees have a number of simple properties, of which the following are perhaps the most important:- A tree with n nodes has exactly n - 1 edges. If a single edge is removed from a tree, then the resulting graph is no longer connected.

Height, Depth and Level Height and level, for instance, are similar concepts, but not the same. - The height of a node is the number of edges in the longest path from the node in question to a leaf. - The depth of a node is the number of edges in the path from the root to the node in question. - The level of a node is equal to the height of the root of the tree minus the depth of the node concerned.

alpha NodeHeightDepthLevel beta deltaepsilon gamma zeta alpha202 beta111 gamma011 delta020 epsilon020 zeta020 Figure: Height, depth and level Figure: A rooted tree Figure: Height, depth and level

BINARY TREES A binary tree is a tree in which no node can have more than two subtrees. In other words, a node can have zero, one or two subtrees. Figure A binary tree

12.11 Recursive definition of binary trees We can also define a structure or an ADT recursively. The following gives the recursive definition of a binary tree. Note that, based on this definition, a binary tree can have a root, but each subtree can also have a root.

12.12 Figure shows eight trees, the first of which is an empty binary tree (sometimes called a null binary tree). Figure Examples of binary trees

12.13 Operations on binary trees The six most common operations defined for a binary tree are tree (creates an empty tree), insert, delete, retrieve, empty and traversal. We discuss binary tree traversal in this section. A binary tree traversal requires that each node of the tree be processed once and only once in a predetermined sequence. The two general approaches to the traversal sequence are 1) Depth-first ( deeper in the tree before exploring siblings) 2) Breadth-first traversal (Trees can also be traversed in level-order) Binary Tree Traversal

12.14 Figure Depth-first traversal of a binary tree ROOT LEFT RIGHT LEFT ROOT RIGHT LEFT RIGHT ROOT Depth First Transversal

Breadth-first Traversal Trees can also be traversed in level-order, where we visit every node on a level before going to a lower level. This is also called Breadth-first traversal.Breadth-first traversal Example Preorder

Example Figure shows how we visit each node in a tree using preorder traversal i.e A, B, C, D, E, F.

12.17 Binary tree applications Binary trees have many applications in computer science. In this section we mention only two of them: Huffman coding and expression trees. Huffman coding Huffman coding is a compression technique that uses binary trees to generate a variable length binary code from a string of symbols. We discuss Huffman coding in detail in next lectures.

12.18 Expression trees An arithmetic expression can be represented in three different formats: infix, postfix and prefix. In an infix notation, the operator comes between the two operands. In postfix notation, the operator comes after its two operands, and in prefix notation it comes before the two operands. These formats are shown below for addition of two operands A and B.

12.19 Figure Expression tree

BINARY SEARCH TREES A binary search tree (BST) is a binary tree with one extra property: the key value of each node is greater than the key values of all nodes in each left subtree and smaller than the value of all nodes in each right subtree. Figure shows the idea. Figure Binary search tree (BST)

12.21 Example Figure shows some binary trees that are BSTs and some that are not. Note that a tree is a BST if all its subtrees are BSTs and the whole tree is also a BST. Figure Example 12.12

12.22 A very interesting property of a BST is that if we apply the inorder traversal of a binary tree, the elements that are visited are sorted in ascending order. For example, the three BSTs in Figure 12.29, when traversed in order, give the lists (3, 6, 17), (17, 19) and (3, 6, 14, 17, 19). An inorder traversal of a BST creates a list that is sorted in ascending order. i LEFT ROOT RIGHT

12.23 Binary search tree ADTs The ADT for a binary search tree is similar to the one we defined for a general linear list with the same operation. As a matter of fact, we see more BST lists than general linear lists today. The reason is that searching a BST is more efficient than searching a linear list: a general linear list uses sequential searching, but BSTs use a version of binary search.

12.24 BST implementation BSTs can be implemented using either arrays or linked lists. However, linked list structures are more common and more efficient. The implementation uses nodes with two pointers, left and right. Figure A BST implementation

Trees On a computer any rooted tree may be represented using nodes of the following type. type treenode1 = record value: information eldest - child, next - sibling: ↑ treenode1

Trees We shall often have occasion to use binary trees. In such a tree, each node can have 0, 1, or 2 children. In fact, we almost always assume that a node has two pointers, one to its left and one to its right, either of which can be nil..

Trees If each node of a rooted tree can have no more than k children, we say it is a k-ary tree. One obvious representation uses nodes of the type k-ary-node = record value: information child: array [i.. k] of ↑k-ary-node In the case of a binary tree we can also define type binary-node = record value: information left-child, right-child : ↑binary-node

function Binary search (x,r) {The pointer r points to the root of a search tree. The function searches for the value x in this tree and returns a pointer to the node containing x. If x is missing, the function returns nil.} if r = nil then {x is not in the tree.} return nil else if x = r↑. Value then return r else if x < r ↑. Value then return search (x,r ↑.left-child) else return search(x,r ↑.right-child)

Trees For a tree operations as searches or the addition and deletion of nodes take a O(log n) in the worst case, where n is the number of nodes in the tree.