Computer Science and Engineering

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II Chapter 10 Trees I. 2 Topics Terminology.
Advertisements

Introduction to Trees Chapter 6 Objectives
CS 171: Introduction to Computer Science II
CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
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.
Binary Trees Chapter 6.
COSC2007 Data Structures II
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.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
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.
LESSON 04.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
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.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Trees A non-linear implementation for collection classes.
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
PROGRAMMING LANGUAGES
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Describing Syntax and Semantics
CS510 Compiler Lecture 4.
Chapter 3 Context-Free Grammar and Parsing
Chapter 3 – Describing Syntax
Syntax (1).
CMSC 341 Introduction to Trees.
Trees What is a Tree? Tree terminology Why trees?
Lecture 18. Basics and types of Trees
CHAPTER 4 Trees.
Compiler Construction (CS-636)
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
Trees and Binary Trees.
Trees.
CSE 311: Foundations of Computing
Trees CMSC 202, Version 5/02.
Discrete Mathematics – CIS166
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
CMSC 202 Trees.
Computer Science and Engineering
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Computer Science and Engineering
Binary Trees, Binary Search Trees
Computer Science and Engineering
CE 221 Data Structures and Algorithms
Trees.
Discrete Mathematics – CIS166
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
High-Level Programming Language
B.Ramamurthy Chapter 9 CSE116A,B
Binary Trees.
Binary Trees, Binary Search Trees
Introduction to Trees Chapter 6 Objectives
Cs212: Data Structures Lecture 7: Tree_Part1
Presentation transcript:

Computer Science and Engineering Binary Trees Computer Science and Engineering 1/12/2019 B.Ramamurthy

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. 1/12/2019 B.Ramamurthy

Topics for Discussion Elements of a tree Examples of trees Binary Tree Definition Types of binary trees Contiguous (static) representation Dynamic representation 1/12/2019 B.Ramamurthy

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. 1/12/2019 B. Ramamurthy

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 1/12/2019 B.Ramamurthy

An Ancester Tree (From Greek mythology) Gaea Cronus Phoebe Ocean Zeus Poseidon Demeter Pluto Leto ……… Apollo 1/12/2019 B.Ramamurthy

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. 1/12/2019 B.Ramamurthy

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> 1/12/2019 B.Ramamurthy

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 1/12/2019 B.Ramamurthy

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 1/12/2019 B.Ramamurthy

Game Tree X …… X X X X …. … X X X X X X X X 1/12/2019 B.Ramamurthy

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. 1/12/2019 B.Ramamurthy

Binary Tree NonEmpty Empty NullObject (pattern) Singleton (pattern) 1/12/2019 B.Ramamurthy

Binary Tree (contd.) 1/12/2019 B.Ramamurthy

Binary Tree (contd.) 1/12/2019 B.Ramamurthy

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. 1/12/2019 B.Ramamurthy

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. 1/12/2019 B.Ramamurthy

Example root Level 0 Level 1 internal node leaf Height of the tree:3 1/12/2019 B.Ramamurthy