Depict the Tree Structure in a picture

Slides:



Advertisements
Similar presentations
Computer Science C++ High School Level By Guillermo Moreno.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
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.
Trees CS /02/05 L7: Trees Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition.
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.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
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.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Building Java Programs Binary Trees reading: 17.1 – 17.3.
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.
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.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Lecture 9 Binary Trees Trees General Definition Terminology
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
1 CMSC 341 Introduction to Trees Textbook sections:
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Binary Trees.
CSE 373 Data Structures Lecture 7
CC 215 Data Structures Trees
Binary Trees.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Recursive Definition of Tree Structures
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
CSE 373 Data Structures Lecture 7
Binary Trees, Binary Search Trees
Binary Search Trees Definition (recursive):
Chapter 20: Binary Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Chapter 21: Binary Trees.
Binary Trees.
slides created by Alyssa Harding
Trees.
General Trees & Binary Trees
Find in a linked list? first last 7  4  3  8 NULL
Trees CSE 373 Data Structures.
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Trees.
Binary Trees.
Trees Definitions Implementation Traversals K-ary Trees
Binary Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Announcements Prelim 1 on Tuesday! A4 will be posted today
CSC 143 Java Trees.
Tree.
Chapter 20: Binary Trees.
Tree.
Binary Trees.
Trees.
Trees CSE 373 Data Structures.
Tree and its terminologies
Trees CSE 373 Data Structures.
Data Structures Using C++ 2E
Presentation transcript:

Depict the Tree Structure in a picture height of d10 = 1 Terminologies The root d11 the links are directed height of d9 = 4 1 Ancestors d9 d10 1 2 leaf Descendants Parent d8 2 depth of d8=2 Children d4 d3 d5 d6 d7 3 leaf leaf leaf leaf d1 d2 leaf Siblings 4 leaf 11/8/2018 ITK 168

More Terminologies In-degree: (of a node) the number of links pointing to the node (always 1 or 0); the root is the only node with in-degree 0. 2. Out-degree: (of a node) the number of links 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 23 d4 27 d8 d3 d5 d6 d7 d4 d1 d2 Arity = 5 11/8/2018 ITK 168

Recursive Definition of Tree Structures (not so precise) 11/8/2018 No rule is violated but we don’t like it An empty collection is a tree A single node is a tree A node being linked to a finite number of trees forms a tree. Is this definition good enough ? 23 d4 27 d4 d8 d1 d2 d3 d5 d6 d7 11/8/2018 ITK 168

Recursive Definition of Tree Structures Empty is a tree; the root is null A single node is a tree; the node is the root of the tree. A node points to a finite number of roots of some trees form a tree; the node is the root of the tree. (so, the node can’t point to a node that is not the root.) 23 d4 27 d4 d8 d1 d2 d3 d5 d6 d7 11/8/2018 ITK 168

Tree Implementation Linked Lists data field 11/8/2018 Tree Implementation Linked Lists data data field enough fields for pointers pointing the children Usually, using the number of degree of the tree. Fixed, if the degree is small, e.g., binary tree (degree is 2). 11/8/2018 ITK 168

Tree Implementation using Array the degree is small 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 d7 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 = 4 degree = 2 size = 2height - 1 d1 2 1 d2 d3 5 d4 d5 d6 3 4 6 12 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 d7 What is the advantage and disadvantage of using array? 11/8/2018 ITK 168

A full-tree or complete tree is a perfect example for using array d1 d1 d2 d3 d2 d3 d7 d4 d5 d6 d7 d4 d5 d6 d8 d10 d8 d9 d10 d11 d12 d13 d14 d15 d9 complete tree full-tree 11/8/2018 ITK 168

A random binary tree: Randomly insert data into a binary tree 2 13 34 24 23 4 17 34 11/8/2018 ITK 168

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 <E extends Comparable<E>> { /********** 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 Node<E> *******/ private TNode<E> root; public BinaryTree() { root = null; ..... 11/8/2018 ITK 168

Add a new data to the BinaryTree //add data to this BST, return false if data is already in the tree public boolean add(E data) { if (root != null) return add(root, data); root = new TNode<E>(data, null, null); return true; } A private overloaded add method root is private; 11/8/2018 ITK 168

Randomly Insert (add) a data into a binary tree: toss a coin head private boolean add(TNode<E> t, E data){ if (Math.random() < 0.5) // toss a coin if (t.left == NULL) t.left = new TNode<E>(data,null,null); else add(t.left, data); else if (t.right == NULL) t.right = new TNode<E>(data,null,null); else add(t.right,data); return true; } 11/8/2018 ITK 168

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 11/8/2018 ITK 168

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 11/8/2018 ITK 168

Calculate the size of the tree, i.e., the number of nodes X Ln + 1 + Rn root = NULL; Ln Rn // 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); } 11/8/2018 ITK 168

Count how many k’s in the tree X root = NULL; if x = k, Ln + 1 + Rn ; otherwise, Ln + Rn Ln Rn // 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); } 11/8/2018 ITK 168

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 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); } d10 d9 2 3 d8 d4 d5 4 d1 d2 5 11/8/2018 ITK 168

Copy and Clone root root d8 d8 d3 d5 d3 d5 d1 d2 d1 d2 11/8/2018 ITK 168