Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.

Slides:



Advertisements
Similar presentations
Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.
Advertisements

Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
Advanced Data Structures
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Trees, Binary Trees, and Binary Search Trees COMP171.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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.
Chapter 4: Trees AVL Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Chapter 4: Trees Binary Search Trees
Marc Smith and Jim Ten Eyck
Binary Trees Chapter 6.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-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.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
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.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
Tree Data Structures.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary 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,
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Trees 2: Section 4.2 and 4.3 Binary trees. Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Discrete Mathematics Chapter 5 Trees.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
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.
Binary Search Trees (BST)
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.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
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.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
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.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
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.
Trees Chapter 15.
Binary Search Tree (BST)
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
Tree.
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
General Tree Concepts Binary Trees
Binary Trees, Binary Search Trees
Presentation transcript:

Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java

Trees  Definitions  Representation  Binary trees  Traversals  Expression trees 2

Definitions 3 tree - a non-empty collection of vertices & edges vertex (node) - can have a name and carry other associated information path - list of distinct vertices in which successive vertices are connected by edges  any two vertices must have one and only one path between them else its not a tree  a tree with N nodes has N-1 edges

Definitions root - starting point (top) of the tree parent (ancestor) - the vertex “above” this vertex child (descendent) - the vertices “below” this vertex 4

Definitions leaves (terminal nodes) - have no children level - the number of edges between this node and the root ordered tree - where children’s order is significant 5

Definitions Depth of a node - the length of the path from the root to that node root: depth 0 Height of a node - the length of the longest path from that node to a leaf any leaf: height 0 Height of a tree: The length of the longest path from the root to a leaf 6

Balanced Trees  the difference between the height of the left sub-tree and the height of the right sub-tree is not more than 1. 7

Trees - Example 8 E R T ELPM EA S A root Leaves or terminal nodes Child (of root) Depth of T: 2 Height of T: 1 Level

Tree Representation 9 Class TreeNode { Object element; TreeNode firstChild; TreeNode nextSibling; }

Example 10 a bf e c d g a b e c d f g

Binary Tree 11 S A B N O N P D M I S Internal node External node

Height of a Complete Binary Tree 12 L 0 L 1 L 2 L 3 At each level the number of the nodes is doubled. total number of nodes: = = 15

Nodes and Levels in a Complete Binary Tree 13 Number of the nodes in a tree with M levels: …. 2 M = 2 (M+1) - 1 = 2*2 M - 1 Let N be the number of the nodes. N = 2*2 M - 1, 2*2 M = N M = (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(2 M ) nodes

Binary Tree Node 14 Class BinaryNode { Object Element; // the data in the node BinaryNode left; // Left child BinaryNode right; // Right child }

Binary Tree – Preorder Traversal 15 C L R E T D O N U M P A Root Left Right First letter - at the root Last letter – at the rightmost node

Preorder Algorithm 16 preorderVisit(tree) { if (current != null) { process (current); preorderVisit (left_tree); preorderVisit (right_tree); }

Binary Tree – Inorder Traversal 17 U A E R T N P D M O C L Left Root Right First letter - at the leftmost node Last letter – at the rightmost node

Inorder Algorithm 18 inorderVisit(tree) { if (current != null) { inorderVisit (left_tree); process (current); inorderVisit (right_tree); }

Binary Tree – Postorder Traversal 19 D L U A N E P R O M C T Left Right Root First letter - at the leftmost node Last letter – at the root

Postorder Algorithm 20 postorderVisit(tree) { if (current != null) { postorderVisit (left_tree); postorderVisit (right_tree); process (current); }

Expression Trees The stack contains references to tree nodes (bottom is to the left) * (1+2)*3 Post-fix notation: *

Expression Trees 22 In-order traversal: (1 + 2) * ( 3) Post-order traversal: * *

Binary Search Trees  Definitions  Operations and complexity  Advantages and disadvantages  AVL Trees  Single rotation  Double rotation  Splay Trees  Multi-Way Search 23

Definitions 24 Each node – a record with a key and a value a left link a right link All records with smaller keys – left subtree All records with larger keys – right subtree

Example 25

Operations Search - compare the values and proceed either to the left or to the right Insertion - unsuccessful search - insert the new node at the bottom where the search has stopped Deletion - replace the value in the node with the smallest value in the right subtree or the largest value in the left subtree. Retrieval in sorted order – inorder traversal 26

Complexity 27  Logarithmic, depends on the shape of the tree  In the worst case – O(N) comparisons

Advantages of BST Simple Efficient Dynamic One of the most fundamental algorithms in CS The method of choice in manyapplications 28

Disadvantages of BST The shape of the tree depends on the order of insertions, and it can be degenerated. When inserting or searching for an element, the key of each visited node has to be compared with the key of the element to be inserted/found. Keys may be long and the run time may increase much. 29

Improvements of BST Keeping the tree balanced: AVL trees (Adelson - Velskii and Landis) Balance condition: left and right subtrees of each node can differ by at most one level. It can be proved that if this condition is observed the depth of the tree is O(logN). Reducing the time for key comparison: Radix trees - comparing only the leading bits of the keys (not discussed here) 30