Computer Science C++ High School Level By Guillermo Moreno.

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
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
Algorithms and data structures Protected by
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
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.
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.
Tree ADTs Tree concepts. Applications of Trees. A Tree ADT – requirements, contract. Linked implementation of Trees. Binary Tree ADTs. Binary Search.
Data Structures TREES.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
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.
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.
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.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
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.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
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.
Foundation of Computing Systems Lecture 4 Trees: Part I.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Recursive Definition of Tree Structures
Recursive Objects (Part 4)
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
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.
Trees.
Data Structures & Algorithm Design
Lecture 18. Basics and types of Trees
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
TREES General trees Binary trees Binary search trees AVL trees
Chapter 21: Binary Trees.
Binary Trees.
Find in a linked list? first last 7  4  3  8 NULL
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.
Binary Trees.
Trees Lecture 9 CS2110 – Fall 2009.
Trees Definitions Implementation Traversals K-ary Trees
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Binary Trees.
Trees.
Tree and its terminologies
Non-Linear data structures
Binary Trees, Binary Search Trees
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Computer Science C++ High School Level By Guillermo Moreno

Unit: Tree Data Structure Binary Tree. Binary Tree. A binary tree is made of nodes. A binary tree is made of nodes. Each node contains: Each node contains: 1. Left pointer 2. Right pointer 3. Data element

Binary Trees A binary tree is a finite set of nodes. A binary tree is a finite set of nodes. The set might be empty: The set might be empty: If no nodes, is an empty tree.

If the set is not empty: 1. There is one special node: the root 2. Each node is associated with two different nodes: A) left child B) right child

The parent node: If a node “C” is the child of another node “P”, If a node “C” is the child of another node “P”, Then, we say that “P” is the parent of “C” Then, we say that “P” is the parent of “C” Each node, except the root, has exactly one parent. Each node, except the root, has exactly one parent. The root has no parent. The root has no parent.

Definitions: Parent. The parent of the node is the node linked above it. Parent. The parent of the node is the node linked above it. Sibling. Two nodes are siblings if they have the same parent. Sibling. Two nodes are siblings if they have the same parent. Ancestor. A node’s parent is its first ancestor. Ancestor. A node’s parent is its first ancestor.

Descendant. A node’s children are its first descendants. Descendant. A node’s children are its first descendants. Depth. The depth of a tree is the maximum depth of any of its leaves. Depth. The depth of a tree is the maximum depth of any of its leaves. Full Tree. Every leaf has the same depth, and every non leaf has two children. Full Tree. Every leaf has the same depth, and every non leaf has two children. Leaf. A node with no children is called a leaf. Leaf. A node with no children is called a leaf.

Node. Each element of the set is called a node of the tree. Node. Each element of the set is called a node of the tree. Each node contains some data. Each node contains some data. The data could be an integer, double numbers, strings or characters. The data could be an integer, double numbers, strings or characters.

Code: In C or C++, the binary tree is built with a node type like this... In C or C++, the binary tree is built with a node type like this... struct node { int data; struct node* left; struct node* right; } struct node { int data; struct node* left; struct node* right; }

Breadth-First Traversal A breadth-first traversal of a tree starts at the root of the tree. It next visits the children, then the grand-children and so on. A breadth-first traversal of a tree starts at the root of the tree. It next visits the children, then the grand-children and so on. The numbers indicate the order in which the nodes are visited, not the contents of the nodes. The numbers indicate the order in which the nodes are visited, not the contents of the nodes.

Binary Search Trees Demonstration: Demonstration: There is a demonstration of Binary Search Trees [here]. There is a demonstration of Binary Search Trees [here].here Insert and delete data (strings) into a binary search tree (BST) Insert and delete data (strings) into a binary search tree (BST)

Insertion Given a binary search tree and a number, insert a new node. Given a binary search tree and a number, insert a new node. With the given number into the tree in the correct place. With the given number into the tree in the correct place. Using the insert( ) function. Using the insert( ) function.

C++ Code struct node* NewNode(int data) { struct node* node = new (struct node); node->data = data; node->left = NULL; node->right = NULL; struct node* NewNode(int data) { struct node* node = new (struct node); node->data = data; node->left = NULL; node->right = NULL; return(node); return(node);

printTree( ) Given a binary search tree, iterate over the nodes to print them out in increasing order. So the tree... Given a binary search tree, iterate over the nodes to print them out in increasing order. So the tree... 4 / \ 2 5 / \ / \ 2 5 / \ 1 3 Produces the output " ". This is known as an "inorder" traversal of the tree. Produces the output " ". This is known as an "inorder" traversal of the tree.

Print in-order traversal void printTree(struct node* node) { if (node == NULL) void printTree(struct node* node) { if (node == NULL) return; return; printTree(node->left); printf("%d ", node->data); printTree(node->right); } printTree(node->left); printf("%d ", node->data); printTree(node->right); }