1 8/16/2015 MATH 224 – Discrete Mathematics A rooted tree is a tree that has a distinguished node called the root. Just as with all trees a rooted tree.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Binary Search Trees CSE 331 Section 2 James Daly.
Computer Science C++ High School Level By Guillermo Moreno.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
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,
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Lecture 21 Trees. “ A useful data structure for many applications”
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
Binary Trees. Node structure Data A data field and two pointers, left and right.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
Binary Search Trees CSE 331 Section 2 James Daly.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
Types of Binary Trees Introduction. Types of Binary Trees There are several types of binary trees possible each with its own properties. Few important.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
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.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Starting at Binary Trees
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
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.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
Trees Namiq Sultan. Trees Trees are very flexible, versatile and powerful non-liner data structure that can be used to represent data items possessing.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
Data Structure and Algorithms
Binary Trees and Binary Search Trees
Recursive Objects (Part 4)
UNIT III TREES.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Trees.
Data Structures & Algorithm Design
ITEC 2620M Introduction to Data Structures
Data Structures Using C++ 2E
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Find in a linked list? first last 7  4  3  8 NULL
Trees.
Trees Definitions Implementation Traversals K-ary Trees
CSC 143 Java Trees.
Trees.
Chapter 20: Binary Trees.
Binary and AVL Trees in C Hao Ma
Trees.
Data Structures Using C++ 2E
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

1 8/16/2015 MATH 224 – Discrete Mathematics A rooted tree is a tree that has a distinguished node called the root. Just as with all trees a rooted tree is acyclic (no cycles) and connected. Rooted trees have many applications in computer science, for example the binary search tree. Rooted Trees The root is typically drawn at the top. Children of the root. Grandchildren of the root and leaf nodes.

2 8/16/2015 MATH 224 – Discrete Mathematics A Binary tree is a rooted tree where no node has more than two children. As shown in the previous slide all nodes, except for leaf nodes, have children and some have grandchildren. All nodes except the root have a parent and some have a grandparent and ancestors. A node has at most one parent and at most one grandparent. Rooted Binary Trees The root of a binary tree at level 4 also height 4 and depth 0. Nodes with only 1 child at level 2 and depth 2. Nodes a level 1, and depth 3. Nodes at level 3 and depth 1. Nodes with no children are called leaf nodes. Nodes a level 0, and depth 4.

3 8/16/2015 MATH 224 – Discrete Mathematics A Balanced tree is a rooted tree where the leaf nodes have depths that vary by no more than one. In other words, the depth of a leaf node is either equal to the height of the tree or one less than the height. All of the trees below are balanced. Balanced Trees What are the heights of each of these trees?

4 8/16/2015 MATH 224 – Discrete Mathematics A Binary Search Tree

5 8/16/2015 MATH 224 – Discrete Mathematics Some Tree Applications Binary Search Trees to store items for easy retrieval, insertion and deletion. For balanced trees, each of these steps takes lg(N) time for an N node tree. Variations of the binary search tree include, the AVL tree, Red-Black tree and the B-tree. These are all designed to keep the tree nearly balanced. The first two are binary trees while the B-tree has more than two children for each internal node. Game trees are used extensively in AI. The text has a simple example of a tic-tac- toe tree on Page 654. Huffman trees are used to compress data. They are most commonly used to compress faxes before transmission. Spanning trees are subgraphs that have applications to computer and telephone networks. Minimum spanning trees are of special interest. Steiner trees are a generalization of the spanning tree with are useful in multicast communication.

6 8/16/2015 MATH 224 – Discrete Mathematics Binary Tree Traversal Functions void inorder(node T) if (T ≠ null) { inorder(T.left) print(T.data) ; print(“, ”) inorder(T.right) } fi end inorder a e c d b f g h i j output will be something like d, b, e, a, f, c, g, i, h, j

7 8/16/2015 MATH 224 – Discrete Mathematics Binary Tree Traversal Functions void inorder(node T) if (T ≠ null) { inorder(T.left) print(T.data) ; print(“, ”) inorder(T.right) } fi end inorder a e c d b f g h i j T is NULL T is d prints d T is b T is a main calls inorder

8 8/16/2015 MATH 224 – Discrete Mathematics Binary Tree Traversal Functions void pre_order(node T) if (T ≠ null) { cout data << “, ” pre_order(T−>left) pre_order(T −>right) } end pre_order a e c d b f g h i j output will be something like a, b, d, e, c, f, g, h, i, j

9 8/16/2015 MATH 224 – Discrete Mathematics Binary Tree Traversal Functions void post_order(node T) if (T ≠ null) { post_order(T−>left) post_order(T − >right) cout data << “, ” } end post_order a e c d b f g h i j output will be something like d, e, b, f, i, j, h, g, c, a

10 8/16/2015 MATH 224 – Discrete Mathematics Counting the nodes in a Binary Tree int count(node T) int num = 0 if (T ≠ null) num = 1 + count(T−>left) + count(T− >right) fi return num end count a e c d b f g h i j

11 8/16/2015 MATH 224 – Discrete Mathematics The Height of a Binary Tree int height(node T) int ht = −1 if (T ≠ null) ht = 1 + max(height(T−>left), height(T−>right)) fi return ht end height a e c d b f g h i j Called initially at a Then at b Then a d Then at NULL Returns −1 to d from left and right Returns 0 to b from d and from e Returns 1 to a from left. Returns 3 to a from right Returns 4 from a to original caller

12 8/16/2015 MATH 224 – Discrete Mathematics Inserting into a Binary Search Tree // Note that T is passed by reference insert(node & T, int X) if (T = = null) T = node(X) else if X value insert(T −>left, X) else insert(T −>right, X) fi end height Where would 60 be inserted? If the original value for T is the node containing 30, where is the first recursive call, the second etc? Where would −10 be inserted?

13 8/16/2015 MATH 224 – Discrete Mathematics What Does this Code do? Boolean T_alg(node T, int X) if (T = = null) return FALSE else { print T.value if X = =T.value return TRUE else if T.value > X return T_alg(T.left, X) else return T_alg(T.right X) } //fi } //end T_alg

14 8/16/2015 MATH 224 – Discrete Mathematics Counting the Nodes with even Integers in a Binary Tree int even_count(node T) int num = 0 if (T ≠ null) num = (1 - T.data%2) + even_count(T−>left) + even_count(T− >right) fi return num end even_count

15 8/16/2015 MATH 224 – Discrete Mathematics What does cnt return when given a Binary Tree int cnt(node T) int num = 0 if (T ≠ null) { num = cnt(T−>left) + cnt(T− >right) if (num = = 0) num = 1 fi }fi return num end even_count