CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -

Slides:



Advertisements
Similar presentations
Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Advertisements

Introduction to Trees Chapter 6 Objectives
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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Binary Tree Terminology Linear versus hierarchical data Tree – connected graph with no cycles Child Parent Descendant Sibling Ancestor Leaf vs. internal.
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.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
Binary Tree B G E D I H F c A Binary tree
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
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 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.
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 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
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.
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.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 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.
Foundation of Computing Systems Lecture 4 Trees: Part I.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Trees A non-linear implementation for collection classes.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
Csc 2720 Instructor: Zhuojun Duan
Section 8.1 Trees.
Lecture 18. Basics and types of Trees
CHAPTER 4 Trees.
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Abstract Data Structures
Trees.
Trees Definitions Implementation Traversals K-ary Trees
Trees (Part 1, Theoretical)
Design and Analysis of Algorithms
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Binary Trees, Binary Search Trees
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
Binary Trees.
Non-Linear data structures
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
8.2 Tree Traversals Chapter 8 - Trees.
Introduction to Trees Chapter 6 Objectives
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

CSE 373, Copyright S. Tanimoto, 2002 Binary Trees - Trees in general -- terminology Binary trees -- properties, expression trees Array and linked representations CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -

CSE 373, Copyright S. Tanimoto, 2002 Binary Trees - Trees in General A Tree T is a set (possibly empty) of nodes, and a binary relation on this set called hasChild, such that there is a designated node called the root, and every node is reachable from the root by exactly one path of links in this relation. Alternatively we can define (as in Sahni): A tree t is a finite nonempty set of elements. One of these elements is called the root, and the remaining elements (if any) are partitioned into trees, which are called the subtrees of t. CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -

CSE 373, Copyright S. Tanimoto, 2002 Binary Trees - Example Tree n1 n2 n3 n4 n5 n6 n7 Connected (from the root), acyclic, root has indegree 0, all other nodes have indegree 1. A node with outdegree 0 is a leaf. Otherwise it’s an internal node. If (ni, nj) is in RT then ni is the parent of nj and nj is a child of ni The root is at depth 0. If n is at depth d, then a child of n is at depth d+1. A leaf has height 0. The height of an internal node is 1 + the maximum of the heights of its children. The height* of a tree is 1 + height of its root. The children of a node are considered unordered. The links are called edges or arcs or branches. (*) Note difference from Sahni’s definition of height. CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -

CSE 373, Copyright S. Tanimoto, 2002 Binary Trees - Set of nodes. Binary relation on nodes. (n, m) in RT means n is the parent of m and m is a child of n. There is a designated node called the root. The root has indegree 0. Every other node has indegree 1. Each node has outdegree 0, 1, or 2. Each child is either a left child or a right child. Each node has at most one left child and one right child. CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -

Properties of Binary Trees If T has k nodes, then it has k-1 edges. (This is actually true for any kind of tree.) A full binary tree of height h is a binary tree whose root has height h and which contains 2h+1-1 nodes. A complete binary tree with k nodes consists of a full binary tree plus 0 or more nodes that are children of the (leftmost) deepest elements of the full tree. (A complete binary tree can be thought of as a “prefix” of some full binary tree, scanning elements in level order.) CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -

CSE 373, Copyright S. Tanimoto, 2002 Binary Trees - Expression Tree + - * x 1 1 5 7 Inorder traversal produces: ((x - 1) + (5 * 7)) CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -

CSE 373, Copyright S. Tanimoto, 2002 Binary Trees - Tree Traversal Preorder: Visit the root, visit the left subtree, visit the right subtree + - x 1 * 5 7 Inorder: Visit the left subtree, visit the root, visit the right subtree ((x - 1) + (5 * 7)) Postorder: Visit the left subtree, visit the right subtree, visit the root. x 1 - 5 7 * + CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -

Array Based Representation A “Full” Binary Tree + - * x 1 1 5 7 - x 1 5 7 + * 1 2 3 4 5 6 CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -

Another “Complete” Binary Tree + - ! x 1 1 5 - x 1 5 + ! 1 2 3 4 5 CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -

An “Incomplete” Binary Tree + - ~ x 1 1 7 - x 1 7 + ~ 1 2 3 4 5 6 CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -

Linked Representation + - ~ x 1 7 CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -