Trees. What is a tree? You know…  tall  green  leafy  possibly fruit  branches  roots  I’m sure you’ve seen them.

Slides:



Advertisements
Similar presentations
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms Post-order Traversal: Left Child - Right Child - Root Depth-First Search.
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Computer Science C++ High School Level By Guillermo Moreno.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 171: Introduction to Computer Science II
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 8 – 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.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Lecture 17 Non-Linear data structures Richard Gesick.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Trees CS212 & CS-240 D.J. Foreman. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called.
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.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree Data Structures.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Rudiments of Trees a Joshua presentation DATA STRUCTURES.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
Trees Namiq Sultan. Trees Trees are very flexible, versatile and powerful non-liner data structure that can be used to represent data items possessing.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Trees. Trees: – A trunk from the roots – Divides into branches – Ends in leaves.
Chapter 12 – Data Structures
Recursive Objects (Part 4)
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary search tree. Removing a node
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.
Trees.
Tree.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Data Structures & Algorithm Design
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
Chapter 21: Binary Trees.
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
slides created by Alyssa Harding
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Trees.
Trees Definitions Implementation Traversals K-ary Trees
Lecture 36 Section 12.2 Mon, Apr 23, 2007
2018, Fall Pusan National University Ki-Joune Li
Representing binary trees with lists
Binary Trees, Binary Search Trees
CSE 1002 Fundamentals of Software Development 2 More Linked Structures
ECE 103 Engineering Programming Chapter 64 Tree Implementation
Tree.
Chapter 20: Binary Trees.
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:

Trees

What is a tree? You know…  tall  green  leafy  possibly fruit  branches  roots  I’m sure you’ve seen them

In the context of C (and CS in general) Trees are a Data Structure (Like a linked list) They  Are upside-down  Have one root  Have branches  Have leaves  (No fruit)

A Tree (In general)

Nodes

Edges (Links (Pointers))

The Root

Leaves

Branches (A.K.A. Sub-Trees)

Parent / Children

Some info The root has no parent The leaves have no children The interior nodes have at least one child, and a parent

Types of trees Binary trees  Every node has (at most) two childen Trinary trees  Every node has (at most) three childen N-ary trees  Every node has as many children as it wants/needs

We care about binary trees

Binary search trees

Binary Trees typedef struct S_tree { int value; // For example struct S_tree *left, *right; } tree; Just like a linked list, but instead of one pointer, it has two! (left and right)

In Order Traversal Printing an entire binary search tree. void print_tree(tree *t) { if (t == NULL) return; print_tree(t->left); printf(“%d\n”,t->val); print_tree(t->right); } Recursion!

Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree

Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree

Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree 7

Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree 7 18

Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree

Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree

Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree

Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree

Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree

Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree

Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree

Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree

Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree