Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.

Slides:



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

Trees1 More on Trees University Fac. of Sci. & Eng. Bus. School Law School CS Dept. EE Dept. Math. Dept.
Introduction to Trees Chapter 6 Objectives
Data Structures: A Pseudocode Approach with C 1 Chapter 6 Objectives Upon completion you will be able to: Understand and use basic tree terminology and.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
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.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
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.
Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.
Binary Trees Chapter 6.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
COSC2007 Data Structures II
CS261 Data Structures Trees Introduction and 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.
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.
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,
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
درختها Trees ساختمان داده ها والگوريتمها مقايسه ليست خطي و درخت Linear lists are useful for serially ordered data. – (e 0, e 1, e 2, …, e n-1 ) – Days.
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.
Data Structures TREES.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
Disusun Oleh : Budi Arifitama Pertemuan ke-8. Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
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.
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Foundation of Computing Systems Lecture 4 Trees: Part I.
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.
1 CMSC 341 Introduction to Trees Textbook sections:
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
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.
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.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Trees.
Trees.
Trees Another Abstract Data Type (ADT)
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
CHAPTER 4 Trees.
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
Introduction to Trees IT12112 Lecture 05.
Trees Another Abstract Data Type (ADT)
Trees Another Abstract Data Type (ADT)
Trees (Part 1, Theoretical)
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Binary Trees, Binary Search Trees
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Binary Tree Properties & Representation
Tree.
Binary Trees.
Binary Trees, Binary Search Trees
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Binary Trees

Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are partitioned into two binary trees These are called the left and right subtrees of the binary tree

Differences Between A Tree & A Binary Tree No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a tree. A binary tree may be empty; a tree cannot be empty. The subtrees of a binary tree are ordered; those of a tree are not ordered.

Differences Between A Tree & A Binary Tree The subtrees of a binary tree are ordered; those of a tree are not ordered a b a b Are different when viewed as binary trees Are the same when viewed as trees

Binary Trees Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure that contains no nodes, or is comprised of three disjoint sets of nodes: a root a binary tree called its left subtree a binary tree called its right subtree A binary tree that contains no nodes is called empty Note: the position of a child matters!

Binary Trees Full binary tree : All internal nodes have two children. Complete binary tree : All leaves have the same depth All internal nodes have two children A complete binary tree of height h has 2 h -1 internal nodes and 2 h leaves Also: a binary tree with n nodes has height at least  lgn 

What is a binary tree? Property1: each node can have up to two successor nodes (children) The predecessor node of a node is called its parent The "beginning" node is called the root (no parent) A node without children is called a leaf <=2 0

Property1: each node can have up to two successor nodes (children) The predecessor node of a node is called its parent The "beginning" node is called the root (no parent) A node without children is called a leaf What is a binary tree?

Property2: a unique path exists from the root to every other node What is a binary tree? (cont.)

Some terminology Ancestor of a node: any node on the path from the root to that node Descendant of a node: any node on a path from the node to the last node in the path Level (depth) of a node: number of edges in the path from the root to that node Height of a tree: number of levels (warning: some books define it as #levels - 1)

Properties Binary Trees Because in a binary tree all the nodes have must have the same number of children we are forced to change the concepts slightly We say that all internal nodes have two children External nodes have no children internal node external node

Recursive definition of a Binary Tree Most of concepts related to binary trees can be explained recursive For instance, the definition of what is a binary tree can done recursively A binary tree is: An external node, or An internal node connected to a left binary tree and a right binary tree (called left and right subtrees) In programming terms we can see that our definition for a linked list (singly) can be modified to have two links from each node instead of one.

Mathematical Properties of Binary Trees Let's us look at some important mathematical properties of binary trees A good understanding of these properties will help the understanding of the performance of algorithms that process trees Some of the properties we'll describe relate also the structural properties of these trees. This is the case because performance characteristics of many algorithms depend on these structural properties and not only the number of nodes.

Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each of first h levels. minimum number of nodes is h

Maximum Number Of Nodes All possible nodes at first h levels are present  Maximum number of nodes = … + 2 h-1 = 2 h - 1

Number Of Nodes & Height Let n be the number of nodes in a binary tree whose height is h. h <= n <= 2 h – 1 log 2 (n+1) <= h <= n max height The max height of a tree with N nodes is N (same as a linked list) min height The min height of a tree with N nodes is log(N+1)

Relationship Between Number of Nodes (Internal - External) A binary tree with N internal nodes has N+1 external nodes Let's try to prove this using induction... A binary tree with no internal nodes has one external node so the property holds for N=0 For N>0, any binary tree with N internal nodes has k internal nodes in the left subtree and N-k-1 in the right subtree, for k between 0 and N-1 (since the root is also an internal node). By the hypothesis above the left subtree will have k+1 external nodes in the left subtree and N-k in the right subtree which gives a total of N+1 external nodes

Number of edges A binary tree with N internal nodes has 2N edges In any rooted tree, each node has a unique parent (except the root) So there are N-1 edges connecting internal nodes Also, each of the N+1 external nodes has one edge to its unique parent So there are N+1 edge connecting external nodes to internal nodes Combining the two we can see that we have 2N edges in the binary tree

Number of edges A binary tree with N nodes (internal and external) has N-1 edges If the tree has N nodes k are internal nodes. Since they all (except the root) have one connection to the parent There are k-1 edges connecting internal nodes Also, there are N-k external nodes and each of them have a connection to the parent So there are N-k edges connecting external nodes to internal nodes Combining the two we can see that we have N-1 edges in the binary tree

Some More Definitions The level of a node in a tree is one higher that the level of its parent (root level being 0) The height of a tree is the maximum of the levels of the tree's nodes level 0 level 3

Full Binary Tree A full binary tree of a given height h has 2 h – 1 nodes Height 4 full binary tree

Numbering Nodes in a Full Binary Tree Number the nodes 1 through 2h – 1. Number by levels from top to bottom. Within a level number from left to right

Node Number Properties Parent of node i is node i/2 But node 1 is the root and has no parent Left child of node i is node 2i But if 2i > n, node i has no left child Right child of node i is node 2i+1 But if 2i+1 > n, node i has no right child

Complete Binary Tree With n Nodes Start with a full binary tree that has at least n nodes. Number the nodes as described earlier. The binary tree defined by the nodes numbered 1 through n is the unique n node complete binary tree Complete binary tree with 10 nodes

Binary Tree Representation Array representation Linked representation

Array Representation Number the nodes using the numbering scheme for a full binary tree Store the node numbered i in tree[i] tree[] 0510 abcdefghij b a c d e f g hi j

Right-Skewed Binary Tree An n node binary tree needs an array whose length is between n+1 and 2n a b 1 3 c 7 d 15 tree[] 0510 a-b---c d

Linked Representation Each binary tree node is represented as an object whose data type is BinaryTreeNode The space required by an n node binary tree is n * (space required by one node)

Binary Trees A binary tree is a tree whose nodes have at most two offspring Example typedef … dataType; struct nodeType { dataType data; struct nodeType *left, *right; }; struct nodeType *tree;

Linked Representation Example a cb d f e g h leftChild element rightChild root