Download presentation

Presentation is loading. Please wait.

Published byMelissa Ryan Modified about 1 year ago

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

2
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

3

4
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.

5
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

6
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.

7
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.

8
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.

9
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.

10
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; }

11

12
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.

13
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)

14
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.

15
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);

16
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.

17
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); }

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google