0% found this document useful (0 votes)
45 views

Data Structure: Ali Kalakech

Binary trees are a nonlinear data structure that can be used to store hierarchical data. They consist of nodes connected in a parent-child relationship, with each node containing a value, and references or pointers to its left and right children. Common operations on binary trees include traversing the tree to visit each node, searching for a specific value, and inserting or deleting nodes. Tree traversals like preorder, inorder, and postorder recursively visit the nodes in different orders by first accessing the root, left child, and right child in various sequences. Binary search trees allow for efficient search, insert, and delete operations by organizing nodes to satisfy the binary search tree property where all left descendants are less than the parent and all right descendants are greater

Uploaded by

Mhd Bazzi
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)
45 views

Data Structure: Ali Kalakech

Binary trees are a nonlinear data structure that can be used to store hierarchical data. They consist of nodes connected in a parent-child relationship, with each node containing a value, and references or pointers to its left and right children. Common operations on binary trees include traversing the tree to visit each node, searching for a specific value, and inserting or deleting nodes. Tree traversals like preorder, inorder, and postorder recursively visit the nodes in different orders by first accessing the root, left child, and right child in various sequences. Binary search trees allow for efficient search, insert, and delete operations by organizing nodes to satisfy the binary search tree property where all left descendants are less than the parent and all right descendants are greater

Uploaded by

Mhd Bazzi
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/ 30

Data Structure

Ali Kalakech
[email protected]
1
Binary Trees

2
Linear data structures
Here are some of the data structures we have studied so
far:
Arrays
Singly-linked lists and doubly-linked lists
Circular lists
Stacks, queues

These all have the property that their elements can be


adequately displayed in a straight line
Binary trees are one of the simplest nonlinear data
structures

3
Parts of a 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

4
Implementation of a BT
struct BinaryTree{
int data;
BinaryTree* leftChild, RightChild;
};

5
Picture of a binary tree

b c

d e f

g h i j k

l
6
Size and depth
The size of a binary tree is the
number of nodes in it
a This tree has size 12

b c The depth of a node is its


distance from the root
d e f a is at depth zero
e is at depth 2

g h i j k
The depth of a binary tree is
the depth of its deepest node
l
This tree has depth 4

7
Balance
a a
b c b
c e
d e f g
d f
h i
g h
A balanced binary tree
i j
An unbalanced binary tree

A binary tree is balanced if every level above the lowest is “full”


(contains 2n nodes)
In most applications, a reasonably balanced binary tree is desirable

8
Tree traversals
A binary tree is defined recursively: it consists of a root, a left
subtree, and a right subtree

To traverse (or walk) the binary tree is to visit each node in the
binary tree exactly once

Tree traversals are naturally recursive

Since a binary tree has three “parts,” there are six possible ways
to traverse the binary tree:

root, left, right root, right, left


left, root, right right, root, left
left, right, root right, left, root

9
Preorder traversal

In preorder, the root is visited first

Here’s a preorder traversal to print out all the


elements in the binary tree:

void preorderPrint(BinaryTree *bt) {


if (bt == NULL) return;
printf(“%d”, bt->data);
preorderPrint(bt->leftChild);
preorderPrint(bt->rightChild);
}

10
Inorder traversal

In inorder, the root is visited in the middle

Here’s an inorder traversal to print out all the


elements in the binary tree:

void inorderPrint(BinaryTree *bt) {


if (bt == NULL) return;
inorderPrint(bt->leftChild);
printf(“%d”, bt->data);
inorderPrint(bt->rightChild);
}

11
Postorder traversal

In postorder, the root is visited last

Here’s a postorder traversal to print out all the


elements in the binary tree:

void postorderPrint(BinaryTree *bt) {


if (bt == NULL) return;
postorderPrint(bt->leftChild);
postorderPrint(bt->rightChild);
printf(“%d”, bt->data);
}

12
Tree traversals using “flags”
The order in which the nodes are visited during a tree traversal
can be easily determined by imagining there is a “flag” attached
to each node, as follows:

preorder inorder postorder

To traverse the tree, collect the flags:

A A A

B C B C B C

D E F G D E F G D E F G

ABDECFG DBEAFCG DEBFGCA


13
Binary Tree Traversals - example
Arithmetic Expression using binary tree
inorder traversal (infix expression)
A/B*C*D+E
preorder traversal (prefix expression)
+**/ABCDE
postorder traversal
(postfix expression)
AB/C*D*E+

14
Binary Tree Traversals
Inorder traversal (recursive version)
Typedef BinaryTree * tree_pointer;
output A / B * C * D + E
:
ptr
L
V
R

15
Binary Tree Traversals
Preorder traversal (VLR) (recursive version)

output + * * / A B C D E
:

V
L
R

16
Binary Tree Traversals
Postorder traversal (LRV) (recursive version)

output A B / C * D * E +
:

L
R
V

17
Binary Tree Traversals
Iterative inorder traversal
we use a stack to simulate recursion
5 4 11
8 3 14
2 17
1
A B
/ *C D
* E
+

L
V

output A / B*C * D + E node


:
18
Binary Search Trees
View today as data structures that can support
dynamic set operations.
Search, Minimum, Maximum, Predecessor, Successor, Insert, and
Delete.
Can be used to build
Dictionaries.
Priority Queues.
Basic operations take time proportional to the height of the
tree.

19
Binary Search Tree Property
Stored data must satisfy
the binary search tree
property. 56
 y in left subtree of x, then
data[y]  data[x].
 y in right subtree of x,
26 200
then data[y]  data[x].

18 28 190 213

12 24 27

20
Binary search in an array
Look at array location (lo + hi)/2

Searching for 5:
(0+6)/2 = 3
Using a binary
hi = 2;
(0 + 2)/2 = 1 lo = 2; search tree
(2+2)/2=2
7

3 13
0 1 2 3 4 5 6
2 3 5 7 11 13 17 2 5 11 17

21
Tree Search
BinaryTree* Tree-Search(BinaryTree**xx,,int
BinaryTree*Tree-Search(BinaryTree intkk))
1.{{ifif((((xx==
1. NULL)||||((kk==
==NULL) ==x->data)
x->data)))
2. returnxx;;
2. return
3. ifif((kk<<x->data)
3. x->data)
4.
4. return Tree-Search(x->leftChild
returnTree-Search( x->leftChild,,kk))
5.
5. else elsereturn Tree-Search(x->rightChild
returnTree-Search( x->rightChild,,kk))
}}
56

26 200

18 28 190 213

12 24 27

22
Iterative Tree Search
BinaryTree*
BinaryTree*Iterative-Tree-Search
Iterative-Tree-Search
(BinaryTree**xx,,int
(BinaryTree intkk))
56
1. while((xx!=
1.{{while !=NULL
NULL&& &&kk!=
!=x->data)
x->data)
26 200 2. {{ifif((kk<<x->data)
2. x->data)
3.
3. thenxx==x->leftChild;
then x->leftChild;
18 28 190 213
4.
4. elsexx==x->rightchild;}
else x->rightchild;}
5. returnxx;;
5. return
}}
12 24 27

The iterative tree search is more efficient on most computers.


The recursive tree search is more straightforward.

23
Finding Min & Max
The binary-search-tree property guarantees that:
» The minimum is located at the left-most node.
» The maximum is located at the right-most node.

Tree-Minimum(xx))
Tree-Minimum( Tree-Maximum(xx))
Tree-Maximum(
1. whilex->left
1. while x->left!=NULL
!=NULL 1. whilex->right
1. while x->right==NULL
NULL
2. xx==x->left
2. x->left;; 2.xx==x->right;
2. x->right;
3. returnxx
3. return 3. returnxx
3. return

Q: How long do they take?

24
Inserting the entry M into the list
B, E, G, H, J, K, N, P stored as a tree

25
Inserting the entry M into the list
B, E, G, H, J, K, N, P stored as a tree

26
A procedure for inserting a new entry in
a list stored as a binary tree

27
Copying a binary tree

In postorder, the root is visited last

Here’s a postorder traversal to make a complete copy


of a given binary tree:

BinaryTree* copyTree(BinaryTree* bt) {


BinaryTree* temp;
Temp = (BinaryTree* ) malloc(sizeof(BinaryTree));
if (bt == NULL) return NULL;
temp->left = copyTree(bt->leftChild);
temp->right = copyTree(bt->rightChild);
temp->data = bt->data;
return temp;
}

28
Other traversals
The other traversals are the reverse of these three
standard ones
That is, the right subtree is traversed before the left subtree is
traversed
Reverse preorder: root, right subtree, left subtree
Reverse inorder: right subtree, root, left subtree
Reverse postorder: right subtree, left subtree, root

29
30

You might also like