Binary Tree B G E D I H F c A Binary tree

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
EC-211 DATA STRUCTURES LECTURE Tree Data Structure Introduction –The Data Organizations Presented Earlier are Linear in That Items are One After.
Binary Tree Terminology Linear versus hierarchical data Tree – connected graph with no cycles Child Parent Descendant Sibling Ancestor Leaf vs. internal.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
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.
Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
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.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
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.
Compiled by: Dr. Mohammad Omar Alhawarat
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Lecture – Searching a Tree Neil Ghani University of Strathclyde.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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.
Trees Namiq Sultan. Trees Trees are very flexible, versatile and powerful non-liner data structure that can be used to represent data items possessing.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
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.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Lecture 7: Searching a Tree Neil Ghani University of Strathclyde.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Data Structures Binary Trees 1.
Binary Search Tree (BST)
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Binary Trees.
Abstract Data Structures
Binary Trees.
Binary Trees.
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Tree Data Structures There are a number of applications where linear data structures are not appropriate. Consider a genealogy tree of a family. Mohammad.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
Binary Trees.
Presentation transcript:

Binary Tree B G E D I H F c A Binary tree A binary tree is a finite set of elements that is either empty or is partitioned into three disjoint subsets. The first subset contains a single element called the root of the tree. The other two subsets are themselves binary trees, called the left and right subtrees of the original tree. A left or right subtree can be empty. Each element of a tree is called a node of the tree. B G E D I H F c A Binary tree

Binary Tree Structures that are not binary trees B G E D I H F c A

Binary Tree Structures that are not binary trees A c B F E D G

Binary Tree Structures that are not binary trees A c B F E D G H I

Binary Tree Strictly binary trees: If every nonleaf node in a binary tree has nonempty left and right subtrees, the tree is called a strictly binary tree. A strictly binary tree with n leaves always contains 2n -1 nodes. B D G F E C A

Binary tree Structure that is not a strictly binary tree: because nodes C and E have one son each. B G E D I H F c A

Level and depth of a binary Tree Level of binary tree: The root of the tree has level 0. And the level of any other node is one more than the level of its father. Depth of a binary tree: The depth of a binary tree is the maximum level of any leaf in the tree. B D G F E C A Level 0 Level 1 Level 2 Level 3 Depth is 3.

A complete binary tree Complete binary tree of depth d is the strictly binary tree all of whose leaves are at level d. A complete binary tree of depth d is the binary tree of depth d that contains exactly 2 l nodes at each level l between 0 and d. The total number of nodes = the sum of the number of nodes at each level between 0 and d. = 2 d + 1 - 1 C A O N M F G K J I H E D B L

Almost complete binary tree A binary tree of depth d is an almost complete binary tree if: - 1. A node nd at level less than d -1 has two sons - 2. For any node nd in the tree with a right descendant at level d, nd must have a left son and every left descendant of nd is either a leaf at level d or has two sons. G E D B A C F The strictly binary tree is not almost complete, since it contains leaves at levels 1, 2, and 3. Violates condition 1

Almost complete binary tree A binary tree of depth d is an almost complete binary tree if: - 1. A node nd at level less than d -1 has two sons - 2. For any node nd in the tree with a right descendant at level d, nd must have a left son and every left descendant of nd is either a leaf at level d or has two sons. The strictly binary tree is not almost complete, since A has a right descendant at level 3 (J) but also has a left descendant that is a leaf at level 2 (E) Violates condition 2 Satisfies the condition1, since every leaf node is either at level 2 or at level 3. I E D B A G H C F K J

Almost complete binary tree A binary tree of depth d is an almost complete binary tree if: - 1. A node nd at level less than d -1 has two sons - 2. For any node nd in the tree with a right descendant at level d, nd must have a left son and every left descendant of nd is either a leaf at level d or has two sons. The binary tree is almost complete, Satisfies the condition1, since every leaf node is either at level 2 or at level 3. Satisfies the condition 2 1 A 2 B 3 C 5 4 6 7 D E F G 8 9 H I

Almost complete binary tree A binary tree of depth d is an almost complete binary tree if: - 1. A node nd at level less than d -1 has two sons - 2. For any node nd in the tree with a right descendant at level d, nd must have a left son and every left descendant of nd is either a leaf at level d or has two sons. The binary tree is almost complete, Satisfies the condition1, since every leaf node is either at level 2 or at level 3. Satisfies the condition 2 However, the binary tree is not strictly binary tree, since node E has a left son but not a right son 1 A 2 B 3 C 5 4 6 7 D E F G 9 8 10 H I F

Traversing a binary tree Three methods: - 1. preorder - 2. inorder - 3 postorder Preorder 1. Visit the root 2. Traverse the left subtree in preorder 3. Traverse the right subtree in preorder 14 Inorder 1. Traverse the left subtree in inorder 2. Visit the root 3. Traverse the right subtree in inorder 4 15 3 9 18 7 16 20 Postorder 1. Traverse the left subtree in postorder 2. Traverse the right subtree in postorder 3. Visit the root 17 5

Traversing a binary tree D B A F C E G I H Preorder 1. Visit the root 2. Traverse the left subtree in preorder 3. Traverse the right subtree in preorder preorder: ABDGCEHIF Inorder 1. Traverse the left subtree in inorder 2. Visit the root 3. Traverse the right subtree in inorder inorder: DGBAHEICF Postorder 1. Traverse the left subtree in postorder 2. Traverse the right subtree in postorder 3. Visit the root postorder: GDBHIEFCA

Traversing a binary tree Preorder 1. Visit the root 2. Traverse the left subtree in preorder 3. Traverse the right subtree in preorder preorder: ABCEIFJDGHKL B D C H G L K E F Inorder 1. Traverse the left subtree in inorder 2. Visit the root 3. Traverse the right subtree in inorder inorder: EICFJBGDKHLA J I Postorder 1. Traverse the left subtree in postorder 2. Traverse the right subtree in postorder 3. Visit the root postorder: IEJFCGKLHDBA