Recursive Definition of Tree Structures

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

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Computer Science C++ High School Level By Guillermo Moreno.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Tree Terminology Linear versus hierarchical data Tree – connected graph with no cycles Child Parent Descendant Sibling Ancestor Leaf vs. internal.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
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.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
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.
Tree Data Structures.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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.
Starting at Binary Trees
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Data Structures & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
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.
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 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.
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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
2/11/ IT 179 Recursive Definition of Tree Structures 1.Empty is a tree; the root is null 2.A node points to a finite number of the roots of some.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
Trees. Family Tree Recursive Definition Tree - Definition A tree is a finite set of one or more nodes such that: 1.There is a specially designated node.
1 CMSC 341 Introduction to Trees Textbook sections:
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Binary Trees.
The Tree ADT.
CSCE 210 Data Structures and Algorithms
Binary Trees.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Recursive Objects (Part 4)
Week 6 - Wednesday CS221.
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
CMSC 341 Introduction to Trees.
Trees.
Data Structures & Algorithm Design
Binary Trees, Binary Search Trees
Binary Search Trees Definition (recursive):
Chapter 20: Binary Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Depict the Tree Structure in a picture
Chapter 21: Binary Trees.
Binary Trees.
slides created by Alyssa Harding
Trees.
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Trees.
Binary Trees.
Trees Lecture 9 CS2110 – Fall 2009.
Trees Definitions Implementation Traversals K-ary Trees
Binary Trees, Binary Search Trees
Binary Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
CSC 143 Java Trees.
Tree.
Chapter 20: Binary Trees.
Binary Trees.
Trees.
Tree and its terminologies
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Recursive Definition of Tree Structures Empty is a tree; the root is null A node pointing to a finite number of the roots of some other trees is the root of a tree. null null null null null null null null null null null null null null null 5/25/2018 IT 179

Terminology the links is directed, always pointing down The root 1 1 1 Ancestors a 1 Descendants height of b = 4 b c 1 1 height of c = 1 2 leaf depth of d =2 Parent of e,f,g,h,i 2 d Children of d Siblings e f g h i 3 leaf leaf leaf Siblings j k Siblings m n k 4 leaf leaf leaf leaf leaf 5/25/2018 IT 179

More Terminologies In-degree: the number of links pointing to a node (always 1 or 0); the root is the only node with in-degree 0. 2. Out-degree: the number of link pointing out from a node; leaves are node with out-degree 0 3 . Degree of a tree (arity): the maximum out-degree of nodes in the tree a b c d e f g h i j k 5/25/2018 IT 179

Tree Implementation Using Array Array is a good choice if: 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 10 11 12 13 14 d1 d2 d3 d4 d5 d6 Array is a good choice if: the degree is small the tree is rather full the size does not change too often height = 3 degree = 2 size = 2height - 1 d1 1 2 d2 d3 d7 What is the advantage and disadvantage of using array? d4 d5 d6 3 4 5 6 12 d7 1st child (left-child) of i is 2*i+1; 2nd child (right-child) of i is 2*i+2. The parent of i is (i-1)/2 height = 4 5/25/2018 IT 179

A perfect-tree or complete tree is a perfect example for using array d1 d1 d2 d3 d2 d3 d7 d4 d4 d5 d6 d5 d6 d7 d8 d8 d9 d10 d11 d12 d13 d14 d15 d9 d10 complete tree full-tree (no single child) perfect-tree 24-1 5/25/2018 IT 179

Tree Implementation: linked lists  arrays 5/25/2018 Tree Implementation: linked lists  arrays Links data data field enough fields for pointers pointing the children Usually, we use the degree of the tree as the number of the pointer fields. Fixed, if the degree is small, e.g., binary tree (degree is 2). 5/25/2018 IT 179

Binary Tree (2 degree tree) 5/25/2018 Linked Lists Binary Tree (2 degree tree) data Data field Right and Left Children 2 34 13 4 34 5/25/2018 IT 179

A Binary Tree Node in Java A data field Two pointers to the left-child and right-child A Binary Tree Node in Java public class BinaryTree <T> { /********** This is an inner class for tree nodes************/ private static class TNode<E> { private E data; private TNode<E> left,right; private TNode(E data, TNode<E> left, TNode<E> right) {//Construct a node with two children this.data = data; this.left = left; this.right = right; } /********** This is the end of the inner class TNode<E> *******/ private TNode<T> root; public BinaryTree() { root = null; ..... 5/25/2018 IT 179

Calculate the size of the tree, i.e., the number of nodes X nl + 1 + nr root = null; nl nr // Return the number nodes in the tree ..... public int size() { return size(root); } private int size(TNode<E> t) { if (t == null) return 0; return 1 + size(t.left) + size(t.right); } 5/25/2018 IT 179

Count how many k’s in the tree X root = NULL; if x = k, nl + 1 + nr ; otherwise, nl + nr nl nr // Return the number of k’s in the tree ..... public int count(E k) { return count(root, k); } private int count(TNode<E> t, E k) { if (t == null) return 0; return (k.compareTo(t.data) == 0 ? 1 : 0) + count(t.left, k) + count(t.right, k); } 5/25/2018 IT 179

Height of a Tree: The number of nodes in the longest path from the root. 1 // Return the heights of the tree ..... public int height() { return height(root); } Private int height(TNode<T> t) { if (t == null) return 0; int L = height(t.left); int R = height(t.right); return 1 + (L > R ? L : R); } b c 2 3 d e f 4 g h 5 5/25/2018 IT 179

A random binary tree: Randomly insert data into a binary tree 2 13 34 24 23 4 17 34 5/25/2018 IT 179

Add a new data to the BinaryTree //add data to this BST, return false if data is already in the tree public void add(E data) { root = add(root, data); } A private overloaded add method; 5/25/2018 IT 179

Randomly Insert (add) a data into a binary tree: toss a coin head data data private TNode<E> add(TNode<E> node, E data){ if (node == null) return new Node<E>(data,null,null); if (Math.random() < 0.5) // toss a coin node.left = add(node.left, data); else node.right = add(node.right, data); return node; } 5/25/2018 IT 179

How to remove data from a binary tree? 2 13 34 13 24 23 4 17 34 remove 13 What is your strategy? 5/25/2018 IT 179

Syntax Trees of Arithmetic Expressions + * - * 3 + 3 2 1 3 5 3*(2+1)+(3-3*5) 5/25/2018 IT 179

Syntax Trees for Arithmetic Expressions - + + * * - * 3 3 5 * 3 + 3 3 + 2 1 3 5 2 1 3*(2+1)+3-3*5 3*(2+1)+(3-3*5) 5/25/2018 IT 179

Tree traversal: preorder, inorder, postorder X L R X X L R L R inorder: L  X  R preorder: X  L  R postorder: L  R  X 5/25/2018 IT 179

System.out.print(t.data+", "); // Three ways of traversals ..... public void inorder() { inorder(root); } public void preorder() { preorder(root); } public void postorder() { postorder(root); } private void inorder(TNode<E> t) { if (t==null) return; inorder(t.left); System.out.print(t.data+", "); inorder(t.right); } // (1) Travel to the left-child // (2) The way we visit the node // (3) Travel to the right-child private void preorder(TNode<E> t) { if (t==null) return; System.out.print(t.data+", "); preorder(t.left); preorder(t.right); } // (1) The way we visit the node // (2) Travel to the left-child // (3) Travel to the right-child private void postorder(TNode<E> t) { if (t==null) return; postorder(t.left); postorder(t.right); System.out.print(t.data+", "); } // (1) Travel to the left-child // (2) Travel to the right-child // (3) The way we visit the node 5/25/2018 IT 179