def BFS():
    # #--------------------------------------DELETING/Getting rid of any previous graph-----------------------------------------
    global Graph, visuals, button, toolbar, G, fixed_nodes, fixed_positions
    pos = zoinks.hierarchy_pos(G, 1)
    txt = ""
    nts = Entry1_7.get()  #No. to search
    #This will deal with empty Entry inputs
    if len(nts) == 0:
        nts = list(tree.keys())[-1]
    nx.draw(G,
            pos,
            fixed=None,
            with_labels=True,
            node_size=800,
            node_color='skyblue',
            node_shape="s",
            alpha=0.5,
            linewidths=10,
            font_size=8,
            font_weight='bold')
    x = []
    j = 0
    for i in (list(G.nodes())):
        x.append(i)
        j = j + 1
        plt.title('Iteration {}'.format(j))
        nx.draw_networkx_nodes(G,
                               pos,
                               nodelist=x,
                               node_size=400,
                               node_color='yellow',
                               node_shape="s",
                               alpha=0.5,
                               linewidths=10,
                               font_size=8,
                               font_weight='bold')
        # visuals.after(1000)
        if str(i) == str(nts):
            break
        plt.pause(0.7)
        # plt.clf()
        # plt.show()
    txt = str(x)
    bfs = plt.text(0.5,
                   -0.1,
                   "BFS Order: " + txt,
                   size=12,
                   ha="center",
                   transform=ax.transAxes)  #Shows the Caption below the graph
def inorder():

    global Graph, visuals, button, toolbar, G, fixed_nodes, fixed_positions
    pos = zoinks.hierarchy_pos(G, 1)
    nts = Entry1_7.get()  #No. to search
    #This will deal with empty Entry inputs
    if len(nts) == 0:
        nts = list(tree.keys())[-1]
    txt = ""
    nx.draw(G,
            pos,
            fixed=None,
            with_labels=True,
            node_size=800,
            node_color='skyblue',
            node_shape="s",
            alpha=0.5,
            linewidths=10,
            font_size=8,
            font_weight='bold')
    x = []
    j = 0
    for i in (list(G.nodes())):
        x.append(i)
        j = j + 1
        plt.title('Iteration {}'.format(j))
        nx.draw_networkx_nodes(G,
                               pos,
                               nodelist=x,
                               node_size=400,
                               node_color='yellow',
                               node_shape="s",
                               alpha=0.5,
                               linewidths=10,
                               font_size=8,
                               font_weight='bold')
        if str(i) == str(nts):
            break
        plt.pause(0.7)
    # plt.figtext(str(x))
    txt = str(x)
    ino = plt.text(0.5,
                   -0.1,
                   "In-Order Traversal: " + txt,
                   size=12,
                   ha="center",
                   transform=ax.transAxes)  #Shows the Caption below the graph
def Creation():
    #--------------------------------------DELETING/Getting rid of any previous graph-----------------------------------------
    global Graph, visuals, button, toolbar, G, fixed_nodes, fixed_positions, tree
    visuals.get_tk_widget().pack_forget()
    button.pack_forget()
    toolbar.pack_forget()
    pylab.ion()
    #---------------------------------------------Graph Generation backend----------------------------------------------------
    #Adding the Graph into the TK window
    nos = Entry1.get()  #No. of Nodes
    # n=int(nos)
    nts = Entry1_7.get()  #No. to search
    #This will deal with empty Entry inputs
    if len(nos) == 0:
        nos = 0
    if len(nts) == 0:
        nts = 0
    n = int(nos)
    # viz.num_of_nodes=n
    tree = {}
    j = 2
    for i in range(1, n + 1):
        tree[i] = []
    for i in range(1, n + 1):
        if j <= n:
            tree[i].append(j)
            if j + 1 <= n:
                tree[i].append(j + 1)
            j = j + 2
    #------------------------------------------------------Creating a new Tree--------------------------------------------------
    G = nx.Graph()
    print(tree)
    #Generating the Tree G created using the backend list into the tree
    for i, j in tree.items():
        for k in j:
            G.add_edge(i, k)

    #Matlab: Drawing Graphs; This is where Visualization begins
    fig = plt.figure(dpi=100)
    ax = plt.subplot(111)
    ax.set_title('Complete Binary Tree', fontsize=10)

    # #Fixing the position static for the Graph
    #This line draws it on Canvas(Shows the result on canvas)
    pos = zoinks.hierarchy_pos(G, 1)
    nx.draw(G,
            pos,
            fixed=None,
            with_labels=True,
            node_size=800,
            node_color='skyblue',
            node_shape="s",
            alpha=0.5,
            linewidths=10,
            font_size=8,
            font_weight='bold')

    #Adding the Graph into the TK window
    visuals = FigureCanvasTkAgg(fig, master=Graph)  # A tk.DrawingArea.
    visuals.draw()
    button = tk.Button(master=Graph, text="Quit", command=Graph.quit)
    button.pack(side=tk.BOTTOM)
    visuals.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
def levelorder():
    # #--------------------------------------DELETING/Getting rid of any previous graph-----------------------------------------
    global Graph, visuals, button, toolbar, G, fixed_nodes, fixed_positions
    pos = zoinks.hierarchy_pos(G, 1)
    txt = ""
    nts = Entry1_7.get()  #No. to search
    #This will deal with empty Entry inputs
    if len(nts) == 0:
        nts = list(G.nodes())[-1]
    nx.draw(G,
            pos,
            fixed=None,
            with_labels=True,
            node_size=800,
            node_color='skyblue',
            node_shape="s",
            alpha=0.5,
            linewidths=10,
            font_size=8,
            font_weight='bold')
    x = [2**i
         for i in range(20)]  #Exponential split: No. of elements per level
    print(x)
    y = list(G.nodes())  #The nodelist
    lot = []  #Resultant level wise split list
    treelist = list(G.nodes())
    for i in range(len(x)):
        try:
            lot.append(y[x[i] - 1:x[i + 1] - 1])
        except IndexError as error:
            # Output expected IndexErrors.
            break
    lot = [ele for ele in lot if ele != []]
    j = 0
    for i in (lot):
        j = j + 1
        plt.title('Iteration {}'.format(j))
        nx.draw_networkx_nodes(G,
                               pos,
                               nodelist=i,
                               node_size=400,
                               node_color='yellow',
                               node_shape="s",
                               alpha=0.5,
                               linewidths=10,
                               font_size=8,
                               font_weight='bold')
        if int(nts) in i:
            nx.draw_networkx_nodes(G,
                                   pos,
                                   nodelist=[int(nts)],
                                   node_size=400,
                                   node_color='red',
                                   node_shape="s",
                                   alpha=0.5,
                                   linewidths=10,
                                   font_size=8,
                                   font_weight='bold')
            break
        plt.pause(0.7)
        # plt.clf()
        plt.show()
    txt = str(lot)
    bfs = plt.text(0.5,
                   -0.1,
                   "Level Wise Traversal: " + txt,
                   size=12,
                   ha="center",
                   transform=ax.transAxes)  #Shows the Caption below the graph
def DFS():
    # #--------------------------------------DELETING/Getting rid of any previous graph-----------------------------------------
    global Graph, visuals, button, toolbar, G, fixed_nodes, fixed_positions, tree
    pos = zoinks.hierarchy_pos(G, 1)
    graph = tree
    visited = []
    stack = list(graph.keys())
    stack.append(1)
    dfslist = []
    nts = Entry1_7.get()  #No. to search
    #This will deal with empty Entry inputs
    if len(nts) == 0:
        nts = list(tree.keys())[-1]

    nx.draw(G,
            pos,
            fixed=None,
            with_labels=True,
            node_size=800,
            node_color='skyblue',
            node_shape="s",
            alpha=0.5,
            linewidths=10,
            font_size=8,
            font_weight='bold')

    while (len(visited) != len(graph)):
        popped_item = stack.pop()
        if ((popped_item) not in visited):  #Pop element
            visited.append(popped_item)  #processing
            dfslist.append(popped_item)
        for i in graph[
                popped_item]:  #Add all the popped neighbours in the stack
            if (i not in visited):
                stack.append(i)
    print(nts)
    x = []
    j = 0
    txt = ""
    for i in (dfslist):
        j = j + 1
        x.append(i)
        plt.title('DFS Iteration {}'.format(j))
        nx.draw_networkx_nodes(G,
                               pos,
                               nodelist=x,
                               node_size=400,
                               node_color='yellow',
                               node_shape="s",
                               alpha=0.5,
                               linewidths=10,
                               font_size=8,
                               font_weight='bold')
        if str(i) == str(nts):
            break
        plt.pause(0.7)
    txt = str(x)
    dfs = plt.text(0.5,
                   -0.1,
                   "DFS Order: " + txt,
                   size=12,
                   ha="center",
                   transform=ax.transAxes)  #Shows the Caption below the graph