Binary Trees.

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

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 171: Introduction to Computer Science II
Computer Science 2 Data Structures and Algorithms V section 2 Introduction to Trees Professor: Evan Korth New York University.
Binary Tree Terminology Linear versus hierarchical data Tree – connected graph with no cycles Child Parent Descendant Sibling Ancestor Leaf vs. internal.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Binary Trees. Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-linked lists.
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.
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
Binary Trees. 2 Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-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.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
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 
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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,
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
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:
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.
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.
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.
Discrete Mathematics Chapter 5 Trees.
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.
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.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
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.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
1 CMSC 341 Introduction to Trees Textbook sections:
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Binary 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.
Binary Trees.
Tree.
Objective: Understand Concepts related to trees.
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
CMSC 341 Introduction to Trees.
Section 8.1 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
Binary Trees.
Trees.
Binary Trees.
Binary Trees.
Trees Definitions Implementation Traversals K-ary Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Binary Trees, Binary Search Trees
Trees.
Binary Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
General Tree Concepts Binary Trees
Binary Trees, Binary Search Trees
Presentation transcript:

Binary Trees

Introduction A tree structure means that the data are organized so that items of information are related by branches Examples:

Parts of a binary tree A binary tree is composed of zero or more nodes Each node contains: A value (some sort of data item) A reference or pointer to a left child (may be null), and A reference or pointer to a right child (may be null) A binary tree may be empty (contain no nodes) If not empty, a binary tree has a root node Every node in the binary tree is reachable from the root node by a unique path A node with neither a left child nor a right child is called a leaf In some binary trees, only the leaves contain a value

Terminology Some node: the item of information plus the branches to each node. degree: the number of sub trees of a node degree of a tree: the maximum of the degree of the nodes in the tree. terminal nodes (or leaf): nodes that have degree zero nonterminal nodes: nodes that don’t belong to terminal nodes. children: the roots of the sub trees of a node X are the children of X parent: X is the parent of its children.

Terminology siblings: children of the same parent are said to be siblings. Ancestors of a node: all the nodes along the path from the root to that node. The level of a node: defined by letting the root be at level one. If a node is at level l, then it children are at level l+1. Height (or depth): the maximum level of any node in the tree

Picture of a binary tree d e g h i l f j k

Size and depth The size of a binary tree is the number of nodes in it This tree has size 12 The depth of a node is its distance from the root a is at depth zero e is at depth 2 The depth of a binary tree is the depth of its deepest node This tree has depth 4 a b c d e f g h i j k l

Balance a b c d e f g h i j A balanced binary tree a b c d e f g h i j An unbalanced binary tree A binary tree is balanced if every level above the lowest is “full” (contains 2n nodes) In most applications, a reasonably balanced binary tree is desirable

Example Property: (# edges) = (#nodes) - 1 A is the root node B is the parent of D and E C is the sibling of B D and E are the children of B D, E, F, G, I are external nodes, or leaves A, B, C, H are internal nodes The level of E is 3 The height (depth) of the tree is 4 The degree of node B is 2 The degree of the tree is 3 The ancestors of node I is A, C, H The descendants of node C is F, G, H, I Property: (# edges) = (#nodes) - 1 A B C H I D E F G Level 1 2 3 4

Example of a Tree No. of nodes = 9 Height = 4 Highest Level = 3 5/16/2019 Example of a Tree No. of nodes = 9 Height = 4 Highest Level = 3 Root Node = 8 Leaves = 1,4,7,13 Interior Nodes = 3,10,6,14 Ancestors Of 6 = 3,8 Descendents Of 10 = 14,13 Sibling Of 1 = 6

Full Binary Tree and Complete Binary Tree

Traversal A traversal is a process that visits all the nodes in the tree. Since a tree is a nonlinear data structure, there is no unique traversal. We will consider several traversal algorithms with we group in the following two kinds Depth-first traversal. Breadth-first traversal . There are three different types of depth-first traversals : PreOrder traversal - visit the parent first and then left and right children; InOrder traversal - visit the left child, then the parent and the right child; PostOrder traversal - visit left child, then the right child and then the parent; There is only one kind of breadth-first traversal--the level order traversal. This traversal visits nodes by levels from top to bottom and from left to right.

As an example consider the following tree and its four traversals: PreOrder - 8, 5, 9, 7, 1, 12, 2, 4, 11, 3 InOrder - 9, 5, 1, 7, 2, 12, 8, 4, 3, 11 PostOrder - 9, 1, 2, 12, 7, 5, 3, 11, 4, 8

Binary Tree Traversals Inorder traversal (LVR) (recursive version) output: A / B * C * D + E ptr L V R

Binary Tree Traversals Preorder traversal (VLR) (recursive version) output: + * * / A B C D E V L R

Binary Tree Traversals Postorder traversal (LRV) (recursive version) output: A B / C * D * E + L R V