Data Structure - Binary trees - Notes, Study notes of Data Structures and Algorithms

Description about DATA AND FILE STRUCTURES , Binary tree, Picture of a binary tree, Size and depth, Tree Example, Binary Tree Example.

Typology: Study notes

2010/2011

Uploaded on 09/01/2011

visir66
visir66 🇮🇳

4.4

(74)

97 documents

1 / 51

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
DATA AND FILE STRUCTURES
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33

Partial preview of the text

Download Data Structure - Binary trees - Notes and more Study notes Data Structures and Algorithms in PDF only on Docsity!

DATA AND FILE STRUCTURES

Binary tree

  • (^) A binary tree is composed of zero or more nodes
  • (^) Each node contains:
    • (^) A value (some sort of data item)
    • (^) A reference or pointer to a left child (may be null), and
    • (^) A reference or pointer to a right child (may be null)
  • (^) A binary tree may be empty (contain no nodes)
  • (^) If not empty, a binary tree has a root node
    • (^) Every node in the binary tree is reachable from the root node by a unique path
  • (^) A node with neither a left child nor a right child is

called a leaf

  • (^) In some binary trees, only the leaves contain a value

Size and depth

• The size of a binary tree

is the number of nodes in

it

  • (^) This tree has size 12

• The depth of a node is its

distance from the root

  • (^) a is at depth zero
  • (^) e is at depth 2

• The depth of a binary tree

is the depth of its deepest

node

  • (^) This tree has depth 4

a

b c

d e f

g h i j k

l

Tree Example: UNIX Directory

Tree Traversal (Definition)

• The process of systematically visiting all the

nodes in a tree and performing some

computation at each node in the tree is called a

tree traversal.

• There are two methods in which to traverse a

tree:

1. Breadth-First Traversal.

2. Depth-First Traversal:

  • (^) Preorder traversal
  • (^) Inorder traversal (for binary trees only)
  • (^) Postorder traversal

Tree Traversal Methods

  • (^) Pre-order
    1. Visit node // first
    2. Recursively visit left subtree
    3. Recursively visit right subtree
  • (^) In-order
    1. Recursively visit left subtree
    2. Visit node // second
    3. Recursively right subtree
  • (^) Post-order
    1. Recursively visit left subtree
    2. Recursively visit right subtree
    3. Visit node // last

Depth-first Preorder Traversal

H

D

B

A

C

E

G

I

K M O

N

L

J

F

H D B A C F E G L J I K N M O

N-L-R

Depth-first Inorder Traversal

H

D

B

A

C

E

G

I

K M O

N

L

J

F

L-N-R

A B C D E F G H I J K L M N O

. Note: An inorder traversal of a BST visits the keys sorted in increasing order

Tree Traversal Methods

• Breadth-first

BFS(Node n) {

Queue Q = new Queue();

Q.enqueue(n); // insert node into Q

while ( !Q.empty()) {

n = Q.dequeue(); // remove next node

if ( !n.isEmpty()) {

visit(n); // visit node

Q.enqueue(n.Left()); // insert left subtree in Q

Q.enqueue(n.Right()); // insert right subtree in Q

Breadth-First Traversal

H

D

B

A

C

E

G

I

K M O

N

L

J

F

H D L B F J N A C E G I K M O

Tree Traversal Examples

• Pre-order

  • (^) 44, 17, 32, 78,

• In-order

  • (^) 17, 32, 44, 48,

• Post-order

  • (^) 32, 17, 48, 62,

• Breadth-first

  • (^) 44, 17, 78, 32,

Binary search tree

Sorted

order!

Binary Tree Sort

• The Binary Tree Sort works on a simple

concept: The value at the root of a tree is

always greater than all the values in its left

subtree and less than or equal to all the

values in its right subtree.

• We can search for a value by comparing

that value to the root, and if the value is

smaller, examine the left subtree, and if the

value is larger examine the right subtree.

Binary Tree Sort

• The first step, then, is to take all the values

and build a binary search tree.

• The basis of this process is a function to

insert a new value into an existing binary

search tree.

left subtree exists?

YES

NO

attach value

as a left

subtree

try the left

subtree

Binary Tree Sort

value < value at root?

NO

YES

right subtree exists?

YES

NO

attach value

as a right

subtree

try the right

subtree

Value must be in

the left subtree

Value must be in

the right subtree