Lecture 7. Binary Trees
|
|
- Aubrie Hines
- 6 years ago
- Views:
Transcription
1 Lecture 7. Binary Trees Instructor: 罗国杰 School of EECS Peking University
2 Outline Preliminaries of trees Basic concepts Example: trees in a file system Binary trees Full, complete, extended ~ Properties Traversals 2
3 Preliminaries A tree is a collection of nodes and edges An edge is a ordered pair of nodes <u,v> Recursive definition An empty collection is a tree A single node collection is a tree This node is the root of this tree T is a tree, if T consist of a node r, non-empty subtrees T 1, T 2,, T k, and edges <r,r 1 >, <r,r 2 >,, <r,r k > where r 1, r 2, r k are the roots of the subtress, respectively. The distinguished node r is the root of T 3
4 Preliminaries Parent Child Node A is the parent of node B, if B is the root of one subtree of A. Node B is the child of node A, if A is the parent of B. Sibling Nodes with the same parent are siblings 4
5 Preliminaries Leaf A node is called a leaf if it has no children Internal node Path A nodes that a neither a leaf nor the root The sequence <k 0,k 1,,k s > is a path, if and only if <k 0,k 1 >, <k 1,k 2 >, <k s-1,k s > are edges 5
6 Preliminaries Ancestor (Recursively) node A is an ancestor of node B if A is either the parent of B, or A is the parent of some ancestor of B Descendant Node B is the descendant of node A, if A is an ancestor of node B If there is a path from j to k, j is k s ancestor and k is j s descendant 6
7 Preliminaries Depth (level) of a Node Depth of the root of a tree is 0; and the depth of any other node in the tree is one more than the depth of its parent (The length of path starting from the root) Depth (height, or height-1) of a Tree The depth of a tree is the maximum depth of any leaf in the tree (The longest path starting from the root) 7
8 Example: Unix File System 8
9 Example: Unix File System Static void ListDir(DirectoryOrFile D, int Depth) { if (D is a legitimate entry) { PrintName(D, Depth); if (D is a directory) for each child, C, of D ListDir(C, Depth +1); } } void ListDirectory(DirectoryOrFile D) { ListDir(D, 0); } 9
10 Example: Unix File System Static int SizeDirectory(DirectoryOrFile D) { int TotalSize = 0; if (D is a legitimate entry) { TotalSize = FileSize(D); if (D is a directory) for each child, C, of D TotalSize += SizeDirectory(C); } return TotalSize; } 10
11 Binary Trees Special binary trees Full ~ Complete ~ Extended ~ Properties of binary trees
12 Binary Trees: Definition Binary tree: a finite set of nodes An empty set is a binary tree A set is a binary tree, if it consists of one root node and two non-intersecting subtrees, named left subtree and right subtree, respectively 12
13 Binary Trees: Five Basic Forms 13
14 Full and Complete Binary Trees Full binary tree Every node other than the leaves has exactly two children Complete binary tree Every level, except possibly the last, is completed filled, and all nodes are as far left as possible A A B C B C D E D E F G F G H I J K L (A) 满二叉树 (b) 完全二叉树 14
15 Complete Binary Trees Properties Leaves are only presented in the bottommost two levels In all binary trees with the same number of nodes, a complete binary has the minimum internal path length Internal path length: the sum of the depths of all nodes in a tree 15
16 Extended Binary Trees
17 Extended Binary Trees Special nodes (null trees) are added Add one null leaf at every original internal node with only one child Add two null leaves at every original leaf (so that each original node has two children) A transformation into a full binary tree The number of null trees added equals the number of nodes in the original tree plus one 17
18 Extended Binary Trees Internal nodes: the nodes in the original tree External nodes: the null trees added Internal path length (I) the sum of the depths of all internal nodes in the extended binary tree External path length (E) the sum of the depths of all external nodes in the extended binary tree Property: E = I + 2n where n is the number of internal nodes 18
19 Extended Binary Trees For this example, E = = 39 I = = 19 It satisfies E = I + 2n where n = 10 19
20 Extended Binary Trees E = I + 2n (proof by induction) When n=1, we have I=0 and E=2, thus E=I+2n Assume E n = I n + 2n is satisfied, For an extended binary tree with (n+1) nodes Replace an original leaf with depth k by a null tree to get an extended binary tree with n nodes I n = I n+1 k E n = E n+1 2(k+1) + k = E n+1 k 2 According to these three equations, We get E n+1 = I n+1 + 2(n+1) 20
21 Properties of Binary Trees Property 1. There are at most 2 i nodes with depth i in a binary tree (i 0) (proof by induction) Property 2. A binary tree with depth k has at most 2 k+1-1 nodes (k 0) According to property 1, k n å 2 i = 2 k+1-1 i=0 21
22 Properties of Binary Trees Property 3. If a binary tree has n 0 leaves and n 2 nodes with two children, we have n 0 =n 2 +1 Proof. n = n 0 + n 1 + n 2 where there are n 1 nodes with only one child n = e + 1 = n 1 + 2n where e is the number of edges Thus, n 0 = n
23 Properties of Binary Trees Property 4. (Full Binary Tree Theorem) A non-empty full binary tree has the number of leaves equal to the number of internal nodes plus 1 (use property 3) Property 5. A non-empty binary tree has the number of null trees equal to the number of nodes plus 1 (use property 3) 23
24 Properties of Binary Trees Property 6. A complete binary tree with n nodes has depth k = log 2 (n+1) - 1 Proof. According to property 2 and the definition of completeness 2 k -1 < n 2 k+1 1 k < log 2 (n+1) k+1 d is an integer Thus, k = log 2 (n+1)
25 Properties of Binary Trees Property 7. If a complete binary tree has n nodes, and the nodes are labeled from left to right level-bylevel, we have the following for node i (0 i n-1) (1) If i = 0, node i is the root; if i > 0, its parent has label (i- 1)/2. (2) If 2i+1 n-1, the left child of i is (2i+1); otherwise node i has no left child. If 2i+2 n-1, the right child of i is (2i+2); otherwise node i has no right child. (3) If i is even and 0<i<n, the left sibling of i is (i-1); otherwise node i has no left sibling. If i is odd and i+1 < n, the right sibling of i is (i+1); otherwise node i has no right sibling. 25
26 Binary Tree ADT Abstract data type Depth-first search Breath-first search 26
27 Abstract Data Types Operations focus on accessing the information on the nodes access its left child, right child, or parent, or fetch the data on the node itself Some applications require a traversal The ADT defines the basic operations The ADT is independent of storage structures 27
28 Abstract Data Types: Node template <class T> class BinaryTreeNode { friend class BinaryTree<T>; // to access private members private: T info; // node data public: BinaryTreeNode(); // default constructor BinaryTreeNode(const T& ele); // constructor given the data BinaryTreeNode(const T& ele, // constructor given substrees BinaryTreeNode<T> *l, BinaryTreeNode<T> *r); 28
29 Abstract Data Types: Node }; T value() const; void setvalue(const T& val); BinaryTreeNode<T>* leftchild() const; BinaryTreeNode<T>* rightchild() const; void setleftchild(binarytreenode<t>*); void setrightchild(binarytreenode<t>*); bool isleaf() const; BinaryTreeNode<T>& operator = ( const BinaryTreeNode<T>& Node); 29
30 Abstract Data Types: Tree template <class T> class BinaryTree { private: BinaryTreeNode<T>* root; public: BinaryTree() { root = NULL; }; ~BinaryTree() { DeleteBinaryTree(root); } bool isempty() const; BinaryTreeNode<T>* Root() { return root; } 30
31 Abstract Data Types: Tree }; BinaryTreeNode<T>* Parent(BinaryTreeNode<T> *current); BinaryTreeNode<T>* LeftSibling(BinaryTreeNode<T> *current); BinaryTreeNode<T>* RightSibling(BinaryTreeNode<T> *current); void CreateTree(const T& info, BinaryTree<T>& lefttree, BinaryTree<T>& righttree); void PreOrder(BinaryTreeNode<T> *root); void InOrder(BinaryTreeNode<T> *root); void PostOrder(BinaryTreeNode<T> *root); void LevelOrder(BinaryTreeNode<T> *root); void DeleteBinaryTree(BinaryTreeNode<T> *root); 31
32 Traversal Traversal of a binary tree is to access the nodes sequentially in a certain order, such that each node is accessed exactly once The access can be read/write data from/to the nodes The traversal is essentially a linearization of the nodes in a binary treee 32
33 Depth-First Traversal Recursively define 3 strategies Preorder traversal (tlr order) Visit the root Traverse the left subtree in preorder Traverse the right subtree in preorder Inorder traversal (LtR order) Traverse the left subtree in inorder Visit the root Traverse the right subtree in inorder Postorder traversal (LRt order) Traverse the left subtree in postorder Traverse the right subtree in a postorder Visit the root 33
34 Traversal: Example Example + - H / * + * E - A B C D F G 34
35 Traversal: Example Preorder: +-/+AB*CD*E-FGH Inorder: A+B/C*D-E*F-G+H Postorder: AB+CD*/EFG-*-H H / * * E - A B C D F G 35
36 Traversal: Example Traversal of an expression tree Preorder: +-/+AB*CD*E-FGH Prefix expression (Polish notation) Inorder: A+B/C*D-E*F-G+H Infix expression Postorder: AB+CD*/EFG-*-H+ postfix expression (reverse Polish notation) 36
37 Depth-First Traversal template<class T> void BinaryTree<T>::PreOrder (BinaryTreeNode<T> *root) { if (root!= NULL) { Visit(root->value()); PreOrder(root->leftchild()); PreOrder(root->rightchild()); } } template<class T> void BinaryTree<T>:: InOrder (BinaryTreeNode<T> *root) { if (root!= NULL) { InOrder (root->leftchild()); 37
38 Depth-First Traversal Visit(root->value()); InOrder(root->rightchild()); } } template<class T> void BinaryTree<T>:: PostOrder (BinaryTreeNode<T> *root) { if (root!= NULL) { PostOrder(root->leftchild()); PostOrder (root->rightchild()); Visit(root->value()); } } 38
39 Depth-First Traversal Nonrecursive depth-first traversal Although the recursive algorithm is concise, there may be cases that do not pay recursion Solution: use a stack to simulate the recursive traversal Main idea of preorder traversal Visit a node, push the nonempty right child in the stack, and travers its left child When the traversal of its left child is finished, pop the nodes from the stack, and continue traversal 39
40 Depth-First Traversal: Preorder without Recursion template<class T> void BinaryTree<T>::PreOrderWithoutRecursion( BinaryTreeNode<T> *root) { using std::stack; stack<binarytreenode<t>* > astack; BinaryTreeNode<T> *pointer = root; astack.push(null); // push a dummy node while (pointer) { // or check!astack.empty() Visit(pointer->value()); 40
41 Depth-First Traversal: Preorder without Recursion } if (pointer->rightchild()!= NULL) astack.push(pointer->rightchild()); if (pointer->leftchild()!= NULL) pointer = pointer->leftchild(); else { // visit the right subtree pointer=astack.top(); astack.pop(); } } 41
42 Depth-First Traversal: Inorder without Recursion Main idea Push a node in the stack, before traversing its left subtree Pop the node and visit it after traversing the left subtree Traverse the right subtree 42
43 Depth-First Traversal: Inorder without Recursion template<class T> void BinaryTree<T>::InOrderWithoutRecursion( BinaryTreeNode<T> *root) { using std::stack; stack<binarytreenode<t>* > astack; BinaryTreeNode<T> *pointer = root; while (!astack.empty() pointer) { if (pointer) { 43
44 Depth-First Traversal: Inorder without Recursion } } } } else { astack.push(pointer); // descend from the left child pointer = pointer->leftchild(); pointer = astack.top(); astack.pop(); Visit(pointer->value()); // descend from the right child pointer = pointer->rightchild(); 44
45 Depth-First Traversal: Postorder without Recursion Main idea Push a node in the stack before traversing its left subtree Traverse the right subtree after traversing the left subtree Pop the node and visit it after traversing the right subtree How do you know which subtree has just been traversed? 45
46 Depth-First Traversal: Postorder without Recursion Solution Use an additional tag to indicate whether the right subtree is traversed Tags = {Left, Right} The left subtree has been traversed if tag == Left The right subtree has been traversed if tag == right 46
47 Depth-First Traversal: Postorder without Recursion enum Tags{Left,Right}; template <class T> class StackElement { public: BinaryTreeNode<T>* pointer; Tags tag; }; template<class T> void BinaryTree<T>::PostOrderWithoutRecursion( BinaryTreeNode<T>* root) { using std::stack; StackElement<T> element; 47
48 Depth-First Traversal: Postorder without Recursion stack<stackelement<t > > astack; BinaryTreeNode<T>* pointer; if (root == NULL) return; else pointer = root; while (!astack.empty() pointer) { while (pointer!= NULL) { element.pointer = pointer; element.tag = Left; astack.push(element); pointer = pointer->leftchild(); } element = astack.top(); 48
49 Depth-First Traversal: Postorder without Recursion } } astack.pop(); pointer = element.pointer; if (element.tag == Left) { element.tag = Right; astack.push(element); pointer = pointer->rightchild(); } else { Visit(pointer->value()); pointer = NULL; } 49
50 Breadth-First Traversal The process of traversal is Visit the node at level 0 (the root) Visit the nodes from left to right at level 1 Similarly, after visiting the nodes at level i, visit the nodes from left to right at level (i+1) Example on the right: A B C D E F G H I A B C D E F G H I 50
51 Breadth-First Traversal template<class T> void BinaryTree<T>::LevelOrder(BinaryTreeNode<T> *root) { using std::queue; queue<binarytreenode<t>*> aqueue; BinaryTreeNode<T> *pointer = root; if (pointer) aqueue.push(pointer); while (!aqueue.empty()) { pointer = aqueue.front(); aqueue.pop(); Visit(pointer->value()); if (pointer->leftchild()!= NULL) aqueue.push(pointer->leftchild()); if (pointer->rightchild()!= NULL) aqueue.push(pointer->rightchild()); } } 51
52 Recommended Readings Weiss, DS & Algo. Analysis in C++ (3 rd ed.) Section 4.1 Preliminaries Section 4.2 Binary Trees Section 4.6 Tree Traversals (Revisited) 52
53 课程和教材参考信息 国家级精品课程 数据结构与算法 张铭 赵海燕 王腾蛟 宋国杰 高军 十一五 国家级规划教材 : 张铭, 王腾蛟, 赵海燕, 数据结构与算法 高等教育出版社, 本章主笔 : 王腾蛟 版权所有, 转载或翻印必究
Binary Trees, Binary Search Trees
Binary Trees, Binary Search Trees 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, insert, delete)
More informationTree Data Structures CSC 221
Tree Data Structures CSC 221 Specialized Trees Binary Tree: A restriction of trees such that the maximum degree of a node is 2. Order of nodes is now relevant May have zero nodes (emtpy tree) Formal Definition:
More informationTree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.
Tree A tree consists of a set of nodes and a set of edges connecting pairs of nodes. A tree has the property that there is exactly one path (no more, no less) between any pair of nodes. A path is a connected
More informationTrees, Binary Trees, and Binary Search Trees
COMP171 Trees, Binary Trees, and Binary Search Trees 2 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,
More informationPESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication
USN 1 P E PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -0 Department of Electronics and Communication INTERNAL ASSESSMENT TEST 2 Date : 4//2017 Marks: 50 Subject & Code
More informationSection 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents
Section 5.5 Binary Tree A binary tree is a rooted tree in which each vertex has at most two children and each child is designated as being a left child or a right child. Thus, in a binary tree, each vertex
More informationData Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University
Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University U Kang (2016) 1 In This Lecture The concept of binary tree, its terms, and its operations Full binary tree theorem Idea
More informationLec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)
Lec 17 April 8 Topics: binary Trees expression trees Binary Search Trees (Chapter 5 of text) Trees Linear access time of linked lists is prohibitive Heap can t support search in O(log N) time. (takes O(N)
More informationLecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson
Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Reading: Carrano, Chapter 15 Introduction to trees The data structures we have seen so far to implement
More informationCE 221 Data Structures and Algorithms
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, 4.1 4.2 Izmir University of Economics 1 Preliminaries - I (Recursive) Definition: A tree is a collection of nodes. The
More informationTrees. (Trees) Data Structures and Programming Spring / 28
Trees (Trees) Data Structures and Programming Spring 2018 1 / 28 Trees A tree is a collection of nodes, which can be empty (recursive definition) If not empty, a tree consists of a distinguished node r
More informationTrees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology
Trees : Part Section. () (2) Preorder, Postorder and Levelorder Traversals Definition: A tree is a connected graph with no cycles Consequences: Between any two vertices, there is exactly one unique path
More informationIntroduction to Computers and Programming. Concept Question
Introduction to Computers and Programming Prof. I. K. Lundqvist Lecture 7 April 2 2004 Concept Question G1(V1,E1) A graph G(V, where E) is V1 a finite = {}, nonempty E1 = {} set of G2(V2,E2) vertices and
More informationMULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR
STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM
More informationMarch 20/2003 Jayakanth Srinivasan,
Definition : A simple graph G = (V, E) consists of V, a nonempty set of vertices, and E, a set of unordered pairs of distinct elements of V called edges. Definition : In a multigraph G = (V, E) two or
More informationPrinciples of Computer Science
Principles of Computer Science Binary Trees 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 1 Today s Topics Extending LinkedList with Fast Search Sorted Binary Trees Tree Concepts Traversals of a Binary
More informationbinary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees called the left and right subtrees which are disjoint
More informationTopic 14. The BinaryTree ADT
Topic 14 The BinaryTree ADT 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
More informationCSI33 Data Structures
Outline Department of Mathematics and Computer Science Bronx Community College November 13, 2017 Outline Outline 1 C++ Supplement.1: Trees Outline C++ Supplement.1: Trees 1 C++ Supplement.1: Trees Uses
More informationBinary Trees Fall 2018 Margaret Reid-Miller
Binary Trees 15-121 Fall 2018 Margaret Reid-Miller Trees Fall 2018 15-121 (Reid-Miller) 2 Binary Trees A binary tree is either empty or it contains a root node and left- and right-subtrees that are also
More informationData Structures and Algorithms(6)
Ming Zhang "ata Structures and Algorithms" ata Structures and Algorithms(6) Instructor: Ming Zhang Textbook Authors: Ming Zhang, Tengjiao Wang and Haiyan Zhao Higher ducation Press, 2008.6 (the "leventh
More informationBinary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management
Binary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from
More informationBinary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals
Binary Trees 1 Binary Tree Node Relationships 2 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the
More informationBST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams
Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr mahmoud.sakr@cis.asu.edu.eg Cairo, Egypt, October 2012 Binary Search Trees (BST) 1. Hierarchical data structure with a single reference to root
More informationTREES. Trees - Introduction
TREES Chapter 6 Trees - Introduction All previous data organizations we've studied are linear each element can have only one predecessor and successor Accessing all elements in a linear sequence is O(n)
More informationtree nonlinear Examples
The Tree ADT 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 10-2
More informationOutline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications
Trees 1 Outline Preliminaries What is Tree? Implementation of Trees using C++ Tree traversals and applications Binary Trees Binary Search Trees Structure and operations Analysis 2 What is a Tree? A tree
More informationBinary Trees
Binary Trees 4-7-2005 Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? What is a Tree? You are all familiar with what
More informationAlgorithms and Data Structures (INF1) Lecture 8/15 Hua Lu
Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu Department of Computer Science Aalborg University Fall 2007 This Lecture Trees Basics Rooted trees Binary trees Binary tree ADT Tree traversal
More informationChapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.
Chapter 4 Trees 4-1 Trees and Spanning Trees Trees, T: A simple, cycle-free, loop-free graph satisfies: If v and w are vertices in T, there is a unique simple path from v to w. Eg. Trees. Spanning trees:
More informationAlgorithms and Data Structures
Lesson 3: trees and visits Luciano Bononi http://www.cs.unibo.it/~bononi/ (slide credits: these slides are a revised version of slides created by Dr. Gabriele D Angelo) International
More informationChapter 20: Binary Trees
Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other
More informationTrees! Ellen Walker! CPSC 201 Data Structures! Hiram College!
Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College! ADTʼs Weʼve Studied! Position-oriented ADT! List! Stack! Queue! Value-oriented ADT! Sorted list! All of these are linear! One previous item;
More informationData Structure - Binary Tree 1 -
Data Structure - Binary Tree 1 - Hanyang University Jong-Il Park Basic Tree Concepts Logical structures Chap. 2~4 Chap. 5 Chap. 6 Linear list Tree Graph Linear structures Non-linear structures Linear Lists
More information7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w
Chapter 7 Trees 7.1 Introduction A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w Tree Terminology Parent Ancestor Child Descendant Siblings
More information13 BINARY TREES DATA STRUCTURES AND ALGORITHMS INORDER, PREORDER, POSTORDER TRAVERSALS
DATA STRUCTURES AND ALGORITHMS 13 BINARY TREES INORDER, PREORDER, POSTORDER TRAVERSALS IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD WWW.IMRANIHSAN.COM LECTURES ADAPTED FROM: DANIEL KANE,
More informationTree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010
SE-286: Data Structures t and Programming Tree Virendra Singh Indian Institute of Science Bangalore Lecture 11 Courtesy: Prof. Sartaj Sahni 1 Trees Nature Lover sviewofatree leaves branches root 3 Computer
More informationLECTURE 13 BINARY TREES
DATA STRUCTURES AND ALGORITHMS LECTURE 13 BINARY TREES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD DEFINITION The arbitrary number of children in general trees is often unnecessary many real-life
More informationUpcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:
Upcoming ACM Events Linux Crash Course Date: Oct. 2nd and 3rd Time: 1:15 pm - 3:15 pm Location: UW1-210 (10/02) and UW1-221 (10/03) Weekly Crack the Coding Interview Date: Weekly Fridays from Oct. 5th
More informationCISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees Outline General Trees Terminology, Representation, Properties Binary Trees Representations, Properties, Traversals Recursive Algorithms
More informationBinary Trees. Height 1
Binary Trees Definitions A tree is a finite set of one or more nodes that shows parent-child relationship such that There is a special node called root Remaining nodes are portioned into subsets T1,T2,T3.
More informationTrees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1
Trees Introduction & Terminology Cinda Heeren / Geoffrey Tien 1 Review: linked lists Linked lists are constructed out of nodes, consisting of a data element a pointer to another node Lists are constructed
More informationTree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.
Trees, Binary Search Trees, and Heaps CS 5301 Fall 2013 Jill Seaman Tree: non-recursive definition Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every node (except
More informationTrees. CSE 373 Data Structures
Trees CSE 373 Data Structures Readings Reading Chapter 7 Trees 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical relationships File directories
More informationTrees. Truong Tuan Anh CSE-HCMUT
Trees Truong Tuan Anh CSE-HCMUT Outline Basic concepts Trees Trees A tree consists of a finite set of elements, called nodes, and a finite set of directed lines, called branches, that connect the nodes
More informationLecture 37 Section 9.4. Wed, Apr 22, 2009
Preorder Inorder Postorder Lecture 37 Section 9.4 Hampden-Sydney College Wed, Apr 22, 2009 Outline Preorder Inorder Postorder 1 2 3 Preorder Inorder Postorder 4 Preorder Inorder Postorder Definition (Traverse)
More informationTopic Binary Trees (Non-Linear Data Structures)
Topic Binary Trees (Non-Linear Data Structures) CIS210 1 Linear Data Structures Arrays Linked lists Skip lists Self-organizing lists CIS210 2 Non-Linear Data Structures Hierarchical representation? Trees
More informationBBM 201 Data structures
BBM 201 Data structures Lecture 11: Trees 2018-2019 Fall Content Terminology The Binary Tree The Binary Search Tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013
More informationTodays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.
Trees! Todays Lecture Assignment 2 deadline: 11 pm Monday 2/17 President s day. You have 5 Calendar days to complete. Trees What are trees in computer science? Data Structures for Trees Algorithms useful
More informationTrees. Trees. CSE 2011 Winter 2007
Trees CSE 2011 Winter 2007 2/5/2007 10:00 PM 1 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,
More informationTrees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology
Chapter 11 Trees Definition of a 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
More informationCS302 - Data Structures using C++
CS302 - Data Structures using C++ Topic: Trees Kostas Alexis Trees List, stacks, and queues are linear in their organization of data. Items are one after another In this section, we organize data in a
More informationChapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1
Chapter 11!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 2015-12-01 09:30:53 1/54 Chapter-11.pdf (#13) Terminology Definition of a general tree! A general tree T is a set of one or
More informationChapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1
Chapter 11!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 2015-03-25 21:47:41 1/53 Chapter-11.pdf (#4) Terminology Definition of a general tree! A general tree T is a set of one or more
More informationSection Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees
Chapter 11 Copyright McGraw-Hill Education. All rights reserved. No reproduction or distribution without the prior written consent of McGraw-Hill Education. Chapter Summary Introduction to Trees Applications
More information3137 Data Structures and Algorithms in C++
3137 Data Structures and Algorithms in C++ Lecture 3 July 12 2006 Shlomo Hershkop 1 Announcements Homework 2 out tonight Please make sure you complete hw1 asap if you have issues, please contact me will
More informationTree Structures. A hierarchical data structure whose point of entry is the root node
Binary Trees 1 Tree Structures A tree is A hierarchical data structure whose point of entry is the root node This structure can be partitioned into disjoint subsets These subsets are themselves trees and
More informationBinary Trees and Binary Search Trees
Binary Trees and Binary Search Trees Learning Goals After this unit, you should be able to... Determine if a given tree is an instance of a particular type (e.g. binary, and later heap, etc.) Describe
More informationThe tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo
The tree data structure 1 Trees COL 106 Amit Kumar Shweta Agrawal Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo 1 Trees The tree data structure 3 A rooted tree data structure stores
More information6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node
6-TREE Data Structure Management (330701) Tree: A tree is defined as a finite set of one or more nodes such that There is a special node called the root node R. The remaining nodes are divided into n 0
More informationData Structures and Algorithms for Engineers
04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University
More informationWhy Do We Need Trees?
CSE 373 Lecture 6: Trees Today s agenda: Trees: Definition and terminology Traversing trees Binary search trees Inserting into and deleting from trees Covered in Chapter 4 of the text Why Do We Need Trees?
More informationComputer Science 210 Data Structures Siena College Fall Topic Notes: Trees
Computer Science 0 Data Structures Siena College Fall 08 Topic Notes: Trees We ve spent a lot of time looking at a variety of structures where there is a natural linear ordering of the elements in arrays,
More informationH.O.#12 Fall 2015 Gary Chan. Binary Tree (N:12)
H.O.#12 Fall 2015 Gary Chan Binary Tree (N:12) Outline Binary tree terminology Tree traversals: preorder, inorder and postorder Dictionary and binary search tree Binary search tree operations Search min
More information4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees
4. Trees 4.1 Preliminaries 4.2 Binary trees 4.3 Binary search trees 4.4 AVL trees 4.5 Splay trees 4.6 B-trees Malek Mouhoub, CS340 Fall 2002 1 4.1 Preliminaries A Root B C D E F G Height=3 Leaves H I J
More informationTrees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.
Trees Q: Why study trees? : Many advance DTs are implemented using tree-based data structures. Recursive Definition of (Rooted) Tree: Let T be a set with n 0 elements. (i) If n = 0, T is an empty tree,
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 11: Binary Search Trees MOUNA KACEM mouna@cs.wisc.edu Fall 2018 General Overview of Data Structures 2 Introduction to trees 3 Tree: Important non-linear data structure
More informationCSI33 Data Structures
Outline Department of Mathematics and Computer Science Bronx Community College October 19, 2016 Outline Outline 1 Chapter 7: Trees Outline Chapter 7: Trees 1 Chapter 7: Trees Uses Of Trees Chapter 7: Trees
More informationData Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD
Data Structures Trees By Dr. Mohammad Ali H. Eljinini Trees Are collections of items arranged in a tree like data structure (none linear). Items are stored inside units called nodes. However: We can use
More informationLECTURE 11 TREE TRAVERSALS
DATA STRUCTURES AND ALGORITHMS LECTURE 11 TREE TRAVERSALS IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD BACKGROUND All the objects stored in an array or linked list can be accessed sequentially
More informationName CPTR246 Spring '17 (100 total points) Exam 3
Name CPTR246 Spring '17 (100 total points) Exam 3 1. Linked Lists Consider the following linked list of integers (sorted from lowest to highest) and the changes described. Make the necessary changes in
More informationBinary Trees. Examples:
Binary Trees A tree is a data structure that is made of nodes and pointers, much like a linked list. The difference between them lies in how they are organized: In a linked list each node is connected
More informationINF2220: algorithms and data structures Series 1
Universitetet i Oslo Institutt for Informatikk A. Maus, R.K. Runde, I. Yu INF2220: algorithms and data structures Series 1 Topic Trees & estimation of running time (Exercises with hints for solution) Issued:
More informationTrees and Binary Trees
Trees and Binary Trees Become Rich Force Others to be Poor Rob Banks Stock Fraud The class notes are a compilation and edition from many sources. The instructor does not claim intellectual property or
More informationBinary Tree Implementation
Binary Tree Implementation Lecture 32 Section 19.1 Robb T. Koether Hampden-Sydney College Mon, Apr 16, 2018 Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 16, 2018 1 / 24
More informationUnit 1 (9Hrs) Trees. Mahesh Sanghavi & Deepali Pawar Department of Computer Engineering, SNJB s KBJ College of Engineering, Chandwad, India
Unit 1 (9Hrs) Trees Mahesh Sanghavi & Deepali Pawar Department of Computer Engineering, SNJB s KBJ College of Engineering, Chandwad, India The class notes are a compilation and edition from many sources.
More informationDATA STRUCTURES USING C
DATA STRUCTURES USING C The British Constitution Crown Church of England Cabinet House of Commons House of Lords Supreme Court Ministers County Metropolitan Council police Rural District County Borough
More informationTree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest
Tree Structures Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest o A tree is a connected digraph with these properties: There is exactly one node (Root)
More informationLecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?
Lecture 32 No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions? Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 1 Outline Introduction
More information1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking
Review for Test 2 (Chapter 6-10) Chapter 6: Template functions & classes 1) What is the primary purpose of template functions? A. To allow a single function to be used with varying types of arguments B.
More informationChapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Trees Chapter 11 Chapter Summary Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees Introduction to Trees Section 11.1 Section Summary Introduction to Trees
More informationStacks, Queues and Hierarchical Collections
Programming III Stacks, Queues and Hierarchical Collections 2501ICT Nathan Contents Linked Data Structures Revisited Stacks Queues Trees Binary Trees Generic Trees Implementations 2 Copyright 2002- by
More informationA set of nodes (or vertices) with a single starting point
Binary Search Trees Understand tree terminology Understand and implement tree traversals Define the binary search tree property Implement binary search trees Implement the TreeSort algorithm 2 A set of
More informationCS 206 Introduction to Computer Science II
CS 206 Introduction to Computer Science II 10 / 10 / 2016 Instructor: Michael Eckmann Today s Topics Questions? Comments? A few comments about Doubly Linked Lists w/ dummy head/tail Trees Binary trees
More informationChapter 5. Binary Trees
Chapter 5 Binary Trees Definitions and Properties A binary tree is made up of a finite set of elements called nodes It consists of a root and two subtrees There is an edge from the root to its children
More informationMID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.
MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the elements may locate at far positions
More information1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.
Unit 6: Binary Trees Part 1 Engineering 4892: Data Structures Faculty of Engineering & Applied Science Memorial University of Newfoundland July 11, 2011 1 Binary trees 1 Binary search trees Analysis of
More informationBioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)
Bioinformatics Programming EE, NCKU Tien-Hao Chang (Darby Chang) 1 Tree 2 A Tree Structure A tree structure means that the data are organized so that items of information are related by branches 3 Definition
More informationCS 151. Binary Trees. Friday, October 5, 12
CS 151 Binary Trees 1 Binary Tree Examples Without telling you what a binary tree is, here are some examples (that I will draw on the board): The dots/circles are called nodes, or vertices (singular: one
More informationGraphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}
Graphs and Trees 1 Graphs (simple) graph G = (V, ) consists of V, a nonempty set of vertices and, a set of unordered pairs of distinct vertices called edges. xamples V={,,,,} ={ (,),(,),(,), (,),(,),(,)}
More informationRevision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,
[1] Big-O Analysis AVERAGE(n) 1. sum 0 2. i 0. while i < n 4. number input_number(). sum sum + number 6. i i + 1 7. mean sum / n 8. return mean Revision Statement no. of times executed 1 1 2 1 n+1 4 n
More informationCourse Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU
Course Review for Midterm Exam 1 Cpt S 223 Fall 2011 1 Midterm Exam 1 When: Friday (10/14) 1:10-2pm Where: in class Closed book, closed notes Comprehensive Material for preparation: Lecture slides & in-class
More informationFirst Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...
First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms.... Q1) What are some of the applications for the tree data structure? Q2) There are 8, 15, 13, and
More informationStacks, Queues and Hierarchical Collections. 2501ICT Logan
Stacks, Queues and Hierarchical Collections 2501ICT Logan Contents Linked Data Structures Revisited Stacks Queues Trees Binary Trees Generic Trees Implementations 2 Queues and Stacks Queues and Stacks
More informationBinary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I
Binary Trees College of Computing & Information Technology King Abdulaziz University CPCS-204 Data Structures I Outline Tree Stuff Trees Binary Trees Implementation of a Binary Tree Tree Traversals Depth
More informationCS302 - Data Structures using C++
CS302 - Data Structures using C++ Topic: Tree Implementations Kostas Alexis Nodes in a Binary Tree Representing tree nodes Must contain both data and pointers to node s children Each node will be an object
More information[ DATA STRUCTURES ] Fig. (1) : A Tree
[ DATA STRUCTURES ] Chapter - 07 : Trees A Tree is a non-linear data structure in which items are arranged in a sorted sequence. It is used to represent hierarchical relationship existing amongst several
More informationTrees: examples (Family trees)
Ch 4: Trees it s a jungle out there... I think that I will never see a linked list useful as a tree; Linked lists are used by everybody, but it takes real smarts to do a tree Trees: examples (Family trees)
More informationData Structures Question Bank Multiple Choice
Section 1. Fundamentals: Complexity, Algorthm Analysis 1. An algorithm solves A single problem or function Multiple problems or functions Has a single programming language implementation 2. A solution
More informationUNIT IV -NON-LINEAR DATA STRUCTURES 4.1 Trees TREE: A tree is a finite set of one or more nodes such that there is a specially designated node called the Root, and zero or more non empty sub trees T1,
More information