trees data structure

20
using Java 2015 Data Structure Prepared by: Mahmoud Rafeek Al-farra in Java 9. Trees

Upload: mahmoud-alfarra

Post on 15-Jan-2017

347 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Trees data structure

using Java

2015

Data Structure Prepared by: Mahmoud Rafeek Al-farra

in Java

9. Trees

Page 2: Trees data structure

mfarra.cst.ps www.fb.com/MahmoudRFarra

Contents

Building and Searching in BST

Binary Search Tree

Introduction

Tree Traversal

BST Implementation

Page 3: Trees data structure

Introductionmfarra.cst.ps www.fb.com/MahmoudRFarra

A tree is a classic data structure with many important applications.

A tree provides a hierarchical organization in which data are stored in the nodes.

Page 4: Trees data structure

Introductionmfarra.cst.ps www.fb.com/MahmoudRFarra

A tree is a set of nodes connected by edges. The nodes represent the entities (example:

people) that make up an organization. The edges represent the relationship between

the entities.

Page 5: Trees data structure

Introductionmfarra.cst.ps www.fb.com/MahmoudRFarra

Page 6: Trees data structure

Binary Search Treemfarra.cst.ps www.fb.com/MahmoudRFarra

A binary search tree can be implemented using a linked structure. (can be implemented using Array ?)

It either is empty or consists of an element, called the root, and two distinct binary trees, called the left subtree and right subtree, either or both of which may be empty.

Page 7: Trees data structure

Binary Search Treemfarra.cst.ps www.fb.com/MahmoudRFarra

The length of a path is the number of the edges

in the path. The depth of a node is

the length of the path from the root to the node.

The set of all nodes at a given depth

is sometimes called a level of the tree. Siblings

are nodes that share the same parent

node. The root of a left (right) subtree of a node

is called a left (right) child of the node.

A node without children is called a leaf. The

height of a nonempty tree is the length

of the path from the root node to its furthest leaf.

The height of a tree that contains a

single node is 0. Conventionally, the height of an

empty tree is —1.

Page 8: Trees data structure

Binary Search Treemfarra.cst.ps www.fb.com/MahmoudRFarra

The length of a path is the number of the edges

in the path.

The depth of a node is the length of the path

from the root to the node.

The set of all nodes at a given depth is

sometimes called a level of the tree.

Siblings are nodes that share the same parent

node.

Page 9: Trees data structure

Binary Search Treemfarra.cst.ps www.fb.com/MahmoudRFarra

The root of a left (right) subtree of a node is

called a left (right) child of the node.

A node without children is called a leaf.

The height of a nonempty tree is the length of

the path from the root node to its furthest leaf.

The height of a tree that contains a single node is

0.

The height of an empty tree is —1.

Page 10: Trees data structure

Binary Search Treemfarra.cst.ps www.fb.com/MahmoudRFarra

For an interactive GUI demo to see how a BST works, go to www.cs.armstrong.edu/liang/animation/web/BST.html

A BST (with no duplicate elements) has the

property that for every node in the tree, the

value of any node in its left subtree is less than

the value of the node, and the value of any node

in its right subtree is greater than the value of

the node.

Page 11: Trees data structure

Representing Binary Search Treesmfarra.cst.ps www.fb.com/MahmoudRFarra

A binary tree can be represented using a set of

linked nodes. Each node contains a value and two links named

left and right that reference the left child and right child.

Page 12: Trees data structure

Inserting an Element into a BSTmfarra.cst.ps www.fb.com/MahmoudRFarra

To insert an element into a BST, you need to locate where to insert it in the tree.

The key idea is to locate the parent for the new node.

Page 13: Trees data structure

Inserting an Element into a BSTmfarra.cst.ps www.fb.com/MahmoudRFarra

Page 14: Trees data structure

Searching for an Elementmfarra.cst.ps www.fb.com/MahmoudRFarra

To search for an element in the BST, you start

from the root and scan down from it until a

match is found or you arrive at an empty

subtree.

Page 15: Trees data structure

Searching for an Elementmfarra.cst.ps www.fb.com/MahmoudRFarra

Let current point to the root. Repeat the following steps until current is null or

the element matches current.element. If element is less than current.element, assign

current.left to current. If element is greater than current.element,

assign current.right to current. If element is equal to current.element, return

true. If current is null, the subtree is empty and the

element is not in the tree.

Page 16: Trees data structure

Searching for an Elementmfarra.cst.ps www.fb.com/MahmoudRFarra

Page 17: Trees data structure

Tree Traversalmfarra.cst.ps www.fb.com/MahmoudRFarra

Tree traversal is the process of visiting each node in the tree exactly once.

There are several ways to traverse a tree as: Inorder. Postorder. Preorder. depth-first. breadth-first.

Page 18: Trees data structure

BST Implementationmfarra.cst.ps www.fb.com/MahmoudRFarra

A concrete BST class can be defined to extend AbstractTree, as shown in the following figure.

Self Study: more about implementation of BST in the text book page 935.

Page 19: Trees data structure

BST Implementationmfarra.cst.ps www.fb.com/MahmoudRFarra

Page 20: Trees data structure

using Java

2015

FB: M a h m o u d R F a r r aYouTube: M a h m o u d R F a r SlidesShare: mralfarra

Thank you