CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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,
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Tree Binary Tree. ARRAYS 1. Good Data structure for Searching algorithms. 2. Disadvantage : Insertion and Deletion of Elements require Data movements(Time.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
© 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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
10. Binary Trees A. Introduction: Searching a linked list.
Binary Search Trees Chapter 6.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
BINARY TREES && TREE TRAVERSALS. DEFINITION : Binary Tree A binary tree is made of nodes Each node contains –a "left" pointer -- left child –a "right"
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
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.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
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 ),
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Searching:
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Tree Data Structures. Introductory Examples Willliam Willliam BillMary Curt Marjorie Richard Anne Data organization such that items of information are.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 10. Binary Trees Read Sec A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Search: Binary Search Trees Dr. Yingwu Zhu. Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n Assume == and.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
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.
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.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Trees Namiq Sultan. Trees Trees are very flexible, versatile and powerful non-liner data structure that can be used to represent data items possessing.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Binary Tree.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Search: Binary Search Trees Dr. Yingwu Zhu. Review: Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n – Assume.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Binary Search Trees Manolis Koubarakis Data Structures and Programming Techniques 1.
Trees Chapter 15.
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Trees.
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Searching: Binary Trees
Chapter 20: Binary Trees.
Trees.
Presentation transcript:

CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM

2 Trees A data structure which consists of a finite set of elements called nodes or vertices a finite set of directed arcs which connect the nodes If the tree is nonempty one of the nodes (the root) has no incoming arc every other node can be reached by following a unique sequence of consecutive arcs

3 Binary Trees Each node has at most two children Useful in modeling processes where a comparison or experiment has exactly two possible outcomes the test is performed repeatedly Example multiple coin tosses encoding/decoding messages in dots and dashes such as Mores code

4 Example of a Binary Tree Root Leaf Parents

5 Binary Trees each node may have 0, 1, or 2 children. node on the left as the left child and the node on the right as the right child Binary tree is a set of nodes that either the tree is empty, or the tree is partitioned into three disjoint subsets: a single node R, the root; two possible empty sets that are binary trees, called left and right subtrees of R

6 Binary Trees Degenerate tree occurs in which there is a single leaf node and each non-leaf node has only one child. It is equivalent to a linked list. A complete binary tree is a tree in which each level 0 to n-1 has full set of node and all leaf nodes at level n occupy the leftmost position in the tree.

7 Binary Trees - complete

8 Binary Trees - Degenerate tree

9 Binary Trees ARRAY IMPLEMENTATION

10 Binary Trees - Array Assume you have the below diagram:

11 Binary Trees - Array We can represent this binary tree using an array, arr.

12 Binary Trees - Array Eg: Locate parent of C C = A[9] Parents of C = A[9/2] = A[4] = B Locate right child of F F = A[5] = A[2[5] + 1] = A[11] = G

13 Binary Trees - Array Issue: You must prepare maximum array element to store the binary tree database on the depth (3 level = 2 3 = 8 element), even if your binary tree is not a complete binary tree. Many wasted empty spaces.

14 Binary Trees TREE TRAVERSAL

15 Tree Traversal Refers to the process of visiting all the nodes of the tree, in some specific sequence. In a tree, visiting a node generally means retrieving the data items contained in the nodes, and sending it to some process (printing, for example).

16 Tree Traversal Traversal orders : 1. Pre-order (VLR): the node is visited before the left and right subtrees. 2. In-order (LVR): the node is visited between the left and right subtrees. 3. Post-order (LRV): the node is visited after both of the left and right subtrees.

17 Preorder Traversal (VLR) 1. Visit the current node 2. Traverse the left sub- tree of the current node 3. Traverse the right sub- tree of the current node Traversal order: + - A * B C D

18 Preorder Traversal (VLR) Traversal order: ??

19 Inorder Traversal (LVR) 1. Traverse the left sub- tree of the current node 2. Visit the current node 3. Traverse the right sub-tree of the current node Traversal order: A – B * C + D

20 Inorder Traversal (LVR) Traversal order: ??

21 Postorder Traversal (LRV) 1. Traverse the left sub- tree of the current node 2. Traverse the right sub-tree of the current node 3. Visit the current node Traversal order: A B C * - D +

22 Postorder Traversal (LRV) Traversal order: ??

23 Exercise 1.Inorder? 2.Preorder? 3.Postorder? - + / x/ - w y z /p vu

24 Representation of Algebraic Expressions A binary tree can be used to represent an algebraic expression that involves only binary arithmetic operators +, -, /, and *. The root node holds an operator, and each of its sub-trees represents either a variable (operand), or another expression. The following tree represents the expression A*(B+C) * A+ BC

25 Representation of Algebraic Expressions (a + b) – (c + d) ? a + b – c + d ? ((5*(y-3)) + (2/(x+7))) ?

26 Binary Search Tree A binary search tree is a binary tree, with the additional condition that: If a node contains a value k, then every node in its left sub-tree contain a value less than k. And every node in its right sub-tree contains a value greater than or equal to k. The above condition implies that: Every left child must have a key less than its parent Every right child must have a key greater than or equal to its parent.

27 Example of a Binary Search Tree

28 Inserting a New Node The operation if inserting a new node proceeds in the same sequence as the search operation until it encounters a “NULL” New node is inserted and connected to the previous node as its parent’ Example insert node

29 Deleting a Node The node to be deleted is a leaf (has no children). Node is deleted straight from the binary tree. The node to be deleted has one child. Change the appropriate reference in the node’s parent to point to the child if the deleted node. The child along with its sub-trees, now take the place of the deleted node.

30 Deleting a Node The node to be deleted has two children. Step 1: Replace the node with its inorder successor. Since the node to be deleted has two children, it has a right sub-tree, and its inorder successor is the last left node in this sub-tree. Step 2: Since the inorder successor is the last left node in a sub-tree, it can not has a left child. Therefore it can have at most one child. If it has a right child, the right child will occupy the position of the inorder successor.

31 Example of deleting a node BEFORE deletion of node

32 Example of deleting a node AFTER deletion of node

33 EXERCISE Refer to exercise.doc

34 Binary Trees LINKED LIST IMPLEMENTATION

35 Linked List Implementation Data Structure: typedef struct treenode{ char entry; struct treenode *left, *right; }TreeNode; left right ** entry (char)

36 Linked List Implementation Create new tree: void CreateTree(TreeNode **root) { *root = NULL; } Check status of tree (empty): Boolean TreeEmpty(TreeNode *root) { return root == NULL; }

37 Linked List Implementation Create new node: TreeNode *MakeTreeNode(TreeEntry x) { TreeNode *p; p = (TreeNode *) malloc(sizeof(TreeNode)); if(!p) printf("Failed – not enough space"); else { p->left = NULL; p->right = NULL; p->entry = x; } return p; }

38 Linked List Implementation Traversal Orders of a binary tree (Preorder) void Preorder(TreeNode *root) { if(root) { printf("%c", root->entry); Preorder(root->left); Preorder(root->right); }

39 Linked List Implementation Traversal Orders of a binary tree (Inorder) void Inorder(TreeNode *root) { if(root) { Inorder(root->left); printf("%c", root->entry); Inorder(root->right); }

40 Linked List Implementation Traversal Orders of a binary tree (Postorder) void Postorder(TreeNode *root) { if(root) { Postorder(root->left); Postorder(root->right); printf("%c", root->entry); }

41 Linked List Implementation Tree Search TreeNode * TreeSearch(TreeNode *root, int target) { if(root) { if(target entry) root = TreeSearch(root->left,target); else if(target > root->entry) root = TreeSearch(root->right,target); } return root; }

42 Linked List Implementation Insert node TreeNode *InsertNode (TreeNode *root, TreeNode *newnode) { if (root == NULL) { root = newnode; root->left = root->right = NULL; } else if (newnode->entry entry) root->left = InsertNode(root->left, newnode); else root->right = InsertNode(root->right, newnode); return root; }

43 Linked List Implementation Delete node void DeleteNodeTree(TreeNode **p) { TreeNode *r,*q; r = *p; if(r == NULL) printf("Attempt to delete nonexistent node"); else if(r->right == NULL) { *p = r->left; free(r); }

44 Linked List Implementation Delete node void DeleteNodeTree(TreeNode **p) { else if(r->left == NULL) { *p = r->right; free(r); }

45 Linked List Implementation Delete node void DeleteNodeTree(TreeNode **p) { for(q = r->right; q->left; q = q->left) q->left = r->left; *p = r->right; free(r); }

46 Announcement Midterm Test Date : 22/09/2010 (Wednesday) Time : pm Venue : BW-3-L15/16 Topic : Chapter 1 – Chapter 4 Tips : Study quizzes and exercises

47 The End