Www.hndit.com Introduction to Trees IT12112 Lecture 05.

Slides:



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

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
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.
1 Introduction to Binary Trees. 2 Background All data structures examined so far are linear data structures. Each element in a linear data structure has.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Binary Trees Chapter 6.
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.
Chapter 11 A Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 A-2 Terminology A tree consists of vertices and edges, –An edge connects to.
Tree.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
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.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
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.
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.
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
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.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
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.
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.
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.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Trees Another Abstract Data Type (ADT)
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Csc 2720 Instructor: Zhuojun Duan
CMSC 341 Introduction to Trees.
Data Structures & Algorithm Design
Lecture 18. Basics and types of Trees
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Tonga Institute of Higher Education
i206: Lecture 13: Recursion, continued Trees
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
TREES General trees Binary trees Binary search trees AVL trees
Trees and Binary Trees.
Trees Another Abstract Data Type (ADT)
Trees.
Trees Another Abstract Data Type (ADT)
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
Trees.
Binary Tree Properties & Representation
CSC 205 – Java Programming II
Binary Trees, Binary Search Trees
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Cs212: Data Structures Lecture 7: Tree_Part1
Presentation transcript:

www.hndit.com Introduction to Trees IT12112 Lecture 05

Introduction www.hndit.com Tree is one of the most important non-linear data structures in computing. It allows us to implement faster algorithms( compared with algorithms using linear data structures). Application areas are : A popular one is the directory structure in many common operating systems, including Unix and DOS etc. Almost all operating systems store files in trees or tree like structures. Compiler Design/Text processing Searching Algorithms Evaluating a mathematical expression. Analysis of electrical circuits.

Introduction to Tree www.hndit.com 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.

Tree characteristics Consists of nodes connected by edges. www.hndit.com Consists of nodes connected by edges. Nodes often represent entities (complex objects) such as people, car parts etc. Edges between the nodes represent the way the nodes are related. Its easy for a program to get from one node to another if there is a line connecting them. The only way to get from node to node is to follow a path along the edges.

Tree Terminology Path: Traversal from node to node along the edges results in a sequence called path. Root: Node at the top of the tree. Parent: Any node, except root has exactly one edge running upward to another node. The node above it is called parent. Child: Any node may have one or more lines running downward to other nodes. Nodes below are children. Leaf: A node that has no children. Subtree: Any node can be considered to be the root of a subtree, which consists of its children and its children’s children and so on.

Tree Terminology (cont.) www.hndit.com Sibling Ancestor Descendant Path of two nodes Length of a path

Tree Terminology (cont.) www.hndit.com A C B D E F Node or vertex - the labeled squares Edge -the connecting lines In the General tree to the right: B is the child of A - A is parent of B B and C are siblings A is the root of the tree B and its children (D, E, F) are a subtree of A The parent-child relationship is generalized as ancestor -descendant. B is a descendant of A - A is ancestor to B

Tree Terminology (cont.) www.hndit.com Visiting: A node is visited when program control arrives at the node, usually for processing. Traversing: To traverse a tree means to visit all the nodes in some specified order. Levels: The level of a particular node refers to how many generations the node is from the root. Root is assumed to be level 0. Keys: Key value is used to search for the item or perform other operations on it.

Trees E.g. mystuff Jane home work Mat Jennifer Brian games teaching www.hndit.com mystuff Jane home work Mat Jennifer Brian games teaching research Susan Jerry Jack Jane’s children and grandchildren DAS.ppt OS.ppt File organization in a computer

Tree Types Binary tree — each node has at most two children www.hndit.com Binary tree — each node has at most two children General tree — each node can have an arbitrary number of children. Binary search trees AVL trees

General Trees. Trees can be defined in two ways : Recursive www.hndit.com Trees can be defined in two ways : Recursive Non- recursive One natural way to define a tree is recursively

General trees-Definition www.hndit.com A tree is a collection of nodes. The collection can be empty; otherwise a tree consists of a distinguish node r, called root, and zero or more non-empty (sub)trees T1,T2,T3,….TK. . Each of whose roots are connected by a directed edge from r.

General trees-Definition www.hndit.com A tree consists of set of nodes and set of edges that connected pair of nodes. A B C D E F G H J I K

E.g. A table of contents and its tree representation www.hndit.com Book C1 S1.1 S1.2 C2 S2.1 S2.1.1 S2.1.2 S2.2 S2.3 C3 Book C3 C1 C2 S2.3 S1.1 S1.2 S2.1 S2.2 S2.1.1 S2.1.2

Degree : The number of sub tree of a node is called its degree. www.hndit.com Degree : The number of sub tree of a node is called its degree. E.g. Degree of book  3, C12,C30 Nodes that have degree 0 is called Leaf or Terminal node. Other nodes called non-terminal nodes. E.g.Leaf nodes :C3,S1.1,S1.2 etc. Book is said to be the father (parent) of C1,C2,C3 and C1,C2,C3 are said to be sons (children ) of book. Children of the same parent are said to be siblings. E.g.C1,C2,C3 are siblings (Brothers) Length : The length of a path is one less than the number of nodes in the path. E.g. path from book to s1.1=3-1=2

If there is a path from node a to node b , then a is an ancestor of b and b is descendent of a. In above example, the ancestor of S2.1are itself,C2 and book, while it descendent are itself, S2.1.1 and S2.1.2. An ancestor or descendent of a node, other than the node itself is called a proper ancestor or proper descendent. Height of a tree — the maximum depth of a leaf node. ..[ In above example height=3] Depth of a node — the length of the path between the root and the node.[In above example node C1 has Depth 1, node S2.1.2 has Depth 3.etc.] www.hndit.com

Binary Trees www.hndit.com A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtree and the right subtree. Any tree can be transformed into binary tree. by left child-right sibling representation The left subtree and the right subtree are distinguished.

Left child-right child tree representation of a tree www.hndit.com A B C E D K F G H L M I J

Samples of Trees Complete Binary Tree A A A 1 B B 2 B C C www.hndit.com Complete Binary Tree A A A 1 B B 2 B C C Skewed Binary Tree 3 D E F G D 4 H I E 5

Trees A rooted tree is a connected, acyclic, undirected graph www.hndit.com A rooted tree is a connected, acyclic, undirected graph

Trees www.hndit.com A is the root node. B is the parent of D and E. A is ancestor of D and E. D and E are descendants of A. C is the sibling of B D and E are the children of B. D, E, F, G, I are leaves.

Trees A, B, C, H are internal nodes The depth (level) of E is 2 www.hndit.com A, B, C, H are internal nodes The depth (level) of E is 2 The height of the tree is 3 The degree of node B is 2

Binary Tree www.hndit.com Binary tree: ordered tree with all internal nodes of degree 2

Representing Rooted Trees www.hndit.com BinaryTree: Parent: BinaryTree LeftChild: BinaryTree RightChild: BinaryTree Root Æ Æ Æ Æ Æ Æ Æ Æ Æ Æ Æ

B-Tree www.hndit.com

Binary Trees www.hndit.com 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.

Binary Trees www.hndit.com 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 Property2: a unique path exists from the root to every other node

Binary Search Trees Binary Search Tree Property: www.hndit.com Binary Search Tree Property: In a binary search tree, the left subtree contains key values less than the root the right subtree contains key values greater than or equal to the root. Each subtree is itself a binary search tree.

Searching a binary search tree www.hndit.com (1) Start at the root (2) Compare the value of the item you are searching for with the value stored at the root (3) If the values are equal, then item found; otherwise, if it is a leaf node, then not found

Searching a binary search tree (cont.) www.hndit.com (4) If it is less than the value stored at the root, then search the left subtree (5) If it is greater than the value stored at the root, then search the right subtree (6) Repeat steps 2-6 for the root of the subtree chosen in the previous step 4 or 5 Is this better than searching a linked list? Yes !! ---> O(logN)

Tree node structure struct TreeNode { ItemType info; TreeNode* left; www.hndit.com struct TreeNode { ItemType info; TreeNode* left; TreeNode* right; };

Function InsertItem Use the binary search tree property to insert the new item at the correct place

Does the order of inserting elements into a tree matter? www.hndit.com Does the order of inserting elements into a tree matter? (cont.)

Function DeleteItem First, find the item; then, delete it www.hndit.com First, find the item; then, delete it Important: binary search tree property must be preserved!! We need to consider three different cases:  (1) Deleting a leaf (2) Deleting a node with only one child (3) Deleting a node with two children

(1) Deleting a leaf www.hndit.com

(2) Deleting a node with only one child www.hndit.com (2) Deleting a node with only one child

(3) Deleting a node with two children www.hndit.com

(3) Deleting a node with two children (cont.) www.hndit.com Find predecessor (it is the rightmost node in the left subtree) Replace the data of the node to be deleted with predecessor's data Delete predecessor node

Representing Tree in C++ www.hndit.com Similar to Linked List but 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.

Array implementation www.hndit.com

Binary Tree Definition www.hndit.com T is a binary tree if either: T has no nodes, or T is of the form: where R is a node and TL and TR are both binary trees. R TL TR if R is the root of T then : TL is the left subtree of R - if it is not null then its root is the left child of R TR is the right subtree of R - if it is not null then its root is the right child of R

Full Binary Trees In a full binary tree: www.hndit.com In a full binary tree: All nodes have two children except leaf nodes. All leaf nodes are located in the lowest level of the tree. A B G C D E F

Complete Binary Trees In a complete binary tree: www.hndit.com In a complete binary tree: All nodes have two children except those in the bottom two levels. The bottom level is filled from left to right. A B C D E F G H I G

Binary Search Trees www.hndit.com A binary search tree represents a hierarchy of elements that are arranged by size: For any node n: n's value is greater than any value in its left subtree n's value is less than any value in its right subtree F D I B E H K A C G J

Binary Expression Trees www.hndit.com A binary expression tree represents an arithmetic expression: For any node n: if n is a leaf node: it must contain an operand. if n is not a leaf node: it must contain an operator. it must have two subtrees. * + - d b c a This tree represents the expression: (a + b) * (c -d) or a b + c d -*

Maximum Number of Nodes in BT www.hndit.com The maximum number of nodes on level i of a binary tree is 2i-1, i>=1. The maximum nubmer of nodes in a binary tree of depth k is 2k-1, k>=1. i-1

Traversing the Tree www.hndit.com There are three possible traversals of binary trees that differ only in the order that they perform the 3 basic operations. Three simple ways to traverse a tree: Inorder Preorder Postorder

Inorder Traversing www.hndit.com Inorder traversal will cause all the nodes to be visited in ascending order. Steps involved in Inorder traversal (recursion) are: -- Call itself to traverse the node’s left subtree -- Visit the node (e.g. display a key) -- Call itself to traverse the node’s right subtree.

Tree Traversal (continued) www.hndit.com Sequence of preorder traversal: e.g. use for infix parse tree to generate prefix -- Visit the node -- Call itself to traverse the node’s left subtree -- Call itself to traverse the node’s right subtree Sequence of postorder traversal: e.g. use for infix parse tree to generate postfix -- Visit the node.

Inorder Traversal www.hndit.com inorder(binTree tree){ // inorder traversal of tree if(tree != null){ inorder(left subtree of tree) print tree's data inorder(right subtree of tree) } * + - d b c a For this tree produces: a + b * c - d

Postorder Traversal www.hndit.com postorder(binTree tree){ //postorder traversal of tree if(tree != null){ postorder(left subtree of tree) postorder(right subtree of tree) print tree's data } * + - d b c a For this tree produces: a b + c d - *

Preorder Traversal www.hndit.com preorder(binTree tree){ // preorder traversal of tree if(tree != null){ print tree's data preorder(left subtree of tree) preorder(right subtree of tree) } * + - d b c a For this tree produces: * + a b - c d

Tree Traversals www.hndit.com

Finding a Node www.hndit.com 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 www.hndit.com 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.

Finding Maximum and Minimum Values www.hndit.com 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.