Trees.

Slides:



Advertisements
Similar presentations
Introduction to Trees Chapter 6 Objectives
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
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Marc Smith and Jim Ten Eyck
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.
COSC2007 Data Structures II
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Trees, Binary Trees, and Binary Search Trees COMP171.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
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.
Data Structures TREES.
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.
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.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
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.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
CS 201 Data Structures and Algorithms
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.
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
Csc 2720 Instructor: Zhuojun Duan
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Lecture 18. Basics and types of Trees
ITEC 2620M Introduction to Data Structures
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Introduction to Trees IT12112 Lecture 05.
CE 221 Data Structures and Algorithms
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Introduction to Trees Chapter 6 Objectives
Presentation transcript:

Trees

Course Objectives At the end of the lesson students are expected to be able to: Understand the tree concept and terms related to tree. Identify characteristics of general tree, binary tree and binary search tree Identify basic operations of a tree such as tree traversals, insert node, delete node, searching. Understand and know how to apply and implement tree in problem solving and in programming.

1.0 Introduction to Tree

Introduction to tree - Definition Tree is a non-linear data structure Data in a tree is stored in hierarchy Example of tree application: Represent algebraic formulas Store data in hierarchy form. Ex: organization chart Artificial intelligence – information is accessed based on certain decision which is stored in a tree. algebraic formulas : (1+2)*3 organization chart

Introduction to tree – Decision Tree Binary tree associated with a decision process Internal nodes: questions with yes/no answer External nodes: decisions Example: dining decision Introduction to tree - Definition

Trees A tree is a collection of nodes and edges The collection can be empty (recursive definition) If not empty, a tree consists of a distinguished node r (the root), and zero or more nonempty subtrees T1, T2, ...., Tk, each of whose roots are connected by a directed edge from r

2.0 Tree Terminology

Tree terminology Any two vertices must have one and only one path between them else its not a tree Trees are hierarchical Parent-child relationship between two nodes Ancestor-descendant relationships among nodes

Tree terminology General tree A general tree T is a set of one or more nodes such that T is partitioned into disjoint subsets: A single node r, the root Sets that are general trees, called subtrees of r Subtree of a tree: Any node and its descendants

Terminology A general tree A subtree of the tree in general tree

Some Terminologies Root The only node in the tree with no parent A tree has only one root Root : A Child and parent Every node except the root has one parent  Parent of node n The node directly above node n in the tree A is Parent to B,C,D,E,F,G A node can have an arbitrary number of children Child of node n A node directly below node n in the tree B,C,D,E,F,G Are children of A

Some Terminologies Leaves Nodes with no children B,C,H,I,P,Q,K,L,M,N Sibling nodes with same parent P and Q are siblings Subtree of node n A tree that consists of a child (if any) of node n and the child’s descendants subtree of A Right subtree of E

Some Terminologies Ancestor of node n Descendant of node n A node on the path from the root to n Ancesstor P : J,E,A Nod A is ancestor for all node in the tree Descendant of node n A node on a path from n to a leaf Descendant E: I,J,P,Q All nodes in the tree are descendant to the root.

Some Terminologies Path Length Depth of a node sequence of nodes in which each node is adjacent o the next one. Example: Path from root to L: AFL, Path from root to P: AEJP Length number of edges on the path Length of Tree : 3 Depth of a node length of the unique path from the root to that node The depth of a tree is equal to the depth of the deepest leaf Depth of Tree : 3 Depth of K : 2

Some Terminologies Level of a node the distance from the node to the root. Root is at level 0. Node B,C,D,E,F,G are at level 1. Node H,I,J,K,L,M,N are at level 2, while node P and Q are at level 3. Height of a tree the highest level of leaf in a tree plus 1. The highest level of the tree in the figure is 3. Thus, the height of the tree is 4.

Some Terminologies Height of a node length of the longest path from that node to a leaf all leaves are at height 1 The height of a tree is equal to the height of the root or the number of nodes along the longest path from the root to a leaf Height 3 5 7

Trees - Example Level E R T L P M A S 1 2 3 root Child (of root) Leaves or terminal nodes Child (of root) Depth of T: 2 Tree height: 4 1 2 3

3.0 Binary Tree

Binary Trees A tree in which no node can have more than two children The depth of an “average” binary tree is considerably smaller than N, eventhough in the worst case, the depth can be as large as N – 1.

A Binary Tree A binary tree is a set T of nodes such that either T is empty, or T is partitioned into three disjoint subsets: A single node r, the root Two possibly empty sets that are binary trees, called the left subtree of r and the right subtree of r

A General Tree & A Binary Tree Figure 10-3 (a) An organization chart; (b) a family tree

Collection of Binary Trees Tree = NULL Empty Tree – a binary tree with no nodes. Tree One node – Level 0 A Tree Three nodes – Level 1 A B C Tree Skewed to left – Level 3 A B C Tree Skewed to right – Level 3 A B C

More Binary Trees Binary trees that represent algebraic expressions:

A Binary Search Tree A binary search tree A binary tree that has the following properties for each node n n’s value is > all values in n’s left subtree TL n’s value is < all values in n’s right subtree TR Both TL and TR are binary search trees

A Binary Search Tree A binary search tree of names

Full Binary Trees A binary tree of height h is full if Nodes at levels < h have two children each Recursive definition If T is empty, T is a full binary tree of height 0 If T is not empty and has height h > 0, T is a full binary tree if its root’s subtrees are both full binary trees of height h – 1 A full binary tree of height 3

Complete Binary Trees A binary tree of height h is complete if It is full to level h–2, and Level h is filled from left to right

Height of a Full Binary Tree At each level the number of the nodes is doubled. total number of nodes: 1 + 2 + 22 + 23 = 24 - 1 = 15

Nodes and Levels in a Full Binary Tree Number of the nodes in a tree with M levels: 1 + 2 + 22 + …. 2M = 2 (M+1) - 1 = 2*2M - 1 Let N be the number of the nodes. N = 2*2M - 1, 2*2M = N + 1 2M = (N+1)/2 M = log( (N+1)/2 ) N nodes : log( (N+1)/2 ) = O(log(N)) levels M levels: 2 (M+1) - 1 = O(2M ) nodes

Binary Tree Example A complete binary tree but not a full binary tree Not a full binary tree and not a complete binary tree Not a full binary tree and not a complete binary tree

Balanced Binary Trees A binary tree is balanced if the heights of any node’s two subtrees differ by no more than 1 Complete binary trees are balanced Full binary trees are complete and balanced Unbalanced tree : skewed to the right

Example: Expression Trees Leaves are operands (constants or variables) The other nodes (internal nodes) contain operators Will not be a binary tree if some operators are not binary

Exercise Week 13_1 Is the tree complete?Yes/NO Is the tree full? Yes/No Is the tree balances? Yes/No List the nodes that have D as an ancestor. List the children of J. Determine the height of the tree? Determine the level of node A. List the leave nodes. Identify the root node. Identify the siblings of B.

Tree traversal A traversal visits each node in a tree You do something with the node during a visit For example, display the data in the node Used to print out the data in a tree in a certain order Type of Traversal Inorder traversal Preorder traversal Postordertraversal

Pre-order traversal Pre-order traversal prefix expression Print the data at the root Recursively print out all data in the left subtree Recursively print out all data in the right subtree prefix expression ++a*bc*+*defg

Postorder traversal Postorder traversal Postfix expression Recursively print out all data in the left subtree Recursively print out all data in the right subtree Print the data at the root Postfix expression abc*+de*f+g*+

Inorder traversal Inorder traversal infix expression Recursively print out all data in the left subtree Print the data at the root Recursively print out all data in the right subtree infix expression a+b*c+d*e+f*g

Traversals of a Binary Tree

Exercise Week 13_2 Give the inorder, preorder and postorder traversal of the binary search tree.

Summary Binary trees provide a hierarchical organization of data The implementation of a binary tree is usually pointer-based If the binary tree is complete, an efficient array-based implementation is possible Traversing a tree to “visit”—that is, do something to or with—each node is useful You pass a client-defined “visit” function to the traversal operation to customize its effect on the items in the tree

Summary The binary search tree allows you to use a binary search- like algorithm to search for an item having a specified value Binary search trees come in many shapes The height of a binary search tree with n nodes can range from a minimum of log2(n + 1) to a maximum of n The shape of a binary search tree determines the efficiency of its operations

Summary An inorder traversal of a binary search tree visits the tree’s nodes in sorted search-key order The treesort algorithm efficiently sorts an array by using the binary search tree’s insertion and traversal operations