Difference between revisions of "All about Binary search tree"
From Hawk Wiki
(→Binary tree depth) |
|||
Line 3: | Line 3: | ||
===Tree loop all=== | ===Tree loop all=== | ||
===Tree to linked list=== | ===Tree to linked list=== | ||
− | < | + | <pre class="php"><?php |
class bTree{ | class bTree{ | ||
public $val=null; | public $val=null; | ||
Line 69: | Line 69: | ||
$list=$list->left; | $list=$list->left; | ||
} | } | ||
− | </ | + | </pre> |
===Deleting node=== | ===Deleting node=== | ||
http://adtinfo.org/libavl.html/Deleting-from-a-BST.html<br> | http://adtinfo.org/libavl.html/Deleting-from-a-BST.html<br> | ||
− | < | + | <pre class="php"> |
function deleteNodeFromTree(&$tree,$val){ | function deleteNodeFromTree(&$tree,$val){ | ||
if($tree===null) | if($tree===null) | ||
Line 130: | Line 130: | ||
deleteNodeFromTree($tree,10); | deleteNodeFromTree($tree,10); | ||
var_dump($tree); | var_dump($tree); | ||
− | </ | + | </pre> |
===Binary search=== | ===Binary search=== | ||
− | < | + | <pre class="php"> |
def search_binary_tree(node, key): | def search_binary_tree(node, key): | ||
if node is None: | if node is None: | ||
Line 142: | Line 142: | ||
else: # key is equal to node key | else: # key is equal to node key | ||
return node.value # found key | return node.value # found key | ||
− | </ | + | </pre> |
===Binary tree insert=== | ===Binary tree insert=== | ||
− | < | + | <pre class="php"> |
/* Inserts the node pointed to by "newNode" into the subtree rooted at "treeNode" */ | /* Inserts the node pointed to by "newNode" into the subtree rooted at "treeNode" */ | ||
void InsertNode(Node* &treeNode, Node *newNode) | void InsertNode(Node* &treeNode, Node *newNode) | ||
Line 155: | Line 155: | ||
InsertNode(treeNode->right, newNode); | InsertNode(treeNode->right, newNode); | ||
} | } | ||
− | </ | + | </pre> |
===Binary tree depth=== | ===Binary tree depth=== | ||
− | < | + | <pre class="php"> |
− | + | ||
/////////////////////////////////////////////////////////////////////// | /////////////////////////////////////////////////////////////////////// | ||
// Get depth of a binary tree | // Get depth of a binary tree | ||
Line 176: | Line 175: | ||
} | } | ||
</pre> | </pre> | ||
− |
Revision as of 23:05, 3 April 2012
Contents
Some code of binary search tree
Tree insert
Tree loop all
Tree to linked list
<?php class bTree{ public $val=null; public $left=null; public $right=null; function bTree($val0){ $this->val=$val0; } }; function insert(&$tree,$val){ if($tree==null){ $tree=new bTree($val); } else{ if($val>$tree->val){ insert($tree->right,$val); } else{ insert($tree->left,$val); } } } function outputTree($tree){ if($tree==null) return; outputTree($tree->left); echo $tree->val." "; outputTree($tree->right); } $currentNode=null; function tree2List(&$tree,&$list=null){ global $currentNode; if($tree===null) return; tree2List($tree->left,$list); if($list===null){ $list=new bTree($tree->val); //create new | first left most element } else{ $list->right=new bTree($tree->val);//only create new element on the right $list->right->left=$list; $list=$list->right; } echo $list->val." "; tree2List($tree->right,$list); } $tree=null; insert($tree,10); insert($tree,5); insert($tree,2); insert($tree,15); insert($tree,20); insert($tree,12); echo "Tree output:<br />"; outputTree($tree); echo "<br />"; echo "Tree to Linked list output:<br />"; $list=null; tree2List($tree,$list); echo "<br />"; echo "Link list (backwards after conversion): "; while($list!==null){ echo $list->val." "; $list=$list->left; }
Deleting node
http://adtinfo.org/libavl.html/Deleting-from-a-BST.html
function deleteNodeFromTree(&$tree,$val){ if($tree===null) return; if($tree->val==$val){ echo "Got this val in tree<br/>"; //case 1 (hard) right child has left child if($tree->right!==null&&$tree->right->left!==null){ echo "case 1: right child has left child<br />"; $treetmp=&$tree->right->left; while($treetmp->left!=null){//find the right branch left most child. This value greater than this->val but smaller than all right branches $treetmp=&$treetmp->left; } $tree->val=$treetmp->val; //Replace(delete) this val $treetmp=null;//Delete that node left most node } //case 2 right child has no left node else if($tree->right!==null&&$tree->right->left===null){ //replace the current val with the right one echo "case 2: right child has no left child<br />"; $tree->val=$tree->right->val; $tree->right=$tree->right->right; } //case 3: no right child else if($tree->right===null){ echo "case 3: no right child<br />"; if($tree->left!==null){ echo "case 3.a: Node has left child<br />"; $tree->val=$tree->left->val; $tree->left=$tree->left->left; } else{ echo "case 3.b: Node has no child<br />"; $tree=null; } } } else if($val>$tree->val){ deleteNodeFromTree($tree->right,$val); } else{ deleteNodeFromTree($tree->left,$val); } } //create tree $tree=null; insert($tree,10); insert($tree,5); insert($tree,2); insert($tree,15); insert($tree,20); insert($tree,12); //delete from tree var_dump($tree); deleteNodeFromTree($tree,10); var_dump($tree);
Binary search
def search_binary_tree(node, key): if node is None: return None # key not found if key < node.key: return search_binary_tree(node.leftChild, key) elif key > node.key: return search_binary_tree(node.rightChild, key) else: # key is equal to node key return node.value # found key
Binary tree insert
/* Inserts the node pointed to by "newNode" into the subtree rooted at "treeNode" */ void InsertNode(Node* &treeNode, Node *newNode) { if (treeNode == NULL) treeNode = newNode; else if (newNode->key < treeNode->key) InsertNode(treeNode->left, newNode); else InsertNode(treeNode->right, newNode); }
Binary tree depth
/////////////////////////////////////////////////////////////////////// // Get depth of a binary tree // Input: pTreeNode - the head of a binary tree // Output: the depth of a binary tree /////////////////////////////////////////////////////////////////////// int TreeDepth(SBinaryTreeNode *pTreeNode){ // the depth of a empty tree is 0 if(!pTreeNode) return 0; // the depth of left sub-tree int nLeft = TreeDepth(pTreeNode->m_pLeft); // the depth of right sub-tree int nRight = TreeDepth(pTreeNode->m_pRight); // depth is the binary tree return (nLeft > nRight) ? (nLeft + 1) : (nRight + 1); }