# ADT Tree by dfhdhdhdhjr

VIEWS: 9 PAGES: 25

• pg 1
ADT Tree
Tree : nonempty collection of vertices and edges that satisfies certain requirements.
Vertex (node): Have a name and carry other associated information
Edge: A connection between two vertices.

We usually draw trees with the root (node A) at the top and leaves at the bottom!

1
ADT Tree
• Any node (except the root) has exactly one edge running upward to
another node. The node above it is called the parent of the node. For
example, D is the parent of H and I. G is the parent of J and K.

• The nodes below a given node are called its children. For example,
B, C and D are children of A. L is a child of H.

• Nodes with no children are known as leaves. For example,
E,F,J,L,M and N are leaves.

• A node that has children is an internal node. For example, C is an
internal node.

• The height of a tree is the length (i.e. number of edges) of a longest
path from the root to a leaf. For example, the height of the above 2
ADT Tree
Subtree

Subtree

3
ADT Binary Tree
Binary tree : no node can have more than two children (left child and right child).

4
Expression Tree



+
_

A         B                                    E

C        D

•   Expression tree for (A + B)  ((C / D) – E).
•   Leaves are operands (constants or variable names). Internal nodes are operators
•   If we traverse the above tree in postorder we will get
AB+ CD/E -  which is the postfix of (A + B)  ((C / D) – E).                     5
Complete Binary Tree

• All internal nodes have (exactly) two children
• All leaves are at the same level
• Height of an n-node complete binary tree is log n
6
Implementation : ADT Binary Tree

Data Structure:
typedef struct Node *Node_ptr;
struct Node
{
ElementType element;
Node_ptr     left;
Node_ptr      right;
};

typedef Node_ptr tree;                    7
ADT Binary Tree - Operations
•   create an empty tree
•   add a new node to the tree
•   remove a node from the tree.
•   tree traversal : preorder, inorder and post order

8
Tree traversals
Inorder:

• Traverse the left subtree in inorder
• Visit the root (and print the value )
• Traverse the right subtree in inorder

9
Tree traversals
Preorder:

• Visit the root (and print the value )
• Traverse the left subtree in preorder
• Traverse the right subtree in preorder

10
Tree traversals
Postorder:

• Traverse the left subtree in postorder
• Traverse the right subtree in postorder
• Visit the root (and print the value )

11
Tree traversals
Inorder : B A C
A

Preorder : A B C

Postorder : B C A
B        C

12
Tree traversals
Inorder:   DBE A FCG
A

Preorder: A   BDE CFG
C
B
Postorder: D E B   FGCA

G
D       E       F

13
Tree traversals
Inorder :
A
GDH B       A EI C JFK

C
B

Preorder:
D
E               F       A B DGH C EI FJK

G       H               I       J       K

Postorder:
GHD B IE JKF C           A

14
ADT Binary Search Tree (BST)

•   Binary search tree is a binary tree such that for every node, X, in the tree, the values of the
all the nodes in the left subtree are less the value stored at X, and the values of all the
nodes in the right subtree are equal or larger than the value stored at X ( BST Property).

• That is, a node’s left child must have a value less than its parent, and a node’s
right child must have a value equal or greater than its parent .

15
ADT Binary Search Tree (BST)

Operations:
• create a BST
• insert an element
• delete an element
• find an element (search)
• display all elements in a sorted order

16
ADT Binary Search Tree (BST)

Insert:
• Start at the root
• If value we want to insert is less than value of current node, we have to go to the
left subtree
• Otherwise we have to go to the right subtree
• If the current node is empty (not existing) create a node with the value we are
inserting and place it here.

17
Building a BST
54, 17, 87, 11, 19, 18, 93
54

54               54
54                                                                                      87
17

17             87
17                                                    11

54                                               54

17         87
17              87

11          19         93
11         19

18
18
ADT Binary Search Tree (BST)
Delete:
Similar to the insert function, after deletion of a node, the BST property must be
maintained.

There are 3 possible cases:
•     Node to be deleted has no children
 We just delete the node.

•     Node to be deleted has only one child
 The node can be deleted by replacing the node by that child.

•     Node to be deleted has two children

19
ADT Binary Search Tree (BST) – Delete (contd.)

Node to be deleted has two children

20
ADT Binary Search Tree (BST) – Delete (contd.)
Node to be deleted has two children
Steps - A:
•    Find minimum value of right subtree
•    Delete minimum node of right subtree but keep its value
•    Replace the value of the node to be deleted by this minimum
value
OR
Steps - B:
•    Find maximum value of left subtree
•    Delete maximum node of left subtree but keep its value
•    Replace the value of the node to be deleted by this maximum
value
21
ADT Binary Search Tree (BST) – Delete (contd.)
Steps-A

22
ADT Binary Search Tree (BST)

Search-
for element X in Tree T (r is the value stored at root)

if T is empty
"X is not in the tree";
else {
if X = r
"X is found"
else {
if    X<r
search the left tree
else
search the right tree            23
}
ADT Binary Search Tree (BST)
Using the data structure described in page 7, the above algorithm can be implemented in C as follows:

tree Binarysearch (ElementType X, tree T)
{if (T = NULL)
ERROR("X is not in the tree") ;
else {
if (X = T->element)
return T;
else {
if (X < T->element)
return Binarysearch (X, T->left);
else
return Binarysearch (X, T->right);
}
}
24
}
Unsorted list           Sorted                     BST        Balanced BST
list                       Worst case

INSERT            O(1)                    O(n)                       O(n)           O(log n)
or
O(n log n) – using
sorting gorithm

SEARCH            O(n)-linear search      O(log n) – Binary search O(n)             O(log n)

An AVL (Adelson-Velskii and Landis) tree is a balanced BST with the height O(log n)

25

To top