l-10 trees.ppt
TRANSCRIPT
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 1/24
Data Structures (FALL-2011)
Trees
Thursday, September 09, 2004
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 2/24
Data Structures (FALL-2011)
Trees: Basic terminology[1]
• Hierarchical data structure
• Each position in the tree is called a node
• The “top” of the tree is called the root
• The nodes immediately below a node are called itschildren; nodes with no children are called leaves (orterminal nodes), or terminal nodes, and the nodeabove a given node is its parent (or father)
• A node x is ancestor of node y if x is father of y or
father of some ancestor of y. y is called descendent of x. Ancestor of a node is its parent, grand parent or grand-grand parent or so on….
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 3/24
Data Structures (FALL-2011)
Trees: Basic terminology[2]
• Nodes with the same parent are siblings
• A node plus the collection of nodes beneath it
is called a subtree
• The number of nodes in the longest path from
the root to a leaf is the depth (or height) of the
tree
– is the depth 2 or 3?
– depends on the author
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 4/24
Data Structures (FALL-2011)
Root
Node A
Node H
Node B
Node G Node F Node E Node D Node C
Node K Node J
Node L
Node I
Nodes C, H, and I form a subtree Nodes H and I are siblings
C is H’s parent, H and I are children of C
What is the depth of this tree?
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 5/24
Data Structures (FALL-2011)
Binary Trees
• A commonly used type of tree is a binary tree
• Each node has at most two children
• The “tree” is a conceptual structure
• The data can be stored either in a dynamiclinked tree structure, or in contiguous
memory cells (array) according to a set pattern; in other words, implementation can be pointer-based or array-based
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 6/24
Data Structures (FALL-2011)
Binary trees
• A tree is called strictly binary tree if every non leaf
node has exactly two children.
• A strictly binary tree having n leaves always contain
2n-1 nodes• Text book definition of depth of a tree
– Depth of binary tree is maximum level of any leaf in the
tree.
– Root of tree is at level 0, and level of any other node in the
tree is one more than the level of its father
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 7/24
Data Structures (FALL-2011)
Binary Trees
• A complete binary tree of level d is the strictly binary
tree all of whose leaves are at level d
• A complete binary tree of level d has 2l nodes at each
level l where 0<=l <=d• Total number of nodes (tn) in a complete binary tree of
depth d will be:
1222222 1
0
210
d
d
j
jd tn
For a binary tree of height h there are O(2h) nodes
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 8/24
Data Structures (FALL-2011)
Binary Trees
• For a complete binary tree if number of nodes (tn) areknown, then we may find depth of the binary tree
1-1)(log
1)(log1
12
12
2
2
1
1
tnd
tnd
tn
tn
d
d
A binary tree with nodes n has height O(log2n)
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 9/24
Data Structures (FALL-2011)
Binary Trees Storage
• Linked List based implementation
• Array based implementation
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 10/24
Data Structures (FALL-2011)
Binary Tree: as a linked structure
• Each node in the tree consists of:
– The data, or value contained in the element
– A left child pointer (pointer to first child)
– A right child pointer (pointer to second child)
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 11/24
Data Structures (FALL-2011)
Binary Tree: as a linked structure
• A root pointer points to the root node
– Follow pointers to find every other element in the
tree
• Add and remove nodes by manipulating
pointers
• Leaf nodes have child pointers set to null
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 12/24
Data Structures (FALL-2011)
class CBinTree
{
struct Node
{
int value;Node *LeftChild,*RightChild;
}*Root;
/***Operations*********//************************/
};
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 13/24
Data Structures (FALL-2011)
ROOT
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 14/24
Data Structures (FALL-2011)
Binary Tree: contiguous storage
• Value in root node stored first, followed by left child,then right child
• Each successive level in the tree stored left to right;unused nodes in tree represented by a bit pattern to
indicate nothing stored there• Children of any given node n is stored in cells 2n and
2n + 1 (If array index starts at 1)
• Can calculate position for any given node
• Storage allocated as for full tree, even if many nodesempty
• For a tree of depth h we need array of 2h+1-1 cells
D S (FA 2011)
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 15/24
Data Structures (FALL-2011)
Binary Trees
• Full binary tree of height (depth) h: all nodes
at a height less than h have exactly two
children• Balanced binary tree: for each node, the
difference in depth of the right and left
subtrees is no more than one• Completely balanced tree: left and right
subtrees of every node have the same height
D t St t (FALL 2011)
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 16/24
Data Structures (FALL-2011)
Full Binary Tree
A very sparse,unbalanced tree
D t St t (FALL 2011)
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 17/24
Data Structures (FALL-2011)
Primitive Operations
• Left(node): Gives index/pointer of left child
• Right(node): Gives index/pointer of right child
• Parent(node): Returns index/pointer of parent
• Brother(node): Returns index/pointer of brother
• Root: Gives index/pointer of root node
• Info(Node): Data/Info stored at node
• IsLeft(node): Is node left child? Yes/No
• IsRight(node): Is node right child? Yes/No
D t St t (FALL 2011)
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 18/24
Data Structures (FALL-2011)
Common Operations
• Tree traversal
• Node addition
• Node deletion• Destroy
D t St t (FALL 2011)
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 19/24
Data Structures (FALL-2011)
Traversal of Binary Trees
• Pass through all nodes of tree
• Inorder (symmetric traversal)
• Preorder (depth first traversal)• Postorder
Data Str ct res (FALL 2011)
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 20/24
Data Structures (FALL-2011)
Trees Traversal
• Inorder
– (Left) Root (Right)
• Preorder – Root (Left) (Right)
• Postorder
– (Left) (Right) Root
Root
Left Right
Data Structures (FALL 2011)
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 21/24
Data Structures (FALL-2011)
Inorder Traversal
Left Root Right manner
• Left + Right• [Left*Right]+[Left+Right]
• (A*B)+[(Left*Right)+E)
• (A*B)+[(C*D)+E]
+
* +
A B * E
C D
(A*B)+(C*D+E)
Data Structures (FALL 2011)
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 22/24
Data Structures (FALL-2011)
Preorder Traversal
Root Left Right manner
• + Left Right
• + [*Left Right] [+Left Right]• +(*AB) [+ *Left Right E]
• +*AB + *C D E
+
* +
A B * E
C D
Data Structures (FALL 2011)
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 23/24
Data Structures (FALL-2011)
Postorder Traversal
Left Right Root manner
• Left Right +• [Left Right *] [Left Right+] +
• (AB*) [Left Right * E + ]+
• (AB*) [C D * E + ]+• AB* C D * E + +
+
* +
A B * E
C D
Data Structures (FALL 2011)
7/29/2019 L-10 Trees.ppt
http://slidepdf.com/reader/full/l-10-treesppt 24/24
Data Structures (FALL-2011)
Binary Search Tree
• Elements are sorted:
• For any node n in a binary search tree:
– The value at n is greater than the values of any of the nodes
in its left subtree – The value at n is less than the values of any of the nodes in
its right subtree
– Its left and right subtrees are binary search trees
• Need to define “greater than” and “less than” for thespecific data