Computer Science and Engineering

Slides:



Advertisements
Similar presentations
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Trees What is a Tree? Tree terminology Why trees? General Trees and their implementation N-ary Trees N-ary Trees implementation Implementing trees Binary.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications.
Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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,
Topic 17 Introduction to Trees
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.
Data Structures TREES.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
Binary Tree.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
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 CMSC 341 Introduction to Trees Textbook sections:
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Trees Chapter 15.
Data Structure and Algorithms
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
CS510 Compiler Lecture 4.
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
CS 302 Data Structures Trees.
Lecture Trees Chapter 9 of textbook 1. Concepts of trees
CMSC 341 Introduction to Trees.
Trees What is a Tree? Tree terminology Why trees?
Lecture 18. Basics and types of Trees
CHAPTER 4 Trees.
Binary Trees, Binary Search Trees
Binary Tree and General Tree
TREES General trees Binary trees Binary search trees AVL trees
Trees and Binary Trees.
CS212: Data Structures and Algorithms
Trees.
Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Week nine-ten: Trees Trees.
Trees Lecture 9 CS2110 – Fall 2009.
Trees CMSC 202, Version 5/02.
Computer Science and Engineering
CMSC 202 Trees.
Computer Science and Engineering
Computer Science and Engineering
Binary Trees, Binary Search Trees
Trees Chapter 10.
Non-Linear Structures
CE 221 Data Structures and Algorithms
Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Announcements Prelim 1 on Tuesday! A4 will be posted today
High-Level Programming Language
B.Ramamurthy Chapter 9 CSE116A,B
Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
COMPILER CONSTRUCTION
Introduction to Trees Chapter 6 Objectives
Faculty of Computer Science and Information System
Trees Lecture 10 CS2110 – Spring 2013.
Cs212: Data Structures Lecture 7: Tree_Part1
Presentation transcript:

Computer Science and Engineering Binary Trees Computer Science and Engineering 2/23/2019

Introduction We studied linked list is a dynamic linear data structure. It is dynamic since it supports efficient addition and deletion of items. It is linear since it is sequential and each element in it has exactly one successor. A tree is nonlinear data structure. Each element may have more than one successor. Can be static or dynamic. 2/23/2019

Topics for Discussion Elements of a tree Examples of trees Binary Tree Definition Types of binary trees Contiguous (static) representation Dynamic representation 2/23/2019

Terminology Trees are used to represent relationships: items in a tree are referred to as nodes and the lines connecting the nodes that express the hierarchical relationship are referred to as edges. The edges in a tree are directed. Trees are hierarchical which means that a parent-child relationship exist between the nodes in the tree. Each node has at most one parent. The node with no parents is the root node. The nodes that have no successors (no children nodes) are known as leaf nodes. Lets look at some examples and identify the various elements. 2/23/2019

Examples Family ancestor tree Directory of files organization in your computer system Parse tree Languages are defined using grammar Grammars are specified using rules or syntax Syntax is expressed using a notation called Backaus-Naur Form (BNF) (John Backus and Peter Naur) Expression trees Game trees 2/23/2019

An Ancester Tree (From Greek mythology) Gaea Cronus Phoebe Ocean Zeus Poseidon Demeter Pluto Leto ……… Apollo 2/23/2019

BNF for a Language BNF notation includes nonterminals and terminals. Terminals are literals or particular symbols. Nonterminals are general expressions that can be substituted with terminals and nonterminals. Grammar rules specify the definition of a nonterminal. Nonterminals are enclosed with angle brackets <nonterminal> Symbols used in construction include ::= (defines), | (or) and other common operators. 2/23/2019

BNF for a Java Statement <statement> ::= <selection-stmt> | <other-stmt> <selection-stmt> ::= if (<expr>) <statement> else <statement> <expr>::= <relational-expr>|<assign-expr>|<identifier> <relational-expr> ::= <expr> <rel-op> <expr> <assign-expr> ::= <expr> = <expr> 2/23/2019

Parse tree <statement> <selection-stmt> if ( <expr> ) <statement> else <statement> … <relational-expr> <expr> <expr> <rel-op> <expr> …. A major task of the compiler is to construct a parse tree from the input program and verify it is correct. <identifier> <identifier> < b a 2/23/2019

Expression tree + A + B + C * D + <left><root><right> (in-order expression) <root><left><right> (pre-order expression> <left><right><root> (post-order expression) * A B C D Single representation; Multiple views 2/23/2019

Game Tree X X X X …… X …. … X X X X X X X X 2/23/2019

Binary Tree A binary tree can be defined recursively as follows. It is either empty, or consists of a root node together with left and right trees, both of which are binary trees. 2/23/2019

Binary Tree NonEmpty Empty NullObject (pattern) Singleton (pattern) 2/23/2019

Binary Tree (contd.) 2/23/2019

Binary Tree (contd.) 2/23/2019

Characteristics of trees A path is a sequence of nodes n1, n2, ..., nk such that node ni is the parent of node ni+1 for all 1 <= i <= k. The length of a path is the number of edges on the path. The height of a node is the length of the longest path from the node to a leaf. The height of tree is the height of its root. The level of a node is the length of the path from the root to the node. 2/23/2019

Full Binary Tree A full binary tree is a tree in which each node has exactly zero or two non-empty children. All leaves are at the same level. A complete binary tree in which all the leaf nodes are in level n or n-1 and all leaves on the level n are filled from left to right. There are some interesting properties that arise out of this definition. Lets look at some examples to illustrate the various definitions. 2/23/2019

Example root Level 0 Level 1 internal node leaf Height of the tree:3 2/23/2019

Contiguous Representation for complete binary tree 1 2 3 5 4 6 7 8 2/23/2019

Complete binary tree (contd.) Number the N nodes sequentially, from 1.. N, starting with the root , level by level from left to right. Parent(n) = floor(n/2) for n>1 Left child(n) = 2n (if 2n <= N, else no left child) Right child(n) = 2n+1 (if 2n+1 <= N, else no right child) The node number of children and parent can be calculated for a given node n. 2/23/2019

Contiguous representation By placing the N nodes in a contiguous sequence we can use simple arithmetic relationships to process the nodes. This will eliminate storage for the pointers to sub trees. Root object 1 2 3 4 5 6 8 7 2/23/2019

Array Representation Refer to the array in slide 21 which represents the complete binary tree in slide 19; Array index 1 has the root object, 2 and 3 the left and right sub tree of root object respectively and so on. The storage needed for the pointers to the left and right sub tree for each node is eliminated; But the location need to be calculated every time a node is accessed. Trade off is between the storage need for the pointers and extra execution time incurred for computing the location. 2/23/2019

Linked Representation In its simplest form class BTree{ Object obj; BTree left; BTree right; //constructor get, set methods //public Object acceptVisitor(…) //for all other //operations } 2/23/2019

Linked Representation BTreeInterface Object getData( ) Object getLeft( ) Object getRight( ) void setData( ) void setLeft( ) void setRight( ) BTree Simple interface/implementation /application Addition of a visitor (Visitor Pattern) State-based implementation 2/23/2019

Methods for Tree class Trees are very widely used in many applications. It is simply impossible and impractical to think of all the applications to decide the operations to implement in a tree class. Many may not be known apriori. Even if you implement all possible operations you can think of, only a small subset may be used by a given application. For example, preorder traversal may not be needed for game tree application. Solution: add operations to the tree class depending on the application. This requires recompilation. Another solution is to use a Visitor pattern. 2/23/2019

Visitor Pattern Visitor Pattern: elegant solution that allows for future addition of an operation to a class. The operation need not be known to a class at the time of its implementation. Requirements: Class (say, AClass) should have a method to accept visitors (say, acceptVisitor). That is, AClass and its subclasses receive visitors thru’ this method. Visitor super class (say, interface Visitor) that specifies the signatures for the methods (operations) to be implemented by concrete visitors, one operation per concrete subclass of AClass. acceptVisitor method should be passed an object reference to the visitor and place holders for input and output data (objects). This method is independent of implementation/name of the concrete visitors. Concrete visitor has implementations of the operations specified in the interface Visitor. 2/23/2019

Using a Visitor In an application: Instantiate an object, say host, of concrete class of AClass. Btree host = new Btree(); Instantiate an object, aVisitor, of concrete visitor class. Visitor aVisitor = new WeightVisitor(); To carry out the operation defined in the visitor, make the “host” class “accept” the “visitor” through the acceptVisitor method. Object result = host.acceptVisitor(aVisitor, inputObj); 2/23/2019

Visitor Pattern: UML Diagram …. . …… 2/23/2019

Lets look at the code. 2/23/2019

State-based BTree Two states: EmptyTree and NonEmptyTree Abstract class unifying the states: AState SBTreeInterface specifying signatures for the binary tree including acceptVisitor() SBTree concrete class that has a acceptVisitor Implementation of basic operations (set, get, etc.) Implementation of concrete visitors for various operations. 2/23/2019

2/23/2019

2/23/2019

Binary Search Tree 2/23/2019