BINARY TREES. 10-2 Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.

Slides:



Advertisements
Similar presentations
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees Chapter 8.
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.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
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 Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
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.
COSC2007 Data Structures II
Tree.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
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.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
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.
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.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
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.
Starting at Binary Trees
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.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
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.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
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.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
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.
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.
CSE 373 Data Structures Lecture 7
The Tree ADT.
Data Structures and Design in Java © Rick Mercer
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Fundamentals of Programming II Introduction to Trees
Section 8.1 Trees.
Data Structures & Algorithm Design
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Tonga Institute of Higher Education
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
Introduction to Trees IT12112 Lecture 05.
Binary Trees, Binary Search Trees
Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

BINARY TREES

10-2 Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary tree implementation Examine a binary tree example

Observations (so far data structures) Array – Unordered Add, delete, search – Ordered Linked List – ??

Why use a Tree? Fundamental data storage structures used in programming. Combines advantages of an ordered array and a linked list. Searching as fast as in ordered array. Insertion and deletion as fast as in linked list.

10-5 Trees A tree is a nonlinear data structure used to represent entities that are in some hierarchical relationship Examples in real life: Family tree Table of contents of a book Class inheritance hierarchy in Java Computer file system (folders and subfolders) Decision trees Top-down design

10-6 Example: Computer File System Root directory of C drive Documents and SettingsProgram FilesMy Music DesktopFavoritesStart MenuMicrosoft OfficeAdobe

10-7 Tree Definition Tree: a set of elements of the same type such that It is empty Or, it has a distinguished element called the root from which descend zero or more trees (subtrees) What kind of definition is this? What is the base case? What is the recursive part?

10-8 Tree Definition Subtrees of the root Root

10-9 Tree Terminology Leaf nodes Root Interior nodes

10-10 Tree Terminology Nodes: the elements in the tree Edges: connections between nodes Root: the distinguished element that is the origin of the tree There is only one root node in a tree Leaf node: a node without an edge to another node Interior node: a node that is not a leaf node Empty tree has no nodes and no edges

10-11 Parent or predecessor: the node directly above in the hierarchy A node can have only one parent Child or successor: a node directly below in the hierarchy Siblings: nodes that have the same parent Ancestors of a node: its parent, the parent of its parent, etc. Descendants of a node: its children, the children of its children, etc. Tree Terminology

10-12 Height of a Tree A path is a sequence of edges leading from one node to another Length of a path: number of edges on the path Height of a (non-empty) tree : length of the longest path from the root to a leaf What is the height of a tree that has only a root node? By convention, the height of an empty tree is -1

10-13 Level of a Node Level of a node : number of edges between root and node It can be defined recursively: Level of root node is 0 Level of a node that is not the root node is level of its parent + 1 Question: What is the level of a node in terms of path length? Question: What is the height of a tree in terms of levels?

10-14 Level of a Node Level 0 Level 1 Level 2 Level 3

10-15 Subtrees Subtree of a node: consists of a child node and all its descendants A subtree is itself a tree A node may have many subtrees

10-16 Subtrees Subtrees of the root node

10-17 Subtrees Subtrees of the node labeled E E

10-18 More Tree Terminology Degree or arity of a node: the number of children it has Degree or arity of a tree: the maximum of the degrees of the tree’s nodes

10-19 Binary Trees General tree: a tree each of whose nodes may have any number of children n-ary tree: a tree each of whose nodes may have no more than n children Binary tree: a tree each of whose nodes may have no more than 2 children i.e. a binary tree is a tree with degree (arity) 2 The children (if present) are called the left child and right child

10-20 Recursive definition of a binary tree: it is The empty tree Or, a tree which has a root whose left and right subtrees are binary trees A binary tree is a positional tree, i.e. it matters whether the subtree is left or right Binary Trees

10-21 Binary Tree A IH DE B F C G

Binary Trees Every node in a binary tree can have at most two children. The two children of each node are called the left child and right child corresponding to their positions. A node can have only a left child or only a right child or it can have no children at all. Left child is always less that its parent, while right child is greater than its parent in a binary search tree

Representing Tree in Java Similar to Linked List with 2 Links – Store the nodes at unrelated locations in memory and connect them using references in each node that point to its children. Can also be represented as an array, with nodes in specific positions stored in corresponding positions in the array.

24 Binary Tree Nodes

Array implementation

10-26 Tree Traversals A traversal of a tree requires that each node of the tree be visited once Example: a typical reason to traverse a tree is to display the data stored at each node of the tree Standard traversal orderings: preorder inorder postorder level-order

10-27 Traversals A IH DE B F C G We’ll trace the different traversals using this tree; recursive calls, returns, and “visits” will be numbered in the order they occur

10-28 Preorder Traversal Start at the root Visit each node, followed by its children; we will choose to visit left child before right Recursive algorithm for preorder traversal: If tree is not empty, Visit root node of tree Perform preorder traversal of its left subtree Perform preorder traversal of its right subtree What is the base case? What is the recursive part?

10-29 Preorder Traversal A IH DE B F C G Nodes are visited in the order ABDHECFIG

10-30 Inorder Traversal Start at the root Visit the left child of each node, then the node, then any remaining nodes Recursive algorithm for inorder traversal If tree is not empty, Perform inorder traversal of left subtree of root Visit root node of tree Perform inorder traversal of its right subtree

10-31 Inorder Traversal A IH DE B F C G Inorder:Nodes are visited in the order DHBEAIFCG

10-32 Postorder Traversal Start at the root Visit the children of each node, then the node Recursive algorithm for postorder traversal If tree is not empty, Perform postorder traversal of left subtree of root Perform postorder traversal of right subtree of root Visit root node of tree

10-33 Postorder Traversals A IH DE B F C G Postorder:Nodes are visited in the order HDEBIFGCA

10-34 Discussion Note that the relative order of the recursive calls in preorder, inorder and postorder traversals is the same The only differences stem from where the visiting of the root node of a subtree actually takes place

10-35 Level Order Traversal Start at the root Visit the nodes at each level, from left to right Is there a recursive algorithm for a level order traversal?

10-36 Level Order Traversal A IH DE B F C G Level Order:Nodes will be visited in the order ABCDEFGHI

Finding a Node To find a node given its key value, start from the root. If the key value is same as the node, then node is found. If key is greater than node, search the right subtree, else search the left subtree. Continue till the node is found or the entire tree is traversed. Time required to find a node depends on how many levels down it is situated, i.e. O(log N).

Inserting a Node To insert a node we must first find the place to insert it. Follow the path from the root to the appropriate node, which will be the parent of the new node. When this parent is found, the new node is connected as its left or right child, depending on whether the new node’s key is less or greater than that of the parent. What is the complexity?

Finding Maximum and Minimum Values For the minimum, – go to the left child of the root and keep going to the left child until you come to a leaf node. This node is the minimum. For the maximum, – go to the right child of the root and keep going to the right child until you come to a leaf node. This node is the maximum.

Deleting a Node Start by finding the node you want to delete. Then there are three cases to consider: 1.The node to be deleted is a leaf 2.The node to be deleted has one child 3.The node to be deleted has two children

Deletion cases: Leaf Node To delete a leaf node, simply change the appropriate child field in the node’s parent to point to null, instead of to the node. The node still exists, but is no longer a part of the tree. Because of garbage collection feature of most programming languages, the node need not be deleted explicitly.

Deletion: One Child The node to be deleted in this case has only two connections: to its parent and to its only child. Connect the child of the node to the node’s parent, thus cutting off the connection between the node and its child, and between the node and its parent.

Deletion: Two Children

To delete a node with two children, replace the node with its inorder successor. For each node, the node with the next-highest key (to the deleted node) in the subtree is called its inorder successor. To find the successor, – start with the original (deleted) node’s right child. – Then go to this node’s left child and then to its left child and so on, following down the path of left children. – The last left child in this path is the successor of the original node.

Find successor

Delete a node with subtree (case 1) Successor is a leaf node

Delete a node with subtree (case 2) Deletion when the successor is the right child.

Delete a node with subtree (case 3)

Delete a node with subtree (case 1) If the right child of the original node has no left child, this right child is itself the successor. The successor can be the right child or it can be one of this right child’s descendants. If the node to be deleted is the root, set the root to the successor. Else the node can be either a right child or a left child. In this case set the appropriate field in its parent to point to the successor. After this set the left child of the successor to point to the node’s left child.

If successor is a left descendent of the right child of the node to be deleted, perform the following steps: -- Plug the right child of the successor into the left child of the successor’s parent. -- Plug the right child of the node to be deleted into the right child of the successor. -- Unplug the node from the right child of its parent and set this field to point to the successor. -- Unplug the node’s left child and plug it into the left child of the successor.

Efficiency Assume number of nodes N and number of levels L. N = 2 L -1 N+1 = 2 L L = log(N+1) The time needed to carry out the common tree operations is proportional to the base 2 log of N O(log N) time is required for these operations.

Unbalanced Trees Some trees can be unbalanced. They have most of their nodes on one side of the root or the other. Individual subtrees may also be unbalanced. Trees become unbalanced because of the order in which the data items are inserted. If the key values are inserted in ascending or descending order the tree will be unbalanced.

Unbalanced Trees Items inserted in ascending order If you insert a series of nodes whose keys are in either ascending or descending order, the result will be something like that below.

Unbalanced Trees The nodes arrange themselves in a line with no branches. Because each node is larger than the previously inserted one, every node is a right child, so all the nodes are on one side of the root. The tree is maximally unbalanced. If you inserted items in descending order, every node would be the left child of its parent, and the tree would be unbalanced on the other side.

Unbalanced Trees When there are no branches, the tree becomes, in effect, a linked list. The arrangement of data is one-dimensional instead of two-dimensional. Unfortunately, as with a linked list, you must now search through (on the average) half the items to find the one you’re looking for. In this situation, the speed of searching is reduced to O(N), instead of O(logN) as it is for a balanced tree.

10-57 Using Binary Trees: Expression Trees Programs that manipulate or evaluate arithmetic expressions can use binary trees to hold the expressions An expression tree represents an arithmetic expression such as (5 – 3) * / 2 Root node and interior nodes contain operations Leaf nodes contain operands

10-58 Example: An Expression Tree / (5 – 3) * / 2 4 * 9 2

10-59 Evaluating Expression Trees We can use an expression tree to evaluate an expression We start the evaluation at the bottom left What kind of traversal is this?

10-60 Evaluating an Expression Tree - 578/ 29 * This tree represents the expression (9 / 2 + 7) * (8 – 5) Evaluation is based on postorder traversal: If root node is a leaf, return the associated value. Recursively evaluate expression in left subtree. Recursively evaluate expression in right subtree. Perform operation in root node on these two values, and return result. +