Jisa
0
Q:

binary tree in python

class Node:

    def __init__(self, data):

        self.left = None
        self.right = None
        self.data = data

    def insert(self, data):
# Compare the new value with the parent node
        if self.data:
            if data < self.data:
                if self.left is None:
                    self.left = Node(data)
                else:
                    self.left.insert(data)
            elif data > self.data:
                if self.right is None:
                    self.right = Node(data)
                else:
                    self.right.insert(data)
        else:
            self.data = data

# Print the tree
    def PrintTree(self):
        if self.left:
            self.left.PrintTree()
        print( self.data),
        if self.right:
            self.right.PrintTree()

# Use the insert method to add nodes
root = Node(12)
root.insert(6)
root.insert(14)
root.insert(3)

root.PrintTree()
5
Binary tree - each node can have at most 2 nodes, Binary Search tree - is a binary tree and put smaller values on the left and larger values on the right of the root.
1
#!/usr/bin/python

class Node:
    def __init__(self, val):
        self.l = None
        self.r = None
        self.v = val

class Tree:
    def __init__(self):
        self.root = None

    def getRoot(self):
        return self.root

    def add(self, val):
        if(self.root == None):
            self.root = Node(val)
        else:
            self._add(val, self.root)

    def _add(self, val, node):
        if(val < node.v):
            if(node.l != None):
                self._add(val, node.l)
            else:
                node.l = Node(val)
        else:
            if(node.r != None):
                self._add(val, node.r)
            else:
                node.r = Node(val)

    def find(self, val):
        if(self.root != None):
            return self._find(val, self.root)
        else:
            return None

    def _find(self, val, node):
        if(val == node.v):
            return node
        elif(val < node.v and node.l != None):
            self._find(val, node.l)
        elif(val > node.v and node.r != None):
            self._find(val, node.r)

    def deleteTree(self):
        # garbage collector will do this for us. 
        self.root = None

    def printTree(self):
        if(self.root != None):
            self._printTree(self.root)

    def _printTree(self, node):
        if(node != None):
            self._printTree(node.l)
            print str(node.v) + ' '
            self._printTree(node.r)

#     3
# 0     4
#   2      8
tree = Tree()
tree.add(3)
tree.add(4)
tree.add(0)
tree.add(8)
tree.add(2)
tree.printTree()
print (tree.find(3)).v
print tree.find(10)
tree.deleteTree()
tree.printTree()
2
// C++ program to find if there is a duplicate 
// sub-tree of size 2 or more. 
#include<bits/stdc++.h> 
using namespace std; 
  
// Separator node 
const char MARKER = '$'; 
  
// Structure for a binary tree node 
struct Node 
{ 
    char key; 
    Node *left, *right; 
}; 
  
// A utility function to create a new node 
Node* newNode(char key) 
{ 
    Node* node = new Node; 
    node->key = key; 
    node->left = node->right = NULL; 
    return node; 
} 
  
unordered_set<string> subtrees; 
  
// This function returns empty string if tree 
// contains a duplicate subtree of size 2 or more. 
string dupSubUtil(Node *root) 
{ 
    string s = ""; 
  
    // If current node is NULL, return marker 
    if (root == NULL) 
        return s + MARKER; 
  
    // If left subtree has a duplicate subtree. 
    string lStr = dupSubUtil(root->left); 
    if (lStr.compare(s) == 0) 
       return s; 
  
    // Do same for right subtree 
    string rStr = dupSubUtil(root->right); 
    if (rStr.compare(s) == 0) 
       return s; 
  
    // Serialize current subtree 
    s = s + root->key + lStr + rStr; 
  
    // If current subtree already exists in hash 
    // table. [Note that size of a serialized tree 
    // with single node is 3 as it has two marker 
    // nodes. 
    if (s.length() > 3 && 
        subtrees.find(s) != subtrees.end()) 
       return ""; 
  
    subtrees.insert(s); 
  
    return s; 
} 
  
// Driver program to test above functions 
int main() 
{ 
    Node *root = newNode('A'); 
    root->left = newNode('B'); 
    root->right = newNode('C'); 
    root->left->left = newNode('D'); 
    root->left->right = newNode('E'); 
    root->right->right = newNode('B'); 
    root->right->right->right = newNode('E'); 
    root->right->right->left= newNode('D'); 
  
    string str = dupSubUtil(root); 
  
    (str.compare("") == 0) ? cout << " Yes ": 
                             cout << " No " ; 
    return 0; 
} 
0

New to Communities?

Join the community