Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 171: Introduction to Computer Science II
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.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Trees CS212 & CS-240 D.J. Foreman. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
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.
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.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 16 Binary.
Tree Data Structures.
Compiled by: Dr. Mohammad Omar Alhawarat
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. 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.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
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.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
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.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
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.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Trees Chapter 15.
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Recursive Objects (Part 4)
Tree.
Section 8.1 Trees.
Data Structures & Algorithm Design
Binary Trees, Binary Search Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Binary Trees, Binary Search Trees
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Main Index Contents 11 Main Index Contents Week 6 – Binary Trees

Sequence and Associative Containers Sequence containers access data by position 1.Array (index) 2.Vector (index) 3.List (iterator) Associate containers can access data by value 1.Set and Map / Binary search tree Arrays Vectors Linked lists Trees

Tree in Nature

Tree in our life Need to turn it upside down

Tree in Computer Science Similar to tree in nature Root Leaves

Main Index Contents 66 Main Index Contents Tree Structures Tree Structures Root Parent Child Edge Leaf Interior node Subtree Level Depth = max level

Terminologies used in Trees - Wiki

Main Index Contents 88 Main Index Contents Tree Node Level and Path Length What is the Depth?

Main Index Contents 9 Binary Tree Definition A binary tree T is a finite set of nodes with one of the following properties: – (a) T is a tree if the set of nodes is empty. (An empty tree is a tree, size=0.) – (b) The set consists of a root, R, and exactly two distinct binary trees, the left subtree T L and the right subtree T R. The nodes in T consist of node R and all the nodes in T L and T R. Any node in a binary tree has at most two children

Main Index Contents 10 Main Index Contents Selected Samples of Binary Trees Does tree B have left subtree T L ?

Density of a Binary Tree Intuitively, density is a measure of the size of a tree (number of nodes) relative to the depth of the tree. Trees with a higher density are important as data structures, because they can “pack” more nodes near the root. Access to the nodes is along relatively short paths from the root.

Complete binary tree

Degenerate tree A degenerate (or pathological) tree is where each parent node has only one associated child node. This means that performance-wise, the tree will behave like a linked list data structure.linked list

Evaluating Tree Density Complete binary trees are an ideal storage structure, because of their ability to pack a large number of nodes near the root Assume we want to store n elements in a complete binary tree. We would like to know the depth d of such a tree.

Depth d --- Size n in complete binary tree … ………………… … … level d has 2 d nodes 2 0 = 1 nodes 2 1 = 2 nodes Geometric series

Main Index Contents 17 Main Index Contents Binary Tree Nodes

Node class template { public: T nodeValue; tnode *left, *right; tnode() {} tnode(const T& item, tnode *lptr=NULL, tnode *rptr=NULL):nodeValue(item), left(lptr), right(rptr) {} };

Node structure

Building a Binary Tree A binary tree consists of a collection of dynamically allocated tnode objects whose pointer values specify links to their children.

Recursion Solution to a problem depends on solutions to smaller instances of the same problem. As a tree is a self-referential (recursively defined) data structure, traversal can naturally be described by recursion.recursion Recursive function: a function that calls itself. 21

BINARY TREE SCAN ALGORITHMS How to traverse the tree so that each node is visited exactly once? 1. Depth-first Pre-order In-order Post-order 2. Breadth-first (level-order)

Depth-first Defined as operations recursively at each node. The actions include:  visiting the node and performing some task (N),  making a recursive descent to the left subtree (L),  making a recursive descent to the right subtree (R). The different scanning strategies depend on the order in which we perform the tasks.

In-order Scan The prefix “in” comes from the fact that the visit occurs between the descents into the two subtrees. In this example, we use the order LNR. 1. Traverse the left subtree (“go left”). 2. Visit the node. 3. Traverse the right subtree (“go right”). L N R Recursively!

In-order example L N R L N R   Recursively! In-order scan: B, D, A, E, C  

In-order output

Post-order scan 1. Traverse the left subtree (“go left”). 2. Traverse the right subtree (“go right”). 3. Visit the node D, B, E, C, A

Post-order output

Pre-order scan 1. Visit the node. 2. Traverse the left subtree (“go left”). 3. Traverse the right subtree (“go right”) A, B, D, C, E

Breadth-first (Level-Order) Scan

Example Question F B CE I H DA G Pre-order? In-order? Post-order? Level-order ?

WIKI

Computing the Leaf Count Pre-order scan

Computing the Depth of a Tree Post-order scan

Deleting Tree Nodes Post-order scan

Reading Chapter 4