0% found this document useful (0 votes)
191 views48 pages

Trees Lecture For Students-Animated

The document discusses different types of tree data structures including binary trees, binary search trees, and their common traversals. It provides examples of inserting nodes into a binary search tree and traversing it using inorder, preorder, and postorder traversal methods. Key terms like root, child, parent, ancestor, descendant, sibling, and height of a tree are explained. Examples of full binary trees of different levels are shown to illustrate recursive definitions.

Uploaded by

jscansino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
191 views48 pages

Trees Lecture For Students-Animated

The document discusses different types of tree data structures including binary trees, binary search trees, and their common traversals. It provides examples of inserting nodes into a binary search tree and traversing it using inorder, preorder, and postorder traversal methods. Key terms like root, child, parent, ancestor, descendant, sibling, and height of a tree are explained. Examples of full binary trees of different levels are shown to illustrate recursive definitions.

Uploaded by

jscansino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 48

Trees, Trees, and More Trees

1
Trees, Trees, and More Trees

By looking at forests of terms, awesome animations,


and complete examples, we hope to get at the root of
trees. Hopefully, each of you leaves with something
new and exciting learned about trees. Try not to lose
sight of the forest through the trees. Sorry, this was all
very bad. I hope it didn’t leaf a bad taste in your
mouth.

2
TREES

A binary tree is a set of elements that is either empty or


contains a single element (the root of the tree) and
whose remaining elements are partitioned into two
disjoint subsets, each of which itself is a binary tree.

3
root H Right subtree
Left subtree rooted at P

P
D

G L W
A

E R

4
H
root level 0
Left subtree Right subtree

P
D
level 1

G L W
A
level 2

E R
level 3
5
Full Binary Tree of Level n

6
A is the root of the tree.
A is the parent of B and C.
A is an ancestor of all nodes.

B C

D E F G

N O
H I J K L M

7
B and C are siblings.
J is a descendent of B.

B C

D E F G

N O
H I J K L M

8
Height of binary tree :
• Number of nodes on the longest path from
the root to a leaf.

• The height of the empty tree is 0.

• The height of a single node tree is 1.


• Note: (not an AP term) the definition of “height” is not from some “cs
bible” – some will define other ways

9
Height of binary tree ?

B C

D E F G

N O
H I J K L M

10
Height of binary tree ?

B C

D E F G

N O
H I J K L M

Height = 4

11
Full Binary : A recursive definition

A binary tree is full


if the tree is empty
or
if the tree's left and right subtrees have the same height and both are full
12
Binary Search Tree

• A binary search tree :


– every node in the left subtree is less than or equal to its
parent node.
– Every node in the right subtree is greater than its parent
node
– When the tree is traversed in order, the values of the nodes
will be in order.

13
To insert to a binary search tree:

if value less than


go left
else
go right

14
Insert :
15 8 25 6 14 24 20 22 30 13 26

15

15
Insert :
14 8 25 6 14 24 20 22 30 13 26

15

16
Insert :
14 8 25 6 14 24 20 22 30 13 26

15

8 25

17
Insert :
15 8 25 6 14 24 20 22 30 13 26

15

8 25

18
Insert :
15 8 25 6 14 24 20 22 30 13 26

15

8 25

6
14

19
Insert :
15 8 25 6 14 24 20 22 30 13 26

15

8 25

6
14 24

20
Insert :
15 8 25 6 14 24 20 22 30 13 26

15

8 25

6
14 24

20

21
Insert :
15 8 25 6 14 24 20 22 30 13 26

15

8 25

6
14 24

20

22

22
Insert :
15 8 25 6 14 24 20 22 30 13 26

15

8 25

6
14 24 30

20

22

23
Insert :
15 8 25 6 14 24 20 22 30 13 26

15

8 25

6 24
14 30

20
13
22

24
Insert :
15 8 25 6 14 24 20 22 30 13 26

15

8 25

6 24
14 30

26
13 20

22
25
Tree Traversals

See also: animations on web site


26
Inorder traversal of a binary tree
left…root…right

B C

D E F G

N O
H I J K L M

27
Inorder traversal of a binary tree
left…root…right

B C

D E F G

N O
H I J K L M

HDIBJ EKALFMCNGO
28
Preorder traversal of a binary tree:
root…left…right

B C

D E F G

N O
H I J K L M

29
Preorder traversal of a binary tree:
root…left…right

B C

D E F G

N O
H I J K L M

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

30
Postorder traversal of a binary tree:
left…right…ROOT

B C

D E F G

N O
H I J K L M

31
Postorder traversal of a binary tree:
left…right…ROOT

B C

D E F G

N O
H I J K L M

HI D J KE B LM FN O G CA
32
Inorder traversal ?

B W

X E F G

T E
A Q P

33
Inorder traversal :

B W

X E F G

T E
A Q P

XA B E Q L P F W T G E

34
Preorder traversal ?

B W

X E F G

T E
A Q P

35
Preorder traversal:

B W

X E F G

T E
A Q P

L B X A E Q W F PG T E

36
Postorder traversal ?

B W

X E F G

T E
A Q P

37
Postorder traversal :

B W

X E F G

T E
A Q P

AXQEBPFTEGWL

38
breadth-first-order tree traversal
• ROW (or level) order traversal

A
B C

D E F G

H I

39
breadth-first-order tree traversal
• ROW (or level) order traversal

ABCDEFGHI

A
B C

D E F G
H I

40
Inorder Traversal ?

15

8 25

6 24
14 30

26
13 20

22

41
Inorder Traversal :
15

8 25

6 24
14 30

26
13 20

22

6 8 13 14 15 20 22 24 25 26 30
42
Preorder Traversal ?
15

8 25

6 24
14 30

26
13 20

22

43
Preorder Traversal :
15

8 25

6 24
14 30

26
13 20

22

15 8 6 14 13 25 24 20 22 30 26
44
Postorder Traversal ?
15

8 25

6 24
14 30

26
13 20

22

45
Postorder Traversal :
15

8 25

6 24
14 30

26
13 20

22

6 13 14 8 22 20 24 26 30 25 15
46
Our Tree:
15

8 25

6 24
14 30

26
13 20

22

What is the height of the tree?

47
Height :
15

8 25

6 24
14 30

26
13 20

22

Height = 5

48

You might also like