computer science c++ high school level by guillermo moreno

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

Upload: melissa-ryan

Post on 16-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Science C++ High School Level By Guillermo Moreno

Computer Science C++

High School LevelHigh School Level

By Guillermo MorenoBy Guillermo Moreno

Page 2: 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 pointer1. Left pointer

2. Right pointer2. Right pointer

3. Data element3. Data element

Page 3: Computer Science C++ High School Level By Guillermo Moreno
Page 4: Computer Science C++ High School Level By Guillermo Moreno

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 no nodes, is an empty tree.

Page 5: Computer Science C++ High School Level By Guillermo Moreno

If the set is not empty:

1. There is one special node: the root1. There is one special node: the root

2. Each node is associated with two 2. Each node is associated with two different nodes:different nodes:

A) left childA) left child

B) right childB) right child

Page 6: Computer Science C++ High School Level By Guillermo Moreno

The parent node:

If a node “C” is the child of another node If a node “C” is the child of another node “P”,“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 Each node, except the root, has exactly one

parent.parent. The root has no parent.The root has no parent.

Page 7: Computer Science C++ High School Level By Guillermo Moreno

Definitions:

Parent. The parent of the node is the node Parent. The parent of the node is the node linked above it.linked above it.

Sibling. Two nodes are siblings if they have Sibling. Two nodes are siblings if they have the same parent.the same parent.

Ancestor. A node’s parent is its first Ancestor. A node’s parent is its first ancestor.ancestor.

Page 8: Computer Science C++ High School Level By Guillermo Moreno

Descendant. A node’s children are its first Descendant. A node’s children are its first descendants.descendants.

Depth. The depth of a tree is the maximum depth Depth. The depth of a tree is the maximum depth of any of its leaves.of any of its leaves.

Full Tree. Every leaf has the same depth, and Full Tree. Every leaf has the same depth, and every non leaf has two children.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.

Page 9: Computer Science C++ High School Level By Guillermo Moreno

Node. Each element of the set is called a Node. Each element of the set is called a node of the tree.node of the tree.

Each node contains some data.Each node contains some data.

The data could be an integer, double The data could be an integer, double numbers, strings or characters.numbers, strings or characters.

Page 10: Computer Science C++ High School Level By Guillermo Moreno

Code:

In C or C++, the binary tree is built with a node In C or C++, the binary tree is built with a node type like this... type like this...

struct node { struct node {     int data;     int data;     struct node* left;     struct node* left;     struct node* right;     struct node* right; } }

Page 11: Computer Science C++ High School Level By Guillermo Moreno

1 . . 2 3

. . . . 4 5 6 7 . . . . . . . . 8 9 10 12 14

Page 12: Computer Science C++ High School Level By Guillermo Moreno

Breadth-First Traversal

A A breadth-firstbreadth-first traversal of a tree starts at traversal of a tree starts at the root of the tree. It next visits the the root of the tree. It next visits the children, then the grand-children and so on. children, then the grand-children and so on.

The numbers indicate the order in which the The numbers indicate the order in which the nodes are visited, not the contents of the nodes are visited, not the contents of the nodes.nodes.

Page 13: Computer Science C++ High School Level By Guillermo Moreno

Binary Search Trees

Demonstration:Demonstration:

There is a demonstration of Binary Search There is a demonstration of Binary Search Trees [Trees [herehere]. ].

Insert and delete data (strings) into a binary Insert and delete data (strings) into a binary search tree (BST) search tree (BST)

Page 14: Computer Science C++ High School Level By Guillermo Moreno

Insertion

Given a binary search tree and a number, Given a binary search tree and a number, insert a new node.insert a new node.

With the given number into the tree in the With the given number into the tree in the correct place. correct place.

Using the insert( ) function.Using the insert( ) function.

Page 15: Computer Science C++ High School Level By Guillermo Moreno

C++ Code

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

return(node); return(node);

Page 16: Computer Science C++ High School Level By Guillermo Moreno

printTree( )

Given a binary search tree, iterate over the nodes Given a binary search tree, iterate over the nodes to print them out in increasing order. So the tree... to print them out in increasing order. So the tree...             

4 4       / \       / \      2   5      2   5     / \     / \    1   3    1   3

Produces the output "1 2 3 4 5". This is known as Produces the output "1 2 3 4 5". This is known as an "inorder" traversal of the tree.an "inorder" traversal of the tree.

Page 17: Computer Science C++ High School Level By Guillermo Moreno

Print in-order traversal

void printTree(struct node* node) { void printTree(struct node* node) {   if (node == NULL)   if (node == NULL)

return;   return;   printTree(node->left); printTree(node->left);

    printf("%d ", node->data); printf("%d ", node->data);     printTree(node->right); printTree(node->right); } }